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

Pages: 1-

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 4:21

are you sure you are supposed to do this? Such a file would have 20 * 16^20 = 24178516392292583494123520 bytes.

Name: Anonymous 2012-07-11 6:31

Read SICP

Name: Anonymous 2012-07-11 7:54

You're not returning anything from rand_string

Name: Anonymous 2012-07-11 9:43

This is the worst brute force attack I've ever seen

Name: !L33tUKZj5I 2012-07-11 9:50

I have a thought, why don't you just use crunch which will do the job for you?

Name: Anonymous 2012-07-11 12:32

Why not modify rand_string to append results to a list, first checking that list to make sure there are no duplicates, then print the contents of the list to a file when finished?

Name: Anonymous 2012-07-11 12:47

>>1

printed = set()
for _ in xrange(strings_to_print):
    s = rand_string(string_length)
    if s not in printed:
        print s
        printed.add(s)

But the chances to get a collision with 80 bit strings are pretty much negligible, so maybe it's not worth the effort.

Also, use ''.join(random.choice(CHARS) for _ in xrange(string_length)) instead of your silly loop.

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.

Name: Anonymous 2012-07-11 13:14

>>9
Sorry, I don't even know how that code is even supposed to work, I meant to post


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

Name: Anonymous 2012-07-11 14:58

Name: Anonymous 2012-07-11 16:23

It's gonna be a pretty big file.

#! /usr/bin/env python

import os
import sys

from itertools import product

def main (argv):
    chars  = 'abcdef1234567890'
    length = 20

    if len(argv) < 2:
        print >> sys.stderr, 'usage: %s filename' % (argv[0])
        return 1

    outfile = open(argv[1], 'w')

    for s in product(chars, repeat=length):
        outfile.write(s)
        outfile.write(os.linesep)

    outfile.close()

    return 0

if __name__ == '__main__':
    exit(main(sys.argv))

Name: Anonymous 2012-07-11 17:24

>>1
Hey OP you're a fucking fag.
head -c 10 /dev/urandom | hexdump -v -e '/1 "%02x"'; echo

Name: Anonymous 2012-07-11 17:27

>>13
You're the fag, Mr. >>13. Think about it.

Name: Anonymous 2012-07-11 17:30

>>14
Does your mom know your gay?

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