We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defminion_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=0stuart_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.foriinrange(len(string)):# Is current character a vowel (the beginning character of the current substring).ifstring[i]invowels:# Increment Kevin's score by adding the substring length to the score.kevin_score+=len(string)-ielse:# Increment Stuart's score by adding the substring length to the score.stuart_score+=len(string)-i# Determine the winner by comparing the scores.ifkevin_score>stuart_score:# If Kevin has a higher score, print Kevin's score.print(f"Kevin {kevin_score}")elifstuart_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")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Minion Game
You are viewing a single comment's thread. Return to all comments →