* Computer Science/Project Euler

Permutation

soicem 2016. 9. 30. 17:58

permutation(순열)

1
2
3
4
5
6
7
8
9
10
11
12
def permute(list):
    if not list:
        return [list]
    else:
        res = []
        for i in range(len(list)):
            rest = list[:i] + list[i+1:]
            for x in permute(rest):
                res.append(list[i:i+1]+x)
        return res
 
print permute(['a','b','c','d','e'])
cs
-> n개 줄세우기


1
2
3
4
5
6
7
8
9
10
11
12
13
def subset(list, size):
    if size == 0 or not list:
        return [list[:0]]
    else:
        result =[]
        for i in range(len(list)):
            pick = list[i:i+1]
            rest = list[:i] + list[i+1:]
            for x in subset(rest, size-1):
                result.append(pick+x)
        return result
 
print subset([1,2,3,4,5,6,7,8,9], 4)
cs
-> n개 중 k개 줄세우기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
target = ['3','4','4','5','5','6','7']
 
def subset(list, size):
    if size ==0 or not list:
        return [list[0]]
    else:
        result = set()
 
        for i in range(len(list)):
            pick = str(list[i:i+1][0])
            rest = list[:i] + list[i+1:]
            for x in subset(rest, size-1):
                result.add(pick+x) # set must require str type
        return result
 
print(subset(target, 4))
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from __future__ import generators
 
def xcombinations(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)):
            for cc in xcombinations(items[:i]+items[i+1:],n-1):
                yield [items[i]]+cc
 
def xuniqueCombinations(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)):
            for cc in xuniqueCombinations(items[i+1:],n-1):
                yield [items[i]]+cc
            
def xselections(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)):
            for ss in xselections(items, n-1):
                yield [items[i]]+ss
 
def xpermutations(items):
    return xcombinations(items, len(items))
 
if __name__=="__main__":
    print "Permutations of 'love'"
    for p in xpermutations(['l','o','v','e']): print ''.join(p)
 
    print
    print "Combinations of 2 letters from 'love'"
    for c in xcombinations(['l','o','v','e'],2): print ''.join(c)
 
    print
    print "Unique Combinations of 2 letters from 'love'"
    for uc in xuniqueCombinations(['l','o','v','e'],2): print ''.join(uc)
 
    print
    print "Selections of 2 letters from 'love'"
    for s in xselections(['l','o','v','e'],2): print ''.join(s)
 
    print
    print map(''.join, list(xpermutations('done')))
cs
-generator를 활용한 방법



출처 : http://biohackers.net/wiki/Permutation

'* Computer Science > Project Euler' 카테고리의 다른 글

project euler 37. problem  (0) 2016.10.13
project euler 43. problem  (0) 2016.10.05
project euler 42. problem  (0) 2016.09.23
project euler 40. problem  (0) 2016.09.22
project euler 39. problem  (0) 2016.09.20