-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp24.py
38 lines (34 loc) · 836 Bytes
/
p24.py
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
#!/usr/bin/python
from itertools import permutations
def next_perm(seq):
k = None
l = None
for i in xrange(len(seq) -1):
if seq[i] < seq[i+1]:
k = i
if k:
l = k + 1
for i in xrange(k+1, len(seq) -1):
if seq[k] < seq[i]:
l = i
buf = seq[k]
seq[k] = seq[l]
seq[l] = buf
bufs = seq[k+1:]
bufs.reverse()
#print k, l, seq, bufs, seq[:k+1] + bufs
return seq[:k+1] + bufs
else:
return seq
if __name__ == "__main__":
perm = permutations(range(10))
limit = 1e6
cont = 1
try:
while 1:
buf = perm.next()
if cont == limit:
print ''.join(map(str, buf))
cont += 1
except StopIteration:
print cont