The Minion Game

  • + 2 comments
    def minion_game(string):
        # Define the vowels. This will be used to check if a character is a vowel.
        vowels = "AEIOU"
        
        # Initialize scores for Kevin and Stuart.
        kevin_score = 0
        stuart_score = 0
    
        # We loop through each character of the string.
        # Each character starts new set of substrings.
        # Here's why:
        #       We start at "B" in "BANANA", with 6 possible substrings:
        #       1. "BANANA"
        #       2. "BANAN"
        #       3. "BANA"
        #       4. "BAN"
        #       5. "BA"
        #       6. "B"
        #       Next is "A" in "BANANA", with 5 possible substrings:
        #       1. "ANANA"
        #       2. "ANAN"
        #       3. "ANA
        #       4. "AN"
        #       5. "A"
        # Luckily, the number of substrings = current substring length
        # Example: "BANANA" = 6 characters and "ANANA" = 5 characters.
        # Therefore, the current character count = points to add to the running score.
        for i in range(len(string)):
            # Is current character a vowel (the beginning character of the current substring).
            if string[i] in vowels:
                # Increment Kevin's score by adding the substring length to the score.
                kevin_score += len(string) - i
            else:
                # Increment Stuart's score by adding the substring length to the score.
                stuart_score += len(string) - i
    
        # Determine the winner by comparing the scores.
        if kevin_score > stuart_score:
            # If Kevin has a higher score, print Kevin's score.
            print(f"Kevin {kevin_score}")
        elif stuart_score > kevin_score:
            # If Stuart has a higher score, print Stuart's score.
            print(f"Stuart {stuart_score}")
        else:
            # If scores are equal, print Draw.
            print("Draw")