Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Robot Sort by Mahoter
def swapsort(array):
tab = list(array)
tab2 = list(array)
tab2.sort()
res = ''
a = 0
while a == 0:
dig = 0
while dig < len(tab)-1:
if tab[dig] > tab[dig+1]:
temp = tab[dig]
tab[dig] = tab[dig+1]
tab[dig+1] = temp
res += ','+str(dig)+str(dig+1)
dig += 1
a = 1
if tab != tab2:
a = 0
res = res[1:]
return res
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def check_solution(f, indata):
result = f(indata)
array = list(indata[:])
la = len(array)
if not isinstance(result, str):
print("The result should be a string")
return False
actions = result.split(",") if result else []
for act in actions:
if len(act) != 2 or not act.isdigit():
print("The wrong action: {}".format(act))
return False
i, j = int(act[0]), int(act[1])
if i >= la or j >= la:
print("Index error: {}".format(act))
return False
if abs(i - j) != 1:
print("The wrong action: {}".format(act))
return False
array[i], array[j] = array[j], array[i]
if len(actions) > (la * (la - 1)) // 2:
print("Too many actions. BOOM!")
return False
if array != sorted(indata):
print("The array is not sorted. BOOM!")
return False
return True
assert check_solution(swapsort, (6, 4, 2)), "Reverse simple"
assert check_solution(swapsort, (1, 2, 3, 4, 5)), "All right!"
assert check_solution(swapsort, (1, 2, 3, 5, 3)), "One move"
Jan. 18, 2016