-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswapfrenzy.py
68 lines (60 loc) · 2.68 KB
/
swapfrenzy.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
numberToSwap, timesToSwap = map(int, input().split())
swapped = [0]
def findHighestNumIndexFromList(numList, alreadySwapped):
for i in range(9, 0, - 1):
index = len(numList) - 1
for j in numList[::-1]:
# print("The current number in range is {}, The current number in numlist is {}".format(i, j))
if int(j) == i and index not in alreadySwapped:
return index
else:
index -= 1
return findLowestNumIndexFromList(numList)
def findLowestNumIndexFromList(numList):
for i in range(0, 9, 1):
index = len(numList) - 1
for j in numList[::-1]:
# print("The current number in range is {}, The current number in numlist is {}".format(i, j))
if int(j) == i and numList.index(j):
# print("These are equal")
return index
else:
# print("These are not equal")
index -= 1
numbersToSwap = list(str(numberToSwap))
swapsLeft = timesToSwap
for i in range(timesToSwap):
highNum = findHighestNumIndexFromList(numbersToSwap, swapped)
#print("The highest number is " + numbersToSwap[highNum])
Swaped = False
while not Swaped:
try:
for j in range(len(numbersToSwap)):
if j > highNum:
if int(numbersToSwap[highNum]) < int(numbersToSwap[j]):
numbersToSwap[highNum], numbersToSwap[j] = numbersToSwap[j], numbersToSwap[highNum]
swapsLeft -= 1
swapped.append(j)
Swaped = True
break
elif j < highNum:
if int(numbersToSwap[highNum]) > int(numbersToSwap[j]):
numbersToSwap[highNum], numbersToSwap[j] = numbersToSwap[j], numbersToSwap[highNum]
swapsLeft -= 1
swapped.append(j)
Swaped = True
break
# print("Swapped {} and {}".format(numbersToSwap[highNum], numbersToSwap[j]))
# print("New list is {}".format(numbersToSwap))
else:
swapped.append(j)
highNum = findLowestNumIndexFromList(numbersToSwap)
continue
except TypeError:
pass
#print("Swaps left: " + str(swapsLeft))
if len(numbersToSwap) == 2:
numbersToSwap[1], numbersToSwap[0] = numbersToSwap[0], numbersToSwap[1]
if int(numbersToSwap[0]) == 0:
numbersToSwap.remove("0")
print("".join(numbersToSwap))