top of page

HackerRank - The Minion Game Solution

Apr 27

2 min read

0

12

0



Kevin and Stuart want to play the 'The Minion Game'.


Game Rules

Both players are given the same string, 

Both players have to make substrings using the letters of the string.

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.


For Example:

String  = BANANA

Kevin's vowel beginning word = ANA

Here, ANA occurs twice in BANANA.

Hence, Kevin will get 2 Points.


The solution is below:


def minion_game(string):

    # your code goes here

    vowels = 'AEIOU'

    length_of_string = len(s)

    consonant_value = 0

    vowel_value = 0

    #print(length_of_string)

    for index, letter in enumerate(s):

        if letter not in vowels:

            #print(letter)

            #print(index)

            consonant_value = consonant_value + (length_of_string - index)

        elif letter in vowels:

            vowel_value = vowel_value + (length_of_string - index)

           

    #print(consonant_value)

    #print(vowel_value)

    if consonant_value > vowel_value:

        print ("Stuart "+str(consonant_value))

    elif consonant_value < vowel_value:

        print("Kevin "+str(vowel_value))

        

    else:

        print("Draw")

if name == '__main__':

    s = input()

    minion_game(s)



The steps involved in solving this problem are as follows:

  1. Create a string of vowels.

  2. Calculate the length of the string and store it in a variable.

  3. Initialise two variables, one to hold the consonant substring count and one to hold the vowel substring count.

  4. Use an enumerated for loop, and calculate the difference between the current index and the length of the string - that will determine how many substrings can be made from that letter.

  5. Do this for consonants and vowels.

  6. Use an if statement to determine if Stuart or Kevin is the winner or if it is a draw.

Apr 27

2 min read

0

12

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page