python输出连续字母_python中的连续字母

I am trying to find consecutive letters in an entered string:

if a string contains three consecutive letters based on the layout of a UK QWERTY keyboard then a variable is given 5 points for each set of three.

e.g. asdFG would contain three consecutive sets. upper and lowercase do not matter.

can you please help as don't know where to begin with this?

解决方案

The easiest way would be to first generate all possible triples:

lines = ["`1234567890-=", "qwertyuiop[]", "asdfghjkl;'\\", "

triples = []

for line in lines:

for i in range(len(line)-2):

triples.append(line[i:i+3])

If you only want the characters and not numbers and brackets etc., substitute the lines above with

lines = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]

Now that we have all the triples, you can check with count how many times a triple occurs in inputed string.

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)

Bam, there you go. What this does is it counts for each triple how many times it occurs in a string so you know how many times to add 5 points. We use str.lower() to convert all characters to lowercase because as you said, capitalization does not matter.

If it is the same whether a string contains a certain triple once or three times, then you can do this:

input_string = input().strip().lower()

score = 0

for triple in triples:

if triple in input_string:

score += 5

print(score)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值