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.
- Prepare
- Python
- Strings
- The Minion Game
- Discussions
The Minion Game
The Minion Game
+ 1 comment Count the length of substring of each vowel or consonant
def minion_game(string): kevin=0 k=[] st=[] stuart=0 for i in range(len(string)): temp=[i,string[i]] if string[i]=='A' or string[i]=='E' or string[i]=='I' or string[i]=='O' or string[i]=='U': k.append(temp) else: st.append(temp) for t in st: val=len(string[t[0]:]) stuart+=val for t in k: val=len(string[t[0]:]) kevin+=val if kevin>stuart: print(f"Kevin {kevin}") elif stuart>kevin: print(f"Stuart {stuart}") else: print("Draw")
+ 1 comment def minion_game(string): vowels = "AEIOU" kevin_score = 0 stuart_score = 0 length = len(string)
for i in range(length): if string[i] in vowels: kevin_score += length - i else: stuart_score += length - i 2 tests was wrong , how to check what is wrong? if kevin_score > stuart_score: print(f"Kevin {kevin_score}") elif kevin_score < stuart_score: print(f"Stuart {stuart_score}")
+ 0 comments def minion_game(string): stuart = 0 kevin = 0 stuart = score_it(string,'BCDFGHJKLMNPQRSTVWXYZ') kevin = score_it(string,'AEIOU') if stuart > kevin: print("Stuart", stuart) elif kevin > stuart: print("Kevin", kevin) else: print("Draw") def score_it(s,starts='LETTERS'): score = 0 for i, c in enumerate(list(s)): if c in starts: score += len(s) - i #some of the edge cases are ridiculously long #kept getting memory errors, until optimized the scoring #by just counting chars, instead of lists of substrings return score
+ 0 comments It's an interesting problem, but the uploader has explained it in very complicated terms. From the left-most 'a' in 'banana' we get the FIVE words a-an-ana-anan-anana, and that is what matters. Then we need not worry about how many times a phrase 'a' or 'an' gets repeated. If we keep a list of which substring has been repeated how many times, the code becomes a bit more complex.
+ 2 comments python3
def minion_game(string): # Initialize the scores for Kevin and Stuart kevin_score = 0 stuart_score = 0 # Get the length of the string n = len(string) # Loop through each character in the string for i in range(n): # If the character is a vowel, add points to Kevin's score if string[i] in 'AEIOU': kevin_score += n - i # If the character is a consonant, add points to Stuart's score else: stuart_score += n - i # Determine and announce the winner if kevin_score > stuart_score: print("Kevin", kevin_score) elif kevin_score < stuart_score: print("Stuart", stuart_score) else: print("Draw") if __name__ == '__main__': s = input() minion_game(s)
Load more conversations
Sort 1105 Discussions, By:
Please Login in order to post a comment