The Minion Game

Michelle WayuaMichelle Wayua
6 min read

Problem Statement

Kevin and Stuart are playing a game called The Minion Game. The game starts with a single input string S. Both players must form as many substrings as possible from the original string. However, there’s a twist: Kevin can only create substrings that start with vowels (A, E, I, O, U), while Stuart can only create substrings that start with consonants.

Each occurrence of a valid substring within the original string earns the respective player 1 point. For instance, if a substring appears more than once in the string, it contributes multiple points. The game continues until all possible substrings have been considered. Your task is to write a Python function that takes the string S as input, calculates the total score for each player, and then prints the winner’s name along with their score.

Example;

#sample input
minion("BANANA")

#sample output
Stuart wins with 12 points

For better understanding, see the image below:

The Solution Code

def minion(string):
    n = len(string)

    #initializing scores
    kevin_score = 0
    stuart_score = 0


    for i in range(n):
        if string[i].lower() in "aeiou":
            kevin_score += (n - i)  # Kevin scores for substrings starting with vowels
        else:
            stuart_score += (n - i)  # Stuart scores for substrings starting with consonants

    # Print results
    if kevin_score > stuart_score:
        print(f"Kevin wins with {kevin_score} points")
    elif kevin_score < stuart_score:
        print(f"Stuart wins with {stuart_score} points")
    else:
        print("It's a tie!")

minion("BANANA")

Deep Dive: Breaking Down Each Line of the Code.

def minion(string):

This declares a function called minion that accepts a single input, a string. In our example, we'll use the string 'BANANA'.

    n = len(string)

Here, we use the len() function to determine the length of the string and assign the result to the variable n.

    #initializing scores
    kevin_score = 0
    stuart_score = 0

This line initializes two variables, kevin_score and stuart_score, and sets both of them to 0.

It’s like preparing two counters or score trackers for a game, or comparison between Kevin (player 1) and Stuart (player 2). Starting at 0 ensures both players begin with no points.

    for i in range(n):

This line initiates a loop that runs exactly n times, where n is the length of a string.

The range(n) function generates a sequence of numbers starting from 0 up to n , not including n. Example, the length of string ‘Banana‘ is 6. Looping through each letter by its index (i) will be:

b - 0

a - 1

n - 2

a - 3

n - 4

a - 5

Remember, indexing starts at 0, which is why we go up to but do not include n. If n is 6, the indices range from 0 to 5.

        if string[i].lower() in "aeiou":

This line checks whether the character at position i in the string is a vowel. It first accesses the character at index i using string[i], then converts it to lowercase with the .lower() method to ensure the comparison is case-insensitive. The in "aeiou" part checks whether this lowercase character is one of the five vowel letters.

If it is, the condition evaluates to True, and the code inside the if block will be executed.

            kevin_score += (n - i)  # Kevin scores for substrings starting with vowels

This line increases Kevin’s score by the value of n - i. This represents the number of possible substrings that can be formed starting from index i to the end of the string.

Since Kevin scores points for substrings that begin with vowels, this line is executed only when the current character at index i is a vowel.

Example, if i = 1 and the word has 6 letters, then there are 6 - 1 = 5 substrings starting from that position.

        else:
            stuart_score += (n - i)  # Stuart scores for substrings starting with consonants

This block is executed when the character at index i is not a vowel, the score is awarded to Stuart.

Similar to Kevin’s scoring logic, n - i represents the number of substring that can be formed starting from the current index i to the end of the string. Therefore, whenever a consonant is encountered, Stuart earns points equal to the number of possible substrings that begin with that consonant.

    if kevin_score > stuart_score:
        print(f"Kevin wins with {kevin_score} points")

This is a conditional block that checks whether Kevin has a higher score than Stuart. If the condition is True, it means Kevin has won, and the print() function is executed.

The print() statement uses an f-string, a feature in Python that allows variables to be embedded directly within a string by prefixing the string with the letter f and placing variables inside curly braces {}.

In this case, {kevin_score} will be placed by Kevin’s actual score value. So if Kevin scored 9 points, the message displayed would be, "Kevin wins with 9 points".

    elif kevin_score < stuart_score:
        print(f"Stuart wins with {stuart_score} points")

This block is a conditional statement that runs only if the first if condition (kevin_score > stuart_score) is False.

The keyword elif stands for “else if“ in python and allows you to check another condition when the previous one wasn’t met.

In this case, it checks if Stuart’s score is greater than Kevin’s. If this condition is True, the following print() statement is executed.

Like before, the print statement uses an f-string to insert the value of stuart_score directly into the message. For example, if Stuart scored 12 points, the output would be, "Stuart wins with 12 points".

    else:
        print("It's a tie!")

This block handles all the remaining cases not covered by the previous if and elif conditions.

In this context, it means the score of Kevin and Stuart are equal, resulting in a tie. Since the earlier conditions checked if Kevin’s score is greater than Stuart’s (if) and if Stuart’s score is greater then Kevin’s (elif), the only possibility left is that the scores are the same. When this happens, the program executed the print(“It’s a tie!“) statement. Try “BANANANAAAS“ and let me know 🙃.

NOTE: The else keyword in Python does not require a condition and must be followed by a colon (:).

minion("BANANA")

This is a function call that runs everything.

🎯 Final Takeaway

The Minion Game is a great exercise to strengthen your understanding of string manipulation, loops, and conditional logic in Python. Through a fun and competitive scenario, it teaches how to work with substrings efficiently, especially by using smart counting techniques rather than generating all possible substrings manually, which would be computationally expensive for large strings.

By tracking each character's position and determining how many substrings start there, we avoid unnecessary computation and keep our solution fast even for longer inputs. This approach not only solves the challenge correctly but also introduces a more algorithmic way of thinking, a key skill for any aspiring developer or data scientist.

So whether you're learning Python for web development, data analysis, or just for fun, tackling problems like The Minion Game is a fantastic way to level up your skills. Keep practicing, keep building, and most importantly — keep having fun with code! 🧠💡

0
Subscribe to my newsletter

Read articles from Michelle Wayua directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Michelle Wayua
Michelle Wayua