Python program to find score and name of winner of minion game


Suppose there are two players Amal and Bimal. They are playing a game. The game rules are as follows −

  • Both players have a same string s.

  • Both of them have to make substrings using the letters of s.

  • Bimal has to make words starting with consonants.

  • Amal has to make words starting with vowels.

  • The game will end when both players have made all possible substrings.

Now the scoring criteria is like: a player gains 1 point for each occurrence of the substring in the string s. We have to find winner of this game and his score.

So, if the input is like s = "BANANA", then the output will be Bimal, 12 because

Word : BANANA
Amal
Bimal(WINNER)
Substring
Score
Substring
Score
A
3
B
1
AN
2
N
2
ANA
2
BA
1
ANAN
1
NA
2
ANANA
1
BAN
1


NAN
1


BANA
1


NANA
1


BANAN
1


BANANA
1
Total 9
Total 12

To solve this, we will follow these steps −

  • vowels := a set of vowels
  • p1 := 0
  • p2 := 0
  • for each index i and character c in word, do
    • if c is a vowel, then
      • p2 := p2 + size of word - i
    • otherwise,
      • p1 := p1 + size of word - i
  • if p1 > p2, then
    • return 'Bimal', p1
  • otherwise when p2 > p1, then
    • return 'Amal', p2
  • otherwise,
    • return 'Draw'

Example

Let us see the following implementation to get better understanding

def solve(word):
   vowels = set('AEIOU')
   p1 = 0
   p2 = 0
   for i, c in enumerate(word):
      if c in vowels:
         p2 += len(word) - i
      else:
         p1 += len(word) - i
   if p1 > p2:
      return 'Bimal', p1
   elif p2 > p1:
      return 'Amal', p2
   else:
      return 'Draw'

word = "BANANA"
print(solve(word))

Input

"BANANA"

Output

('Bimal', 12)

Updated on: 12-Oct-2021

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements