
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 c is a vowel, then
- 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)
- Related Articles
- C++ Program to find winner name of stick crossing game
- Program to find winner of stone game in Python
- Program to find winner of array removal game in Python
- Program to find winner of number reducing game in Python
- Program to find the winner of an array game using Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- C++ program to find winner of card game
- Program to find winner of a set element removal game in Python
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- Program to find maximum score of brick removal game in Python
- Program to find maximum score in stone game in Python
- C++ program to find winner of typing game after delay timing

Advertisements