The Minion Game

  • + 0 comments

    My solution involves iterating over the string and check if the character in question is a consonant or a vowel. If the character is a vowel then the length of the substring from that character till the end of the string is to be added to kevin's last recorded score. This is because for each possible substring 1 point is awarded, so if you have a string 'abcd', here 'a', 'ab', 'abc' and 'abcd' substrings will go into kevin's favour, which means he will gain 4 points from all the substrings starting from 'a' and 4 is also the length of the substring starting from 'a' till the very end of the string. Similarly, while iterating over the string if the character is a constant then the length of the substring from the character till the end of the string is to be added to stuart's last known score. After the iteration is complete then the scores are compared and the appropriate result is displayed.

    def minion_game(string):
        kevin_score=stuart_score=0
        length=len(string)
        for index, char in enumerate(string):
            if char in 'AEIOU':
                kevin_score+=length-index
            else:
                stuart_score+=length-index
        if kevin_score>stuart_score:
            print(f'Kevin {kevin_score}')
        elif stuart_score>kevin_score:
            print(f'Stuart {stuart_score}')
        else:
            print('Draw')