Task:
Return an array or a tuple or a string depending on the language (see "Sample Tests") with
-
- the smallest number you got
-
- the index
iof the digitdyou took,ias small as possible
- the index
-
- the index
j(as small as possible) where you insert this digitdto have the smallest number.
- the index
Examples:
smallest(261235) --> [126235, 2, 0] or (126235, 2, 0) or "126235, 2, 0"
126235 is the smallest number gotten by taking 1 at index 2 and putting it at index 0
smallest(209917) --> [29917, 0, 1] or ...
[29917, 1, 0] could be a solution too but index `i` in [29917, 1, 0] is greater than
index `i` in [29917, 0, 1].
29917 is the smallest number gotten by taking 2 at index 0 and putting it at index 1 which gave 029917 which is the number 29917.
smallest(1000000) --> [1, 0, 6] or ...
Solution:
def smallest(n):
m = n
s = str(n)
rg = range(len(s))
for i in rg:
for j in rg:
li = list(s)
d = li.pop(i)
li.insert(j, d)
z = int(''.join(li))
if z < m:
m = z
x = i
y = j
return [m, x, y]
print(smallest(261235))
Find the smallest Solutions | Codewars
No comments :
Post a Comment