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
#11
In your program a letter is either a vowel or it isn't (no confusion about Y) you certainly don't have to test twice.

Why are you making a substring? You know how long the string is. You know the index of the letter you are testing. Can't you come up with an equation to calculate the number of substrings? Why do you insist on counting when math is much faster?

If you have a string of length N, how many different substrings can you make that start with string[i]? Present answer in the form of an equation. Instead of solving a simple equation you would rather count from 0 to 9,999 in a loop and then do it again from 1 to 9,999, 1 to 9,999. Now you want to make a substring that is 10,000 characters long, then 9,999 characters long, then 9,998, etc...

No wonder your program is slow.
Reply
#12
(Jun-27-2020, 11:13 PM)deanhystad Wrote: No wonder your program is slow.

I had to use significantly larger strings than the testcase.txt file to finally see a consistent difference in execution speed, but doing that did show that your code has room for improvement. As deanhystad has indicated, the inefficiencies in your code stem from doing an unnecessary second test when distinguishing consonants from vowels, and from your method of counting substrings. Instead of using len(text[i:]) as you currently do, you should be able to get this same value from an equation that uses variables you have already defined at this point in your code.
Reply
#13
(Jun-27-2020, 11:13 PM)deanhystad Wrote: In your program a letter is either a vowel or it isn't (no confusion about Y) you certainly don't have to test twice.

Why are you making a substring? You know how long the string is. You know the index of the letter you are testing. Can't you come up with an equation to calculate the number of substrings? Why do you insist on counting when math is much faster?

If you have a string of length N, how many different substrings can you make that start with string[i]? Present answer in the form of an equation. Instead of solving a simple equation you would rather count from 0 to 9,999 in a loop and then do it again from 1 to 9,999, 1 to 9,999. Now you want to make a substring that is 10,000 characters long, then 9,999 characters long, then 9,998, etc...

No wonder your program is slow.
Thanks for your reply about using an equation. I never ever thought of that. It escaped my notice. I want to say a big thank you. It really helped. Now, it is optimized and the problem solved. Here is my code. I used a generator because the string length being long I didn't want something on memory. You all really helped me on this one. Now I have learned something new about python thanks to all of you.
def getNextChar(text):
    n = 0
    while n < len(text) :
        yield (text[n], n)
        n += 1

def count_cons(text, vowels):
    countVowels = 0
    countConsonants = 0
    nextChar = getNextChar(text)
    for char in nextChar:
        if char[0] not in vowels :
            countConsonants += len(text) - char[1]
        else :
            countVowels += len(text) - char[1]        
    if countConsonants > countVowels :
        print('Stuart', countConsonants)
    elif countVowels > countConsonants :
        print('Kevin', countVowels)
    else :
        print('Draw')                                                

def minion_game(string):
    # your code goes here
    assert 0<len(string)<=1000000
    vowels = 'AEIOU'
    count_cons(string, vowels)

if __name__ == '__main__':
    s = input()
    minion_game(s)

(Jun-28-2020, 07:20 PM)GOTO10 Wrote:
(Jun-27-2020, 11:13 PM)deanhystad Wrote: No wonder your program is slow.

I had to use significantly larger strings than the testcase.txt file to finally see a consistent difference in execution speed, but doing that did show that your code has room for improvement. As deanhystad has indicated, the inefficiencies in your code stem from doing an unnecessary second test when distinguishing consonants from vowels, and from your method of counting substrings. Instead of using len(text[i:]) as you currently do, you should be able to get this same value from an equation that uses variables you have already defined at this point in your code.
Thanks. I used an equation and it did the job. I appreciate your kindness.
Reply
#14
The way you are using a generator doesn't save memory. It consumes more memory and runs slower than code that uses the list directly. Generators are useful when they can return a partial result. There is no partial result when iterating over an existing list to get the list values.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help - random number/letter guessing game juin22 1 3,194 Aug-16-2020, 06:36 AM
Last Post: DPaul
  Dynamically chosing number of players in a "game" nsadams87xx 5 4,157 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,178 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,813 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,260 Nov-16-2018, 03:56 AM
Last Post: Drone4four
  Strings inside other strings - substrings OmarSinno 2 3,662 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,854 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