The Minion Game in Python - HackerRank Solution
Problem :
Kevin and Stuart want to play the 'The Minion Game'.
Game Rules
Both players are given the same string, S.Both players have to make substrings using the letters of the string S.
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.
A player gets +1 point for each occurrence of the substring in the string S.
String S = BANANA
Kevin's vowel beginning word = ANA
Here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points.
For better understanding, see the image below:
fig : The Minion Game in Python - HackerRank Solution |
Your task is to determine the winner of the game and their score.
Input Format :
Note: The string S will contain only uppercase letters:[A - Z] .
Constraints :
- 0 < len(s) <= 10^6
Output Format :
If the game is a draw, print Draw.
Sample Input :
BANANA
Sample Output :
Stuart 12
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # The Minion Game in Python - Hacker Rank Solution def minion_game(string): # your code goes here # The Minion Game in Python - Hacker Rank Solution START player1 = 0; player2 = 0; str_len = len(string) for i in range(str_len): if s[i] in "AEIOU": player1 += (str_len)-i else : player2 += (str_len)-i if player1 > player2: print("Kevin", player1) elif player1 < player2: print("Stuart",player2) elif player1 == player2: print("Draw") else : print("Draw") # The Minion Game in Python - Hacker Rank Solution END if __name__ == '__main__': s = input() minion_game(s) |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
i too tried it out but my test cases are failing can anyone tell me why?
def minion_game(string):
str1=[]
str2=[]
vowels="AEIOU"
#making sub-strings
str=[s[i:j] for i in range(len(s)) for j in range(i+1,len(s)+1)]
for i in str:
if i[0] in vowels:
str1.append(i)
else:
str2.append(i)
str1=set(str1)
str2=set(str2)
n1=0
n2=0
for i in str1:
count1=str.count(i)
n1+=count1
for j in str2:
count2=str.count(j)
n2+=count2
if(n1>n2):
print("Kevin",n1,sep=" ")
elif(n2>n1):
print("Stuart",n2,sep=" ")
s=input()
minion_game(s)
Bro there is overlapping in the string so it can't be counted so you have to use regex for it
#making sub-strings <== if you are crafting all the subsets, it fails when the input is a very long string! it will generate gazillion subsets it simply won't work. The provided solution posted here works.
The solution here should also consider small letter vowels. We have to use "AEIOUaeiou" instead of just "AEIOU"
"Note: The string will contain only uppercase letters:" Note from the hackerrank website
I think this is incorrect solution, because here you do not take into account duplicates, one word can't be repeated more than one time.
Sorry I was making the problem difficult), it can be repeated.