HackerRank Python: The Minion Game Solution

Table of Contents

Question

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:

hackerrank

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:

  1. The function minion_game(string) is defined. It takes a single argument string, which represents the input string for the game.

  2. Two variables, k and s, are initialized with a value of 0. These variables will be used to store the scores of Kevin and Stuart, respectively.

  3. 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.

  4. A for loop is used to iterate over each character in the input string. The loop variable i represents the index of the current character.

  5. Inside the loop, an if statement checks if the current character (string[i]) is a vowel by using the in operator to check if it is present in the vowels string.

  6. 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.

  7. If the current character is not a vowel, the score of Stuart (s) is incremented using the same logic as in step 6.

  8. 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.

  9. 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.

  10. Inside the if block, the code prompts the user to enter a string by using the input() function and assigns it to the variable s.

  11. Finally, the minion_game(s) function is called, passing the input string s 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.

Sharing Is Caring:

Leave a Comment