The Minion Game

  • + 0 comments

    Wow, first I created a list with all the words, it worked for the example, but the validation has strings that are VERY long so my code was slow. I then realized I just need to count how many letters are in between the starting points and the end of the word.

    like for Banana, let's say starting point is the first "a"

    possible words: a an ana anan anana

    --> add 5 to score = len(string[start:]) or len(range(start, N))

    which is much more efficient.

    import re
    
    def minion_game(string):
        
        def get_score(pattern, string):
            score = 0
            N = len(string)
            for match in re.finditer(pattern, string, re.IGNORECASE):
                start = match.start()
                score += len(range(start, N))
            return score
        
        pattern_vowels = r"(?=[aeiou])"  # Use a capturing group inside a lookahead
        pattern_consonants = r"(?=[bcdfghjklmnpqrstvwxyz])"
        
        score_stuart = get_score(pattern_consonants, string)
        score_kevin =  get_score(pattern_vowels, string)
        if score_kevin > score_stuart:
            print ("Kevin", score_kevin)
        elif score_stuart > score_kevin:
            print("Stuart", score_stuart)
        else:
            print("Draw")