Python Forum
Can someone help me optimize this game for large number of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone help me optimize this game for large number of strings
#1
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.
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)
If you give it a string like 'BANANA' it will output
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

.txt   testcase.txt (Size: 4.88 KB / Downloads: 327)
Reply


Messages In This Thread
Can someone help me optimize this game for large number of strings - by Emekadavid - Jun-25-2020, 11:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help - random number/letter guessing game juin22 1 3,193 Aug-16-2020, 06:36 AM
Last Post: DPaul
  Dynamically chosing number of players in a "game" nsadams87xx 5 4,154 Jul-17-2020, 02:00 PM
Last Post: deanhystad
  making a guessing number game blacklight 1 2,193 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guess the number game jackthechampion 5 3,176 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,812 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,257 Nov-16-2018, 03:56 AM
Last Post: Drone4four
  Strings inside other strings - substrings OmarSinno 2 3,661 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,852 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020