Jun-25-2020, 11:30 AM
(This post was last modified: Jun-25-2020, 11:31 AM by Emekadavid.)
I was given an assignment to create a game that takes a string as input and then evaluate the substrings starting with a consonant and the substrings starting with a vowel and print out which side, the vowel side or the consonant side, has a higher count. If a substring within a consonant or vowel repeats, the score for that substring is increased by one. Like if within a string like 'banana' substrings starting with consonants are b, ba, bana, banan, n, n, na, nan, etc, you can see that n and na repeats so they have scores of 2 while the rest have scores of 1. I will total the scores on the consonent side and compare it with the scores on the vowel side and print out the winner or if there is a draw. Please, can someone show me how to optimize this code to accept long strings. That is where the code fails. It gives runtime error to me or hangs my ide.
If you give it a string like 'BANANA' it will output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
def subsConsonant(text, startswith): ''' text - the string that we used as input startswith - the string of vowels --- returns a list of the substrings in the text that starts with a consonant ''' dlist = [text[i:j + 1 ] for i in range ( len (text)) for j in range (i, len (text)) if text[i] not in startswith] return dlist def subCount(aList): ''' aList - a list --- creates a dictionary from the list, the keys as the unique items in the list and the values as the count of each of the items --- returns the sum of the count of all the keys in the dictionary ''' countDict = {} for i in range ( len (aList)): if aList[i] in countDict: countDict[aList[i]] + = 1 else : countDict[aList[i]] = 1 theSum = sum (value for value in countDict.values()) return theSum def subsVowels(text, startswith): ''' text - string, the input string startswith - the string of vowels --- if a letter in text is a vowel, it takes all the substrings of that letter in the text and appends it to a list. --- returns the list of substrings starting with a vowel ''' dlist = [text[i:j + 1 ] for i in range ( len (text)) for j in range (i, len (text)) if text[i] in startswith] return dlist def gameDecision(stuartScore, kevinScore): ''' stuartScore - the score attributed to Stuart in the game kevinScore - the score attributed to Kevin in the game --- evaluates which of the scores is higher and prints out the higher score, otherwise it is a draw and it prints this out. --- return None ''' if stuartScore > kevinScore : print ( 'Stuart' , stuartScore) elif kevinScore > stuartScore : print ( 'Kevin' , kevinScore) else : print ( 'Draw' ) def minion_game(string): # your code goes here assert 0 < len (string)< = 1000000 vowels = 'AEIOU' stuartScore = subCount(subsConsonant(string, vowels)) kevinScore = subCount(subsVowels(string, vowels)) gameDecision(stuartScore, kevinScore) if __name__ = = '__main__' : s = input () minion_game(s) |
Output:'Stuart 12',
that is Stuart is the winner who is the consonant side with a score of 12. I have attached a txt file with a string of very long length that did not succeed with the game. If someone can try out the txt file and show me how to make it succeed, I will be grateful. The text file is named testcase.txt.
Attached Files