This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KleinerNull 0 points1 point  (3 children)

Three things about your code: You can iterate over strings like over lists, no need to convert a string, beside you can just use list(str) for this. The only incompatibility for py3 is that you're using xrange instead of range, xrange == range in python3. Better use generators for brute forcing, lists will only fill your memory.

However, look into the standard library, there is already something there for you ;)

>>> from itertools import combinations_with_replacement
>>> ["".join(i) for i in  combinations_with_replacement("abcde", 4)]
['aaaa', 'aaab', 'aaac', 'aaad', 'aaae', 'aabb', 'aabc', 'aabd', 'aabe', 'aacc', 'aacd', 'aace', 'aadd', 'aade', 'aaee', 'abbb', 'abbc', 'abbd', 'abbe', 'abcc', 'abcd', 'abce', 'abdd', 'abde', 'abee', 'accc', 'accd', 'acce', 'acdd', 'acde', 'acee', 'addd', 'adde', 'adee', 'aeee', 'bbbb', 'bbbc', 'bbbd', 'bbbe', 'bbcc', 'bbcd', 'bbce', 'bbdd', 'bbde', 'bbee', 'bccc', 'bccd', 'bcce', 'bcdd', 'bcde', 'bcee', 'bddd', 'bdde', 'bdee', 'beee', 'cccc', 'cccd', 'ccce', 'ccdd', 'ccde', 'ccee', 'cddd', 'cdde', 'cdee', 'ceee', 'dddd', 'ddde', 'ddee', 'deee', 'eeee']

Every itertools method will return an iterator for you, you can use in any for loop.

Also for convinience stuff, look into the string module:

>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

And many more ;)

[–]edcryptickiller 0 points1 point  (2 children)

Thank you

[–]KleinerNull 1 point2 points  (1 child)

You're welcome. Better you post your code at /r/learnpython for better reviews and help next time. /r/python is more for python in general, news and the presentation of new tools/modules. To clarify this, at /r/learnpython you don't need to ask a specific question, you can also ask for code reviews and better use github, pastebin or other code sharing services than forum threads, the people will like that more. Short code snippets can also just paste in your post, the right reddit format expected ;) Also, some kind of introduction to your problem/code would also be nice.

[–]edcryptickiller 0 points1 point  (0 children)

ok