Pure bash (usando local
, più veloce, ma non può battere l'altra risposta utilizzando awk al di sotto, o Python sotto):
perm() {
local items="$1"
local out="$2"
local i
[[ "$items" == "" ]] && echo "$out" && return
for ((i=0; i<${#items}; i++)) ; do
perm "${items:0:i}${items:i+1}" "$out${items:i:1}"
done
}
while read line ; do perm $line ; done < File
Pure bash (usando subshell, molto più lento):
perm() {
items="$1"
out="$2"
[[ "$items" == "" ]] && echo "$out" && return
for ((i=0; i<${#items}; i++)) ; do
(perm "${items:0:i}${items:i+1}" "$out${items:i:1}")
done
}
while read line ; do perm $line ; done < File
Dal richiedente ha detto Perl va bene, penso che Python 2.6 +/3.X va bene, troppo:
python -c "from itertools import permutations as p ; print('\n'.join([''.join(item) for line in open('File') for item in p(line[:-1])]))"
Per Python 2.5 +/3.X:
#!/usr/bin/python2.5
# http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python/104436#104436
def all_perms(str):
if len(str) <=1:
yield str
else:
for perm in all_perms(str[1:]):
for i in range(len(perm)+1):
#nb str[0:1] works in both string and list contexts
yield perm[:i] + str[0:1] + perm[i:]
print('\n'.join([''.join(item) for line in open('File') for item in all_perms(line[:-1])]))
Sul mio computer utilizzando un file di prova più grande:
First Python code
Python 2.6: 0.038s
Python 3.1: 0.052s
Second Python code
Python 2.5/2.6: 0.055s
Python 3.1: 0.072s
awk: 0.332s
Bash (local): 2.058s
Bash (subshell): 22+s
Che cosa è esattamente lo scopo di fare questo? –
Mi piacciono le cose da fare ...: P – siliconpi