Skip to main content
added 108 characters in body
Source Link
flornquake
  • 1.6k
  • 11
  • 26

Python, 9797 94

from itertools import*
s,n=input()
L=[]
i=0L=t=[]
exec"i+=1;L+=mapexec"t=t+[s];L+=map(''.join,product(*[s]*i*t));"*n
print L

t=t+[s] can't be shortened to t+=[s] because L and t would be pointing to the same list.

Input: 'ab', 3

Output:

['a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bb
a', 'bbb']

Python, 97

from itertools import*
s,n=input()
L=[]
i=0
exec"i+=1;L+=map(''.join,product(*[s]*i));"*n
print L

Input: 'ab', 3

Output:

['a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bb
a', 'bbb']

Python, 97 94

from itertools import*
s,n=input()
L=t=[]
exec"t=t+[s];L+=map(''.join,product(*t));"*n
print L

t=t+[s] can't be shortened to t+=[s] because L and t would be pointing to the same list.

Input: 'ab', 3

Output:

['a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bb
a', 'bbb']
Source Link
flornquake
  • 1.6k
  • 11
  • 26

Python, 97

from itertools import*
s,n=input()
L=[]
i=0
exec"i+=1;L+=map(''.join,product(*[s]*i));"*n
print L

Input: 'ab', 3

Output:

['a', 'b', 'aa', 'ab', 'ba', 'bb', 'aaa', 'aab', 'aba', 'abb', 'baa', 'bab', 'bb
a', 'bbb']