Robot Sort
All of the refined ingots should be sorted by size in each lot while passing by on a conveyor. Because the conveyor is already running, our robots needs to quickly swap neighboring ingots.
You are given the size and initial order of the ingots as an array of numbers. Indexes are their position, values are their sizes. You should order this array from the smallest to the largest in size.
For each action a Robot can swap two neighboring elements. Each action should be represented as a string with two digits - indexes of the swapped elements (ex, "01" - swap 0th and 1st ingots). The result should be represented as a string that contains the sequence of actions separated by commas. If the array does not require sorting, then return an empty string.
And you can swap only N*(N-1)/2 times, where N - is a quantity of ingots.
Input: An array as a tuple of integers.
Output: The sequence of actions as a string.
Example:
Precondition:
1 ≤ |array| ≤ 10
翻译:需要对一个数组进行两两排序,输出的是排序的方法
def swap_sort(array):
a = []
array1 = list(array)
for j in range(len(array1) - 1):
for i in range(len(array1)):
if i + 1 < len(array1):
x = array1[i]
y = array1[i+1]
if x > y:
array1.insert(i+2, x)
array1.pop(i)
b = str(i)+str(i+1)
a.append(b)
c = ",".join(a)
if a == []:
return ""
else:
return c
How it is used:
This mission will show you how to work the simplest sorting algorithms. It also models one of the game mechanics in the classic puzzle game "Tetris Attack".