-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_024.py
More file actions
43 lines (40 loc) · 1.56 KB
/
Problem_024.py
File metadata and controls
43 lines (40 loc) · 1.56 KB
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
#!/usr/bin/python2.7
# Problem_024.py
import sys
def next_lexical_permutation(number_list):
number_list_len = len(number_list)
# Locate pivot's index.
pivot_index = None
for i in range(-2, -number_list_len - 1, -1):
if number_list[i] < number_list[i + 1]:
pivot_index = i
break
if pivot_index == None:
s = reduce(lambda x,y: x+str(y), number_list, '')
number = int(s)
print("This is the last permutation of the number: %d." % number)
return
# Locate the pivot's successor.
pivot_successor_index = None
pivot_successor = None
for i in range(-1, pivot_index - 1, -1):
if number_list[i] > number_list[pivot_index] and \
(number_list[i] < pivot_successor or pivot_successor == None):
pivot_successor_index = i
pivot_successor = number_list[i]
number_list[pivot_successor_index] = number_list[pivot_index]
number_list[pivot_index] = pivot_successor
number_list[pivot_index + 1:] = sorted(number_list[pivot_index +1:])
return number_list
if __name__ == '__main__':
number_str = raw_input("Input the number to permutate: ")
n_permutations = input("Input the number of permutations to execute: ")
number_list = [int(i) for i in number_str]
permute_list = number_list
for i in range(1, n_permutations):
permute_list = next_lexical_permutation(permute_list)
if permute_list == None:
sys.exit(0)
s = reduce(lambda x,y: x+str(y), permute_list, '')
number = int(s)
print(number)