Jun-27-2020, 09:53 PM
(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. 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.