问题陈述如下
Game Rules
Both players are given the same string, . Both players have to make substrings using the letters of the string . Stuart has to make words starting with consonants. Kevin has to make words starting with vowels. The game ends when both players have made all possible substrings.
Scoring A player gets +1 point for each occurrence of the substring in the string .
For Example: String = BANANA Kevin's vowel beginning word = ANA Here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points. Your task is to determine the winner of the game and their score.
代码:
def minion_game(string):
kevin,stuart=0,0
for i in range(0,len(string)):
for j in range(i,len(string)):
if string[i:j+1][0]=='A' or string[i:j+1][0]=='E' or string[i:j+1][0]=='I' or string[i:j+1][0]=='O' or string[i:j+1][0]=='U':
kevin=kevin+1
else:
stuart=stuart+1
if kevin>stuart:
print('Kevin',kevin)
elif kevin<stuart:
print('Stuart',stuart)
else:
print('Draw')
s = input()
minion_game(s)
输入: Click here
预期产量: 斯图尔特7501500
输出: 由于超时而终止
这是我解决问题的老办法
诀窍是要意识到你不需要尝试所有的组合。一旦你看到了元音或辅音,你就可以确定还有当前字符串子字符串的长度,所以你只需加上那么多点
举个例子,给定Banana这个词,我们看到a B,我们马上知道{B,BA,BAN,BANA,BANA,BANAN,BANA}都会给斯图亚特加分。不用再检查了
相关问题 更多 >
编程相关推荐