Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Need help with python

Name: shink !xi8/JKFwzo 2012-07-11 4:08

hey /prog/
Need help with python
I am randomly generating all possible 20 character long strings consisting of 1234567890abcdef, and printing them to a file.
I generate the strings using:

CHARS="abcdef1234567890"
def rand_string(string_length):
        output = ''            
       
        for i in xrange(string_length):
                output += random.choice(CHARS)


Now i need to print rand_string(20) over and over again to an output file, but checking said file for that string, and only printing it if it isn't there yet.

Any thoughts?

Name: Anonymous 2012-07-11 13:12

Okay I'm a total novice programmer but I'm having a go for the sake of practice.


def random_strings(string_length):
    CHARS = "abcdef1234567890"
    output = []
    while len(output) < 16**string_length:
        string=""
        string.join(random.choice(CHARS) for _ in xrange(string_length))
        if string not in output:
            output.append(string)
    return output


Of course I know this is going to take years to come back at a length of 20 characters, so there must be a smarter way to do it, anyone know what it is?

>>8

Couple of questions, since you seem to know what you're talking about.

If printed is a type set, isn't it redundant to have if s not in printed in there? Because sets don't allow duplicates so it won't get added anyway?

Also, why do you call that loop silly? Isn't ''.join(random.choice(CHARS) for _ in xrange(string_length)) the same loop, just syntactically different?

Excuse my ignorance, like I said I'm a novice.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List