25 Sep The Minion Game
Hello Friends, How are you? Today I am going to solve the HackerRank Python The Minion Game Problem with a very easy explanation. In this article, you will get more than one approach to solve this problem. So let’s start- {tocify} $title={Table of Contents} Kevin and Stuart want to play ‘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. 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:
Source: The Minion Game Python HackerRank |
Your task is to determine the winner of the game and their score.
- Complete the minion_game in the editor below.
- minion_game has the following parameters:
- string: the string to analyse
- string: the winner’s name and score, separated by a space on one line, or Draw if there is no winner
A single line of input containing the string S. Note: The string S will contain only uppercase letters: [A – Z].
BANANA {codeBox}
Stuart 12 {codeBox}
Vowels are only defined as AEIOU. In this problem, Y is not considered a vowel. This challenge can be solved by findings all the substrings of the string and separating the substrings on the basis of their first character. If the string is BANANA, the substrings are: It is interesting to note that in BANANA: Words formed using the first letter B = 6 Words formed using the second letter A = 5 Words formed using the third letter N = 4 Words formed using the fourth letter A = 3 Words formed using the fifth letter N = 2 Words formed using the last letter A = 1 Using this pattern, you can run a loop from the start to the end of the string and filter out words starting with vowels and consonants. Approach I: The Minion Game HackerRank Python Solution
# ========================
# Information
# ======================== # Name: The Minion Game - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/the-minion-game/problem
# Difficulty: Medium
# Max Score: 40
# Language: Python 3 # ========================
# Solution Start
# ======================== # The Minion Game Python - Hacker Rank Solution
def minion_game(string): # your code goes here vowels = 'AEIOU' str_lenght = len(string) kevin_score, stuart_score = 0, 0 for i in range(str_lenght): if s[i] in vowels: kevin_score += (str_lenght - i) else: stuart_score += (str_lenght - i) if kevin_score > stuart_score: print("Kevin", kevin_score) elif kevin_score < stuart_score: print("Stuart", stuart_score) else: print("Draw") #your code ends here if __name__ == '__main__': s = input() minion_game(s) # The Minion Game in Python - Hacker Rank Solution END
# MyEduWaves
Approach II: The Minion Game HackerRank Python Solution
# ========================
# Information
# ======================== # Name: The Minion Game - Python HackerRank Problem
# Direct Link: https://www.hackerrank.com/challenges/the-minion-game/problem
# Difficulty: Medium
# Max Score: 40
# Language: Python 3 # ========================
# Solution Start
# ======================== # The Minion Game Python - Hacker Rank Solution
def minion_game(string): # your code goes here 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") #your code ends here if __name__ == '__main__': s = input() minion_game(s) # The Minion Game in Python - Hacker Rank Solution END
# MyEduWaves
Sorry, the comment form is closed at this time.