forked from Xuefeng-Zhu/Coderbyte-pythonSol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42-permutation.py
66 lines (27 loc) · 1.28 KB
/
42-permutation.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# def swap(nList,i):
# nList[i],nList[i-1] = nList[i-1], nList[i]
# for x in range(i, -1):
# if int(nList[i]) > int(nList[i+1]):
# nList[i],nList[i+1] = nList[i+1], nList[i]
# def PermutationStep(num):
# nList = list(str(num))
# for i in range(-1, -len(nList), -1):
# if int(nList[i]) > int(nList[i-1]):
# swap(nList,i)
# return "".join(nList)
# return -1
# # keep this function call here
# # to see how to enter arguments in Python scroll down
# print PermutationStep(raw_input())
# Using the Python language, have the function PermutationStep(num) take the num parameter being passed and return the next number greater than num using the same digits. For example: if num is 123 return 132, if it's 12453 return 12534. If a number has no greater permutations, return -1 (ie. 999).
# Use the Parameter Testing feature in the box below to test your code with different arguments.
def PermutationStep(num):
temp = str(num)
for i in range(len(temp)-1, 0, -1):
if temp[i] > temp[i-1]:
return temp[:i-1] + temp[i] + "".join(sorted(temp[i-1]+temp[i+1:]))
return -1
# keep this function call here
# to see how to enter arguments in Python scroll down
print PermutationStep(raw_input())
#