forked from michelegorgini/excersises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Next bigger number with the same digits.py
160 lines (132 loc) · 3.63 KB
/
Next bigger number with the same digits.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#Exercise - Next bigger number with the same digits
#def next_bigger(n):
#your code here
# n = 2071
# print(n)
# s = str(n)
# ListDigit_right = []
# bigger = n
# Ind_FromRight = next(( x+1) for x,v in enumerate(s[::-1]) if v > s[x+1])
# print(Ind_FromRight)
# ind_Fromleft = ((len(s)-1)- Ind_FromRight)
# new_s = s[ind_Fromleft:]
# print(s[ind_Fromleft])
# print(new_s)
# Next_DigitBig = next((v) for x,v in enumerate(new_s[::-1]) if v > new_s[x+1] and v > s[ind_Fromleft])
# for i in new_s:
# ListDigit_right.append(i)
# ListDigit_right.remove(Next_DigitBig)
# ListDigit_right = sorted(ListDigit_right)
# Next_DigitBig +=''.join(map(str, ListDigit_right))
# num = int(s[:(ind_Fromleft)] + Next_DigitBig )
# if (num > bigger):
# bigger = num
# else:
# bigger = -1
# print(bigger)
# def next_bigger(n):
# print(n)
# s = str(n)
# ListDigit_right = []
# bigger = n
# Ind_FromRight = next(( x+1) for x,v in enumerate(s[::-1]) if v > s[x+1])
# ind_Fromleft = ((len(s)-1)- Ind_FromRight)
# new_s = s[ind_Fromleft:]
# Next_DigitBig = next((v) for x,v in enumerate(new_s[::-1]) if v > new_s[x+1] and v > s[ind_Fromleft])
# for i in new_s:
# ListDigit_right.append(i)
# ListDigit_right.remove(Next_DigitBig)
# ListDigit_right = sorted(ListDigit_right)
# Next_DigitBig +=''.join(map(str, ListDigit_right))
# num = int(s[:(ind_Fromleft)] + Next_DigitBig )
# if (num > bigger):
# bigger = num
# else:
# bigger = -1
# return(bigger)
# 59884848459853
# 59884848485953
# 59884848483559
# 59884848485953
# 7385661
# It should work for random inputs too: 7386561 should equal 7386156
# n = 12
# def next_bigger(n):
# print(n)
# s = str(n)
# len_s = len(s)
# ListBigger = []
# bigger = n
# for i in range(len(s)):
# removed = s[:i] + s[i+1:]
# for j in range(len(removed)+1):
# num = int(removed[:j] + s[i] + removed[j:])
# if (num > bigger):
# ListBigger.append(num)
# if ListBigger == []:
# bigger = -1
# else:
# tmp_bigger = min(ListBigger)
# str_bigger = str(tmp_bigger)
# print(str_bigger)
# print("/"*20)
# for i in range(len(s)):
# if s[i] != str_bigger[i]:
# Difference = str_bigger[i:]
# break
# else:
# continue
# if (tmp_bigger - n) > 9:
# lastDigit = ''.join(sorted(Difference[1:]))
# bigger = int(str_bigger[:i] + lastDigit)
# else:
# bigger = tmp_bigger
# return(bigger)
#
# result =(next_bigger(n))
# print(result)
n = 9
print(n)
s = str(n)
len_s = len(s)
ListBigger = []
bigger = n
for i in range(len(s)):
removed = s[:i] + s[i+1:]
for j in range(len(removed)+1):
num = int(removed[:j] + s[i] + removed[j:])
if (num > bigger):
ListBigger.append(num)
if ListBigger == []:
bigger = -1
else:
tmp_bigger = min(ListBigger)
str_bigger = str(tmp_bigger)
print(str_bigger)
print("/"*20)
for i in range(len(s)):
if s[i] != str_bigger[i]:
Difference = str_bigger[i:]
break
else:
continue
lastDigit = ''.join(sorted(Difference[1:]))
print(lastDigit)
bigger = int(str_bigger[:i+1] + lastDigit)
print(bigger)
print()
# 2175057886
# 2175058768
# 2175058678
#2175058768 should equal 2175058678
# You have to create a function that takes a positive integer number and returns the next bigger number formed by
# the same digits:
#
# 12 ==> 21
# 513 ==> 531
# 2017 ==> 2071
# If no bigger number can be composed using those digits, return -1:
#
# 9 ==> -1
# 111 ==> -1
# 531 ==> -1