Overview :
Problem tutorial: HackerRank minion game practice tutorial
Input: BAANANAS
Expected output: Kevin 19
My output: Kevin 18
As you can see, I'm off-by-one, but I really can't figure out exactly where the error would be.
Here's the code :
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))))
Anything commented out was probably used for debugging (except for the comments themselves)
(Note: the function is called inside main()
, so it's being called, don't worry. This is just the definition of it)
Nevermind. The .count() method in BAANANAS does not count the substring "ANA" twice if it overlaps. As in the following: BA[ANA]NAS vs. BAAN[ANA]S It only counts one of these, not both.
Try this code. It is much simpler.
def minion_game(string):
stuCount=kevCount=0
for i in range(len(string)):
if string[i] not in "AEIOU":
stuCount+=len(string)-i
for i in range(len(string)):
if string[i] in "AEIOU":
kevCount+=len(string)-i
if stuCount > kevCount:
print("Stuart"+" "+str(stuCount))
elif kevCount > stuCount:
print("Kevin"+" "+str(kevCount))
else:
print("Draw")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.