【Checkio Exercise】Robot Sort
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".
【Checkio Exercise】Robot Sort的更多相关文章
- 【Checkio Exercise】Probably Dice
题目: Probably Dice Battle is full of randomnesses. You should observe randomness in a controlled sett ...
- 【Checkio Exercise】Three Point Circle
计算三角形外接圆的函数: Three Point Circle If we want to build new silos, then we need to make more formal and ...
- 【hdu 1890】Robotic Sort
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给你n个数字; i从1到n; 每次让你把第i小的数和数组的第i个元素之间这段区间内 ...
- 【Codeforces 922D】Robot Vacuum Cleaner
[链接] 我是链接,点我呀:) [题意] 让你把n个字符串重新排序,然后按顺序连接在一起 使得这个组成的字符串的"sh"子序列最多 [题解] /* * 假设A的情况好于B * 也就 ...
- 【39.77%】【codeforces 724B】Batch Sort
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【转帖】linux sort,uniq,cut,wc,tr,xargs命令详解
linux sort,uniq,cut,wc,tr,xargs命令详解 http://embeddedlinux.org.cn/emb-linux/entry-level/201607/21-5550 ...
- 【Codeforces 340D】Bubble Sort Graph
[链接] 我是链接,点我呀:) [题意] 让你根据冒泡排序的规则 建立一张图 问你这张图的最大独立子集的大小 [题解] 考虑a[i]会和哪些点连边? 必然是在a[i]左边且比它大的数字以及在a[i]右 ...
- 【BZOJ-1552&3506】robotic sort&排序机械臂 Splay
1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 806 Solved: 329[Submit][ ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
随机推荐
- clientHeight scrollHeight offsetHeight
<div style="height:200px;padding:10px;border:1px solid green;"></div> 对于上面的di ...
- 【LInux】统计某文件夹下目录的个数
统计当前文件夹下文件的个数,包括子文件夹里的ls -lR|grep "^-"|wc -l 统计文件夹下目录的个数,包括子文件夹里的ls -lR|grep "^d" ...
- Rafy环境配置
如果现在项目已经创建好啦,要使用Rafy框架进行存储,这里我简单的总结下配置的环境步骤: 一.添加引用Rafy的SDK,如下几个; 二.新建文件夹Entities 在此文件夹下创建rafy实体以及仓库 ...
- 121、Data Binding(数据绑定)(转载)
http://www.jianshu.com/p/b1df61a4df77 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/060 ...
- Oracle数据库自带表空间
需求:需要整理现场用户创建的表空间以及其存储数据,进行规范化管理.在整理用户现场建立的表空间时,需要排除掉非用户创建的表空间,所有首先需要那些表空间是用户创建的,那些是Oracle自带的. 本机测试建 ...
- Java泛型 T.class的获取
public interface BaseDao<T> { T get(String id); } import java.lang.reflect.ParameterizedType; ...
- java学习之路--String类的基本方法
String类常见的功能 获取 1.1 字符串中包含的字符数,也就是获取字符串的长度:int length(); 1.2 根据位置获取某个位置上的字符:char charAt(int index) 1 ...
- nodejs(二)浏览器与服务器连接初探
- JQuery限制文本框只能输入数字和小数点的方法
<input type="text" class="txt NumText" Width="100px" /> $(func ...
- ajax获取后台数据渲染(整片文章不分段落)解决方案,要使用htmL方式输出
方案一:使用 HTML pre tag<div class="content"><pre> {{ text_data }}</pre></ ...