minion游戏子串一个接一个

时间:2016-12-17 02:02:26

标签: python slice

概述:

问题教程:HackerRank minion game practice tutorial

deck :: [Card]
deck = [Card rank suit | rank <- enumFrom minBound :: [Rank], suit <- enumFrom minBound :: [Suit]]

正如你所看到的,我是一个接一个,但我真的无法弄清楚错误的确切位置。

以下是代码:

Input: BAANANAS

Expected output: Kevin 19

My output: Kevin 18

任何注释掉的东西都可能用于调试(评论本身除外)

(注意:该函数在def minion_game(string): # your code goes here vowels = ('A', 'E', 'I', 'O', 'U') def kevin(string): kevin_list = [] for i in range(len(string)): if string[i] in vowels: return len(string) - i #Find every possible combination beginning with that letter for j in range(len(string)): #Gets rid of white-space characters...for some reason.... if j >= i and string[i:j+1] not in kevin_list: kevin_list.append(string[i:j+1]) return kevin_list def stuart(string): stuart_list = [] for i in range(len(string)): if string[i] not in vowels: #Find every possible combination beginning with that letter for j in range(len(string)): #Gets rid of white-space characters...for some reason.... if j >= i and string[i:j+1] not in stuart_list: stuart_list.append(string[i:j+1]) return stuart_list def points(words): points_list = [] for substring in words: points_list.append(string.count(substring)) return sum(points_list) def calculateWinner(player1, score1, player2, score2): if score1 > score2: return '%s %d' %(player1, score1) elif score2 > score1: return '%s %d' %(player2, score2) else: return 'Draw' #print(kevin(string)) #print("points:", points(kevin(string))) print(calculateWinner("Stuart", points(stuart(string)), "Kevin", points(kevin(string)))) 内调用,所以它被调用,不用担心。这只是它的定义)

1 个答案:

答案 0 :(得分:0)

没关系。 如果重叠,BAANANAS中的.count()方法不会将子字符串“ANA”计数两次。 如下所示: BA [ANA] NAS vs. BAAN [ANA] S. 它只计算其中一个,而不是两个。