I need help to know where/ how i can run this code


I found this code to help me with something. It prints out every possibility that can be made from those characters. Problem is I don't know where or how to run the code. And everytime I put it into somewhere that it should run, it never finishes. It freezes half way through a set and never finishes. Please help me find out where to run this and or how to run it! Thank you in advance. And by the way, the script is Python if you didn't know

import itertools
# 16 is the length of your result.
res = itertools.product('abcdefghijklmnopqrstuvwxyz1234567890 ',repeat = 16) 
for i in res: 
   print ''.join(i)


Hi @dirtyd03 ,

Install Python 2.7.x on your own computer so you can have the freedom to run this program for a long time. You can get it at python.org.

Watch the output from the code that you posted, as it starts executing. You will soon discover that for it to run to completion, it will need a really really long time. Instead of using repeat = 16, you might rather try something like repeat = 4. You could try a shorter string than 'abcdefghijklmnopqrstuvwxyz1234567890 '.


That was going to be my suggestion.

import itertools
org = "are"
rep = len(org)
res = itertools.product(org,repeat = rep)
for i in res: 
   print (''.join(i))



27 different permutations with only repeat = 3. We can see how this grows exponentially very rapidly.


I have tried installing python but everytime i run it through cmd I get this "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape"


Hi, @dirtyd03 ,

I copied and pasted your code exactly as is, and ran it in IDLE (Python 2.7.5), without its raising a SyntaxError. The only problem with the run was that it would have taken more than the remainder of my lifetime for it to execute to completion, so I had to interrupt it.

When you encountered the error, were you running the same code that you posted?


yes. I ran same code


@dirtyd03 ,

Sorry, but your code runs perfectly on my machine, except for the time issue. We'll have to hope that someone else is able to reproduce the error, using your code, and then can report back to us.


Do you know how much it will take?


Nevermind. I figured out how to run the code. Thank you


Let's do some basic measurements and see what we may conclude.

import itertools
org = "abcdefghijklmnopqrstuvwxyz1234567890 "
def rep(r):
    return list(itertools.product(org, repeat = r))

Now we manually go through a few, changing the repeat

print len(rep(1)) # 37
print len(rep(2)) # 1369
print len(rep(3)) # 50653
print len(rep(4)) # 1874161 (IDLE 3.5)  ...Internal error. Out of memory (in the lab)
print len(rep(5)) # MemoryError in IDLE.
print len(rep(6)) # 
print len(rep(7)) #

but we can see the pattern forming here.

37            => 37 ** 1
37 * 37       => 37 ** 2
37 * 37 * 37  => 37 ** 3

on up to 37 ** 16 which is, 12 337 511 914 217 166 362 274 241

Those are iterations, each and every one. Magnitude 10 to the 25th power. Compare that with the estimated 10 to the 22 stars in the universe. 37! (factorial(37)) is around 10 to the 23rd.

Now consider that each iteration produces a tuple r characters in length, add in the commas and that is 32 characters per tuple. So that's at least 32 bytes per. Where I ran out of memory there would have been 69343957 tuples had it completed. That's 2 219 006 624 bytes of memory.

In and around 2 GB. I don't know how Python uses memory but my machine has !6 GB of RAM. How this correlates is beyond me. Maybe @appylpye can weigh in on this. At any length, we can see how unwieldly the list becomes, and we didn't even make it to 5 reps. Even if 5 reps were to succeed, I definitely wouldn't be able to handle 6.

Looking for ways to optimize on available memory, I took out list() in this test, and iterate over the itertools.object to get a count.

import itertools
org = "abcdefghijklmnopqrstuvwxyz1234567890 "

def repo(r): return itertools.product(org, repeat = r)

def count(it):
    count = 0
    for i in it:
        count += 1
    return count

Then I run count() on a repo() object.

print (count(repo(5)))

It took a while, but did eventually respond with, 69343957. Crappy way to benchmark since we run 6 X 10 ^ 7 iterations AFTER completion of the iterator. But it didn't push memory, just CPU clicks. Off to test 6 reps.

Update: Didn't crap out, but didn't end either after more than 3 mintues of runtime. I finally stopped it. Clearly, though, we are pushing the limits of our resources with this tool, and need to gauge our usage accordingly. Exponential growth can be a big problem.

This is a case for math to step in and calculate the numbers, rather than brute force generating them. This is, as we have seen an easily solved math problem. How many...?

Permutations and Combinations are both interesting topics that overlap this one. Definitely keep it on your study radar.

Off topic, but sort of not,

Some time ago a member started (and sadly abandoned) a topic on number base conversion, and contributed some code that he wanted help with. It's in the Corner Bar somewhere. I worked on the code and took it in the direction of building random sequences. Dig it up and see if it parallels or overlaps with what you have in mind in this topic.


Ok thank you. I'll go digging


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.