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
#10
(Jun-27-2020, 08:43 PM)Emekadavid Wrote:
(Jun-27-2020, 07:28 PM)deanhystad Wrote: Why do you test if the letter is in vowels and then test if it is not in vowels?
That is the essence of the game. Given a string, count the number of substrings that start with a vowel, then count the number of substrings that start with a consonant. Then, print out the higher score, and if a draw, print out that it is the same score. My code scales for medium number of string lengths, but for high number of string lengths, like lengths of 100,000 characters, it fails to run to completion within the set time. I am looking for how to make it scale to such high string lengths.

Think about how you would determine if an unknown letter was a consonant or a vowel outside of a programming context. If someone told you they were thinking of a letter and it wasn't a vowel, would you then ask them if it was a consonant or would you already have enough information to determine that?

(Jun-27-2020, 08:43 PM)Emekadavid Wrote: My code is okay for the banana test case. Where it fails is where the string input is very long, like 100,000 characters. I want something that would scale to a string that is unknown of such a length. I followed your recommendation and what you described above is what I have done. See the last code above. I have removed the inner loop and counted the strings directly. But still, it doesn't scale to high string lengths.

I may have been looking at the wrong version of your code when I made my previous comment. Honestly, your code seems pretty close to optimal at this point to me. On my PC, it returns the result for your text file test case almost instantly. I modified your code to use what I *think* is a slightly simpler method of getting the substring count (instead of len(text[i:])), and I also handled vowels and consonants differently per my comment above, but it doesn't make much of a difference in discernible runtime. I even tried using a string about 10x as long as your test case and didn't see a lot of difference. Maybe it would be more appreciable on a slower machine, or maybe my code is no better optimized than yours. Big Grin

How are you being timed when you execute your test case? Are you looking at an average of execution times or just a single execution? Using something like the time module to calculate these tiny differences is not terrifically accurate.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help - random number/letter guessing game juin22 1 3,211 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,211 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guess the number game jackthechampion 5 3,210 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,831 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,287 Nov-16-2018, 03:56 AM
Last Post: Drone4four
  Strings inside other strings - substrings OmarSinno 2 3,691 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,908 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