【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 ...
随机推荐
- R语言与机器学习学习笔记
人工神经网络(ANN),简称神经网络,是一种模仿生物神经网络的结构和功能的数学模型或计算模型.神经网络由大量的人工神经元联结进行计算.大多数情况下人工神经网络能在外界信息的基础上改变内部结构,是一种自 ...
- 更改Nginx网站根目录以及导致的403 forbidden问题解决
版权声明:本文为博主原创文章,未经博主允许不得转载. 一.更改根目录 Nginx默认网站根目录为/usr/local/nginx/html,要将它改成/home/fuxiao/www 更改方法: ...
- Oracle_lhr_CentOS 7.3 ECS上搭建RAC 18c+单实例DG+EMCC+DG的FSFO快速故障转移配置
Oracle_lhr_CentOS 7.3 ECS上搭建RAC 18c+单实例DG+EMCC+DG的FSFO快速故障转移配置 [大型连续免费公开课]Oracle 18c rac+dg+13.3的em ...
- Redis 数据结构的实现
Redis 数据结构的实现 先看个对照关系: Redis数据结构 实现一 实现二 string 整数(如果value能够表示为整数) 字符串 hash 压缩列表(只包含少量键值对, 并且每个键值对的键 ...
- 【转】Servlet 生命周期、工作原理
Servlet 生命周期:Servlet 加载--->实例化--->服务--->销毁. init():在Servlet的生命周期中,仅执行一次init()方法.它是在服务器装入Ser ...
- 微信小程序中同步 异步的使用
https://www.jianshu.com/p/e92c7495da76 微信小程序中使用Promise进行异步流程处理 https://www.cnblogs.com/cckui/p/102 ...
- 从头配置,开发 cocos2dx 3.14.1
要使用cocos2dx写一个麻将测试工具. 配置流程如下: 1. 下载安装最新版本的VS(VS2017): https://www.visualstudio.com/zh-hans/downloads ...
- 130、 Android OkHttp完全解析(转载)
完全解析:http://blog.csdn.net/lmj623565791/article/details/47911083 从原理角度解析http文件上传 http://blog.csdn.net ...
- 用SUMIF对超15位的代码进行条件求和,出错了,原因是....
用SUMIF对超15位的代码进行条件求和,出错了,原因是.... 2017-10-29 23:01 一.问题 有读者朋友问: 用SUMIF进行条件求和时,如果统计的条件是超15位的代码,就会出错,比如 ...
- kafka---->kafka stream的使用(一)
kafka stream的简单使用,这里是官方文档上面的例子. kafka的简单使用 一.启动Kafka server huhx@gohuhx:~/server/kafka_2.11-1.1.0$ b ...