0

我试图在输入的字符串中找到连续的字母:

如果一个字符串包含三个基于英国 QWERTY 键盘布局的连续字母,则每三个一组给一个变量 5 分。

例如 asdFG 将包含三个连续的集合。大小写无关紧要。

你能帮忙吗,因为不知道从哪里开始?

4

2 回答 2

1

最简单的方法是首先生成所有可能的三元组:

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)
于 2017-09-01T11:45:42.433 回答
-1
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)
于 2017-09-01T12:00:05.177 回答