You don't need two for loops. You only need one and do both words at the same time. Pull count and a up top and do while a != 'hello world' and count < 100000000
Yes, multithreading is the answer. Have an asynchronous function that creates workers, each will generate two words and check them, then have a collector that collects the answers as they come in. Terminate the generator once you get hello world.
import random, string, time
for c in "Hello World!":
while (x:=random.choice("Hello World!" + string.ascii_uppercase)) != c:
print(x, end="", flush=True)
time.sleep(0.05)
print("\b", end="")
print(c, end="")
(Disclaimer: The \b may not work in every console.)
You basically generate two 5-letter words with random letters from a list of possible letters. This is not a smart way to approach what is basically brute forcing.
I recommend you rewrite the word generator parts so they generate aaaaa, aaaab, aaaac etc in sequence. That way, you van never get the same word twice, speeding up the algorithm.
Even better, which is slightly more advanced: import a list of 5-letter words from a dictionary and simply check those. This only works if the string you are looking for consists of actual words too, of course.
I don't know python well enough to tell you what's wrong but I do believe that constants shouldn't be redeclared each loop, just put them at the top where count is
Creating a string by appending single characters to a list and then using ''.join(list) instead of just appending directly to a string is big brain time.
Looks like you are wasting a lot of bytes on variable names. It definitely makes it go slower. Just rename them to one symbol, just like you did with variable a.
Python is about 100 times slower than optimized C/C++/Rust code. If you're doing a certain amount of computation that simply needs to be done and need it to be faster, consider a faster language.
Edit: You have a lot of optimizations you can do here though. For example, constructing a new string millions of times isn't needed here (although it does make for "cleaner" code that is easier to understand). You can get by generating an array of indices and checking that array against the indices you know would produce "Hello world!", skipping the string construction entirely. This would look like if indices[0] == 0 && indices[1] == 1 && .... Fastest would be generating future indices only if you know the ones generated match so far. You'd only need to keep a single integer around, generating new ones within nested if statements that check that it is the right index. If so, replace that integer with a new random integer to see if it is the next needed index, and so on until you have generated all the correct indices.
An important lesson here is the concept of clean code itself. Generally, the more you optimize code, the more difficult it becomes to understand. More and more tricks are used that someone looking at the code has to comprehend. Here, they have to understand you are checking if you'd construct the right string by examining only indices into a string helowrd. The saying that premature optimization is the root of all evil relates to this fact. In corporate code, very often proper design is not as optimized as it could be, favoring other things instead that promote understanding and changing the code in the future.
285
u/SarahAlicia Mar 07 '23
I just noticed the import in the loop. Inspired. This is art.
44
u/halfanothersdozen Mar 07 '23
You don't need two for loops. You only need one and do both words at the same time. Pull count and a up top and do
while a != 'hello world' and count < 10000000078
u/Yonnie-Donnie Mar 07 '23
Have you tried turning it on and off again that usually works
14
u/Rene_Z Mar 07 '23
When I run it, it just prints
Must be a problem with your machine.
13
u/KenTheSystrainee Mar 07 '23
I think you basically did everything possible to make it run as slow as possible…
11
u/OutrageousTravel9343 evil Mar 07 '23
separator and separator_two have the same content
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 07 '23
Image Transcription: Code
[The image shows code and console output in a Jupyter notebook. The code reads as follows:]
[The console output reads as follows:]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
13
u/trutheality Mar 07 '23
Yes, multithreading is the answer. Have an asynchronous function that creates workers, each will generate two words and check them, then have a collector that collects the answers as they come in. Terminate the generator once you get hello world.
11
u/CodeSmell12 Mar 07 '23
You may try to initialize word_one and word_two with hello and world. I think it can be faster than your algorithm.
25
u/[deleted] Mar 07 '23
Python is very bad at for loops, you should avoid those as much as you can.
4
u/_pestarzt_ Mar 07 '23
I think you need caching… try caching the result to “hello world!” and see how that runs.
1
u/DecisivePro Mar 07 '23
word_one and word_two should be created outside the for loops, preferably in the a=... line
1
u/PhysicalRaspberry565 Mar 07 '23
Import only once (at the beginning, doesn't reload anyways) and remove the print commands
1
u/[deleted] Mar 08 '23
Use red syntaxic coloration. Red one goes fasttaaaa
1
u/robin_888 Mar 08 '23
This reminds me of something I tried before:
(Disclaimer: The
\bmay not work in every console.)1
u/Basacally Mar 08 '23
You basically generate two 5-letter words with random letters from a list of possible letters. This is not a smart way to approach what is basically brute forcing.
I recommend you rewrite the word generator parts so they generate aaaaa, aaaab, aaaac etc in sequence. That way, you van never get the same word twice, speeding up the algorithm.
Even better, which is slightly more advanced: import a list of 5-letter words from a dictionary and simply check those. This only works if the string you are looking for consists of actual words too, of course.
1
u/dome_f Mar 08 '23
Ask ChatGPT.
1
u/fiddz0r Mar 08 '23
This was a pretty good post to this sub.
I don't know python well enough to tell you what's wrong but I do believe that constants shouldn't be redeclared each loop, just put them at the top where count is
1
u/SuitableDragonfly Mar 08 '23
Creating a string by appending single characters to a list and then using
''.join(list)instead of just appending directly to a string is big brain time.1
u/Dominio12 Mar 08 '23
Looks like you are wasting a lot of bytes on variable names. It definitely makes it go slower. Just rename them to one symbol, just like you did with variable a.
1
u/zickige_zicke Mar 08 '23
Dont use python
1
u/Estatic_Penguin Mar 08 '23
while True with 2 for loops ! . By chance did it take 20 mins to run ?
1
u/PixelPerfect41 Mar 08 '23
You should import libraries with multithreading that way it will be convinient and faster to load the library multiple times
1
u/Soporiferus Mar 09 '23
I think kubernettes can improve it by making it cloud native.
0
u/kelmano Mar 07 '23
Import the Worth:Ram=<Dword=1 4096=1>
0
u/row6666 Mar 07 '23
use c
0
u/tedbradly Mar 08 '23 edited Mar 08 '23
Python is about 100 times slower than optimized C/C++/Rust code. If you're doing a certain amount of computation that simply needs to be done and need it to be faster, consider a faster language.
Edit: You have a lot of optimizations you can do here though. For example, constructing a new string millions of times isn't needed here (although it does make for "cleaner" code that is easier to understand). You can get by generating an array of indices and checking that array against the indices you know would produce "Hello world!", skipping the string construction entirely. This would look like
if indices[0] == 0 && indices[1] == 1 && .... Fastest would be generating future indices only if you know the ones generated match so far. You'd only need to keep a single integer around, generating new ones within nestedifstatements that check that it is the right index. If so, replace that integer with a new random integer to see if it is the next needed index, and so on until you have generated all the correct indices.An important lesson here is the concept of clean code itself. Generally, the more you optimize code, the more difficult it becomes to understand. More and more tricks are used that someone looking at the code has to comprehend. Here, they have to understand you are checking if you'd construct the right string by examining only indices into a string
helowrd. The saying that premature optimization is the root of all evil relates to this fact. In corporate code, very often proper design is not as optimized as it could be, favoring other things instead that promote understanding and changing the code in the future.0
u/TheProfessorDragon Mar 08 '23
c++ makes everything run faster