Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a continuous string?

I want to generate, in python (without a dictionary), a list of string from aaa-zzz and then output a txtfile such as this (note, the ... is short for the strings in between):

aaa
aab
aac
aad
...
aaz
aba
abb
abc
abd
...
aaz
...
zaa
...
zzy
zzz

The harder challenge is to genrate alternating (upper and lower) strings. How to generate these?

aaa
...
aaz
aaA
...
aaZ
aba
...
abz
...
abA
...
abZ
aBa
...
aBz
aBA
...
aBZ
...
zzz
zzA
...
...
zzZ
zAa
...
zAz
...
zZa
...
zZz
...
...
ZZZ

Just a bonus question, is there a way to not only include a-z, A-Z but also 0-9 in the generation??

like image 366
alvas Avatar asked Aug 21 '12 13:08

alvas


People also ask

How do I create a string with the same characters?

char Array With a for Loop. We can fill a fixed size char array with our desired character and convert that to a string: char[] charArray = new char[N]; for (int i = 0; i < N; i++) { charArray[i] = 'a'; } String newString = new String(charArray); assertEquals(EXPECTED_STRING, newString);

How to continue string on next line Python?

Use a backslash ( \ ) as a line continuation character In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.


1 Answers

import itertools, string

map(''.join, itertools.product(string.ascii_lowercase, repeat=3))
map(''.join, itertools.product(string.ascii_letters, repeat=3))
map(''.join, itertools.product(string.ascii_letters + string.digits, repeat=3))
like image 176
ecatmur Avatar answered Sep 20 '22 12:09

ecatmur