-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollowing_integer.py
More file actions
35 lines (29 loc) · 829 Bytes
/
following_integer.py
File metadata and controls
35 lines (29 loc) · 829 Bytes
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
# Created by JiaYi Lim on 13/1/2014
import sys
def permute(string, current=''):
if len(string) == 1:
return set(string)
perms = set()
for i, c in enumerate(string):
variants = permute(string[:i]+string[i+1:], current+c)
for v in variants:
perms.add(c+v)
return perms
def find_next(string, num=''):
if not num:
num = int(string)
perms = sorted([ int(p) for p in permute(string) ])
for i in range(len(perms)-1):
if perms[i] == num:
return perms[i+1]
# num is the last permutation
string += '0'
return find_next(string, num)
def start():
filepath = sys.argv[1]
with open(filepath) as f:
for line in f:
line = line.strip()
print find_next(line)
if __name__ == '__main__':
start()