Friday, June 28, 2019

The Minion Game - Hacker Rank Solution

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.

For better understanding, see the image below: 
Your task is to determine the winner of the game and their score.
Input Format
A single line of input containing the string .
Note: The string  will contain only uppercase letters: .
Constraints

Output Format
Print one line: the name of the winner and their score separated by a space.
If the game is a draw, print Draw.
Sample Input
BANANA
Sample Output
Stuart 12
Note :
Vowels are only defined as . In this problem,  is not considered a vowel.





The Minion Game - Hacker Rank Solution



This challenge can be solved by finding 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:
B
BA
BAN
BANA
BANAN
BANANA
A
AN
ANA
ANAN
ANANA
N
NA
NAN
NANA
A
AN
ANA
N
NA
A
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.
See problem tester's code to learn more.

The Minion Game - Hacker Rank Solution
Problem Tester's code:
S = raw_input().strip()
S_length = len(S)
player1, player2 = 0,0

for i in xrange(S_length):
    if S[i] in "AEIOU":
        player1 += S_length - i
    else:
        player2 += S_length - i        
        
if player1 > player2:
    print "Kevin", player1
elif player1 < player2:
    print "Stuart", player2
else:
    print "Draw"



14 comments:

  1. Replies
    1. same issue , someone please help and give a less time consuming solution

      Delete
    2. Here is the working solution for The Minion Game problem in Python 3 language. (Try this)

      https://www.new951.com/2020/09/Program%20For%20The%20Minion%20Game%20%20In%20Python%20-%20Hacker%20rank%20Solution.html

      Delete
  2. def minion_game(string):
    Kevin = 0
    Stuart = 0
    word = list(string)
    x = len(word)
    vowels = ['A','E','I','O','U']
    for inx, w in enumerate(word):
    if w in vowels:
    Kevin = Kevin + x
    else:
    Stuart = Stuart + x
    x = x - 1
    if Stuart > Kevin:
    print ('Stuart', Stuart)
    elif Kevin > Stuart:
    print ('Kevin', Kevin)
    else:
    print ('Draw')

    ReplyDelete
  3. https://www.hackerrank.com/challenges/apple-and-orange/problem

    ReplyDelete
  4. def minion_game(string):
    Kevin = 0
    Stuart = 0
    word = list(string)
    x = len(word)
    vowels = ['A','E','I','O','U']
    for inx, w in enumerate(word):
    if w in vowels:
    Kevin = Kevin + x
    else:
    Stuart = Stuart + x
    x = x - 1
    if Stuart > Kevin:
    print ('Stuart', Stuart)
    elif Kevin > Stuart:
    print ('Kevin', Kevin)
    else:
    print ('Draw')

    ReplyDelete
  5. def minion_game(string):
    word = string

    mylist = []

    for x in range(0, len(word)):
    for y in range(x+1, len(word)):
    mylist.append(word[x:y])
    mylist.append(word[x:len(word)])

    vowels_list = ['A', 'E', 'I', 'O', 'U']
    vowels = []
    consonants = []

    for x in mylist:
    if x[0] in vowels_list:
    vowels.append(x)
    else:
    consonants.append(x)

    if len(vowels)>len(consonants):
    print(f'Kevin {len(vowels)}')
    elif len(vowels)<len(consonants):
    print(f'Stuart {len(consonants)}')
    else:
    print("Draw")


    if __name__ == '__main__':

    ReplyDelete
  6. def minion_game(string):
    stuart=0
    kevin=0
    for i in range(len(string)):
    if string[i] in "AEIOU":
    kevin=kevin+len(string)-i
    else:
    stuart=stuart+len(string)-i
    if stuart==kevin:
    print("Draw")
    elif stuart>kevin:
    print(f"Stuart {stuart}")
    else:
    print(f"Kevin {kevin}")


    if __name__ == '__main__':
    s = input()
    minion_game(s)

    ReplyDelete
  7. Here is the working solution for The Minion Game problem in Python 3 language.
    https://www.new951.com/2020/09/Program%20For%20The%20Minion%20Game%20%20In%20Python%20-%20Hacker%20rank%20Solution.html

    ReplyDelete
  8. list=[]
    list1=[]
    vowel=[]
    withoutvowel=[]
    for i in string.upper():
    list.append(i)
    for i in range(0,len(list)):
    for j in range(i+1,len(list)+1):
    list1.append(list[i:j])
    vowels=['A','E','I','O','U']
    for i in list1:
    if i[0] in vowels:
    vowel.append(i)
    else:
    withoutvowel.append(i)
    stuart=len(withoutvowel)
    kevin=len(vowel)
    if stuart>kevin:
    print(f"Stuart {stuart}")
    else:
    print(f"Kevin {kevin}")
    if __name__ == '__main__':
    s = input()
    minion_game(s)

    ReplyDelete
  9. def minion_game(string):
    # your code goes here
    s = list(string)
    vowel = ["A","E", "I", "O", "U"]
    stuart = []
    s_count = 0
    dum_s = []
    kevin = []
    k_count = 0
    dum_k = []

    for v in range(0, len(s)):
    for i in range(len(s)-v):
    x = string[i:i+v+1]
    x1 = list(x)
    if x1[0] not in vowel and x not in dum_s:
    stuart.append(x)
    dum_s.append(x)

    if x1[0] in vowel and x not in dum_k:
    kevin.append(x)
    dum_k.append(x)

    for v in range(0, len(s)):
    for i in range(len(s)-v):
    x = string[i:i+v+1]
    if x in stuart:
    s_count = s_count + 1
    if x in kevin:
    k_count = k_count + 1

    if s_count > k_count:
    print("Stuart", s_count)
    elif k_count> s_count:
    print("Kevin", k_count)
    else:
    print("Draw")


    if __name__ == '__main__':
    s = input()
    minion_game(s)

    time out can anyone help:(

    ReplyDelete
  10. NONE OF THE ABOVE CODE IS WORKING

    ReplyDelete

Powered by Blogger.