I am new to Python and want to have a "pythonic" coding style from the very beginning. Following is a piece of codes I wrote to generate all combinations and permutations of a set of symbols, e.g. abc
or 123
. I mainly seek for suggestions on code style but comments on other aspects of codes are also welcome.
import random as rd
import math as m
def permutationByRecursion(input, s, li):
if len(s) == len(input):
li.append(s)
for i in range(len(input)):
if input[i] not in s:
s+=input[i]
permutationByRecursion(input, s, li)
s=s[0:-1]
def combinationByRecursion(input, s, idx, li):
for i in range(idx, len(input)):
s+=input[i]
li.append(s)
print s, idx
combinationByRecursion(input, s, i+1, li)
s=s[0:-1]
def permutationByIteration(input):
level=[input[0]]
for i in range(1,len(input)):
nList=[]
for item in level:
nList.append(item+input[i])
for j in range(len(item)):
nList.append(item[0:j]+input[i]+item[j:])
level=nList
return nList
def combinationByIteration(input):
level=['']
for i in range(len(input)):
nList=[]
for item in level:
nList.append(item+input[i])
level+=nList
return level[1:]
res=[]
#permutationByRecursion('abc', '', res)
combinationByRecursion('abc', '', 0, res)
#res=permutationByIteration('12345')
print res
print combinationByIteration('abc')