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
#6
(Jun-27-2020, 04:25 PM)deanhystad Wrote: This is a really expensive way to do very simple math
            for j in range(i, len(text)):
                countConsonants += 1    
This is a logic problem, not a programming problem. Step away from the computer and think about how to solve the problem, not how you would write a program to solve the problem.
I decided to look into the model/logic based on what you said and removed the loop. Look at the new code. But the test cases still say that it did not execute within the time limit. Please, if you can help just help me.
def count_cons(text, vowels):
    countVowels = 0
    countConsonants = 0
    textlength = len(text)
    for i in range(textlength):
        if text[i] not in vowels :
            countConsonants += len(text[i:])
        else :
            if text[i] in vowels :
                countVowels += len(text[i:])
    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)
If anyone can give me the concept I am missing in order to optimize the code to run within a very short time, I will research it and try it out. That is why I came here.
Reply


Messages In This Thread
RE: Can someone help me optimize this game for large number of strings - by Emekadavid - Jun-27-2020, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help - random number/letter guessing game juin22 1 3,214 Aug-16-2020, 06:36 AM
Last Post: DPaul
  Dynamically chosing number of players in a "game" nsadams87xx 5 4,192 Jul-17-2020, 02:00 PM
Last Post: deanhystad
  making a guessing number game blacklight 1 2,212 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guess the number game jackthechampion 5 3,218 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,835 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,290 Nov-16-2018, 03:56 AM
Last Post: Drone4four
  Strings inside other strings - substrings OmarSinno 2 3,695 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,910 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