连续的字母
问题描述:
我试图找到一个输入的字符串连续的字母:连续的字母
如果字符串包含基于英国的QWERTY键盘则变量给出5分,每一套的布局连续三个字母三。
例如asdFG将包含三个连续的集合。大小写无关紧要。
请问你能帮忙,因为不知道从哪里开始呢?
答
最简单的方法是首先生成所有可能的三元组:
lines = ["`1234567890-=", "qwertyuiop[]", "asdfghjkl;'\\", "<zxcvbnm,./"]
triples = []
for line in lines:
for i in range(len(line)-2):
triples.append(line[i:i+3])
如果只想字符,而不是数字和支架等,代替lines
以上
lines = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
现在我们有所有的三元组,你可以用count
检查一个三元组在输入字符串中出现的次数。
input_string = input().strip().lower()
score = 0
for triple in triples:
number_of_occurrences = input_string.count(triple)
score += 5 * number_of_occurrences
print(score)
巴姆,你去了。它所做的是计算每个三元组在一个字符串中出现的次数,以便知道添加5个点的次数。我们使用str.lower()
将所有字符转换为小写,因为正如您所说,大小写并不重要。
如果是同一个字符串是否包含一定的三重一次或三次,那么你可以这样做:
input_string = input().strip().lower()
score = 0
for triple in triples:
if triple in input_string:
score += 5
print(score)
答
qwerty = 'qwertyuiopasdfghjklzxcvbnm'
inp = 'ASdfqazfghZZxc'
inp_lower = inp.lower()
points = 0
for idx in range(0, len(inp_lower) - 2):
test_seq = inp_lower[idx:idx + 3]
if test_seq in qwerty:
points += 5
print(test_seq, '->', points)
else:
print(test_seq)
是这条巨蟒2或3,我使用3这样会承担我将需要更改原始输入? – Jardis
是的,它是Python 2.我将它改为python 3。 – campovski