。葡萄酒词是每个字母都有一个相邻字母的词。铁匠是邻接的,因为每个字母至少有一个相邻的字母,所以下面是程序运行所需的:
Line: The blacksmith fights in the tower
Vicinals: blacksmith fights
Non-vicinals: The in the tower
Line: The baker fights in the street
Vicinals: fights
Non-vicinals: The in the
Line: The butcher flees from the scene
Non-vicinals: The from the scene
Line:
到目前为止,我有这个,基本上我还没有把它放入一个循环中,但似乎我的ord比较不起作用,出于某种原因,我得到了一个糟糕的输出。
line = input('Line: ').lower().split()
for item in line:
listy = []
nonvicinals = []
yesvicinals = []
vicinal = True
for letter in item:
listy.append(ord(letter))
for num in listy:
if int(num) != 97 or int(num) != 122:
# because if it is a or z (97, 122) the number can wrap around
if (num + 1) in listy or (num - 1) in listy:
vicinal = True
else:
vicinal = False
else:
if int(num) == 97:
if (num + 1) in listy or 122 in listy:
vicinal = True
else:
vicinal = False
else:
if (num - 1) in listy or 97 in listy:
vicinal = True
else:
vicinal = False
if vicinal == False:
nonvicinals.append(item)
break
if vicinal == True:
yesvicinals.append(item)
if yesvicinals:
print('vicinals: {}'.format(' '.join(yesvicinals)))
if nonvicinals:
print('Non-vicinals: {}'.format(' '.join(nonvicinals)))
现在我在第一个例子中得到的输出是:
Line: The blacksmith fights in the tower
Non-vicinals: tower
我做错了什么?为什么我得不到想要的结果?
而且我的代码坦率地说真的很长而且非常难看。有没有更快的方法来使用lambdas/理解等?
感谢您的帮助,这已经困扰了我好几个小时了!
谢谢,
Itechmatrix公司
最佳答案
这是一个来自当前编程竞赛的问题。请不要为用户24492工作。
user24492/Itechmatrix,有论坛和导师可供您选择。请用那些来代替。