【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 ...
随机推荐
- awd入门教程
(转自:awk入门教程 - 阮一峰) 以下为正文 ———————————————————— awk是处理文本文件的一个应用程序,几乎所有 Linux 系统都自带这个程序. 它依次处理文件的每一行,并读 ...
- idea创建maven SSM项目
maven配置 ➜ ~ cd /Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/conf ➜ conf vim set ...
- IntelliJ IDEA License Server 安装使用 Mac篇
一.下载 IntelliJ IDEA 是Java开发利器,用社区版不爽,干催就用旗舰版,这个是收费的,需要licence. IntelliJ IDEA下载地址:https://www.jetbrai ...
- MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report e
早上来到公司,线上的项目报错: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionExcepti ...
- js - 伪数组转化为数组的几种方法整理(更新中...)
伪数组:无法调用数组的方法,但是有length属性,又可以索引获取内部项的数据结构. 比如:arguments.getElementsByTagName等一系列dom获取的NodeList对象,他们 ...
- STL之template类模板
#include <iostream> using namespace std; template<class T>//类模板 class Person{ public://构 ...
- SQL语句精简版
select US.QQ,US.tel,US.username,SC.EnglishScore,SC.MathScorefrom Userinfor US right join Score SC on ...
- Oracle课程档案。第十一天
读一致性:oracle通过多版本与闪回机制保证读一致性.保证从某个时间点开始查询是一致的.在Oracle中主要通过SCN版本号来控制系统修改的版本,典型的例子是我们可以通过在同一个查询中得到同一个对象 ...
- HR在ERP实施过程中的作用
ERP实施涉及到部门职责.个人职责的改变,在实施过程中HR有着不可估量的作用: 实施制度设计 包括如何对实施人员的激励,对实施人员进行合理的岗位职责调整: 某些企业在实施ERP时自项目经理到关键用户都 ...
- Codeforces 659 - A/B/C/D/E/F/G - (Undone)
链接:https://codeforces.com/contest/659 A - Round House - [取模] AC代码: #include<bits/stdc++.h> usi ...