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.
Kevin and Stuart want to play the 'The Minion Game'.
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 = BANANA
Kevin's vowel beginning word = ANA
Here, ANA occurs twice in BANANA. Hence, Kevin will get
For a better understanding, see the image below:
def minion_game(string): # your code goes here n = len(string) comb = ((n)*(n+1))/2 count_k = 0 count_s = 0 count_k = sum([len(string[i:]) for i in range(len(string)) if string[i] in "AEIOU"]) count_s = comb - count_k if count_s == count_k: print("Draw") elif count_s > count_k: print("Stuart", int(count_s) ) else: print("Kevin", int(count_k)) if __name__ == '__main__': s = input() minion_game(s)
Steps Used to solve the problem:
- To get the total number of substrings in a string we have used the formula comb = ((n)*(n+1))/2.
- After this we used list comprehension to check for all the words which are having vowels.
- For each vowel letter the combination of its words would be the number of words that are coming after that, which could be easily found using len(string[i:]).
- Once we found all the combinations of words that are vowels, we did the sum of them and stored them in the variable count_k.
- To find the count of Stuart we did subtract that with a total number of combinations using count_s = comb - count_k.
This was the solution for 'The Minion Game'. A very popular coding problem on Hackerrank.com
If you still face any problem with this solution, please do comment down below.
- Mradul Mishra
- 1 week, 2 days
- 62 Views