Table of Contents
ToggleQuestion
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.
Scoring
A player gets +1
point for each occurrence of the substring in the string S.
For Example:
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:
Your task is to determine the winner of the game and their score.
Function Description
Complete the minion_game in the editor below.
minion_game has the following parameters:
- string string: the string to analyze
Prints
- string: the winner’s name and score, separated by a space on one line, or
Draw
if there is no winner
Input Format
A single line of input containing the string S.
Note: The string S will contain only uppercase letters:[A-Z].
Constraints
0<len(S)<10^6
Sample Input
BANANA
Sample Output
Stuart 12
Note :
Vowels are only defined as AEIOU. In this problem, Y is not considered a vowel.
Solution
def minion_game(string):
k=0
s=0
vowels="AaEeIiOoUu"
for i in range(len(string)):
if string[i] in vowels:
k=k+len(string)-i
else:
s=s+len(string)-i
if k>s:
print("Kevin",k)
elif k==s:
print("Draw")
else:
print("Stuart",s)
if __name__ == '__main__':
s = input()
minion_game(s)
Here’s a breakdown of how the code works:
The function
minion_game(string)
is defined. It takes a single argumentstring
, which represents the input string for the game.Two variables,
k
ands
, are initialized with a value of 0. These variables will be used to store the scores of Kevin and Stuart, respectively.The string of vowels,
vowels
, is defined as “AaEeIiOoUu”. This string will be used to check if a character in the input string is a vowel.A
for
loop is used to iterate over each character in the input string. The loop variablei
represents the index of the current character.Inside the loop, an
if
statement checks if the current character (string[i]
) is a vowel by using thein
operator to check if it is present in thevowels
string.If the current character is a vowel, the score of Kevin (
k
) is incremented by the difference between the length of the input string and the current index (len(string)-i
). This is done to assign higher scores to substrings that start with a vowel and have more characters remaining.If the current character is not a vowel, the score of Stuart (
s
) is incremented using the same logic as in step 6.After the loop finishes, the scores of Kevin and Stuart are compared. If Kevin’s score (
k
) is greater than Stuart’s score (s
), it prints “Kevin” along with Kevin’s score. If the scores are equal, it prints “Draw”. Otherwise, it prints “Stuart” along with Stuart’s score.The
if __name__ == '__main__':
block is used to check if the code is being run as a standalone script and not imported as a module.Inside the
if
block, the code prompts the user to enter a string by using theinput()
function and assigns it to the variables
.Finally, the
minion_game(s)
function is called, passing the input strings
as an argument, and the game is played.
In summary, this code implements the Minion Game, where players earn points based on substrings of a given string that start with a vowel (Kevin) or a consonant (Stuart). The scores are calculated using the length of the remaining substring, and the player with the higher score is declared the winner.
If you find anything wrong in this Solution, feel free to reach us in the comment section.