Absolute sort

Let's try some sorting. Here is an array with the specific rules.

The array (a tuple) has various numbers. You should sort it, but sort it by absolute value in ascending order. For example, the sequence (-20, -5, 10, 15) will be sorted like so: (-5, 10, 15, -20). Your function should return the sorted list or tuple.

Hints: This task can be easily solved using these functions: sorted andabs. You should try to use the key parameter for sorting.

Precondition: The numbers in the array are unique by their absolute values.

Input: An array of numbers , a tuple..

Output: The list or tuple (but not a generator) sorted by absolute values in ascending order.

题目大义:按绝对值排序

还是C语言的思路,sorted中传入cmp函数,如果x < y返回负值,x > y返回正值,x = y返回0

1 def cmp(numberA, numberB):
2 return abs(numberA) - abs(numberB)
3
4 def checkio(numbers_array):
5 return sorted(numbers_array, cmp)

不过既然用了Python如此高大上的语言,自然要显摆下,sorted还有一个参数为key,实际上sorted(number_array, key=abs)即可

Absolute sort的更多相关文章

  1. Check iO:初学Python

    The end of other For language training our Robots want to learn about suffixes. In this task, you ar ...

  2. 5-5 可视化库Seaborn-多变量分析绘图

      Parameters:¶ 参数 解释 变量 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整 ...

  3. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  4. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  6. 2016多校联合训练contest4 1012Bubble Sort

    Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  7. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  8. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  9. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

随机推荐

  1. Android android.support.v7.appcompat.R$styleable

    引入第三方jar或者library时比如自定义控件.突然报如下错误: 07-17 09:22:25.430: E/CrashHandler(14102): Caused by: android.vie ...

  2. 【转】彻底解决INSTALL_FAILED_UPDATE_INCOMPATIBLE的安装错误

    原文网址:http://bbs.9ria.com/thread-245162-1-1.html 利用adb shell进入系统,进入/data/app或者/data/data,删除跟你安装的apk同样 ...

  3. 【转】FAE及其发展前景

    原文网址:http://blog.sina.com.cn/s/blog_6e80c27b0100okd9.html FAE Field Application Engineer(现场应用工程师) ,其 ...

  4. Python安装MySQLdb并连接MySQL数据库

    当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...

  5. (转)iPhone 判断UITableView 滚动到底部

    UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate. ...

  6. float position的測试案例

    依据<a target=_blank href="http://blog.csdn.net/goodshot/article/details/44348525">htt ...

  7. Linux环境下Eclipse + Tomcat + MySQL 配置J2EE开发环境的方法

    1. 版本号信息 (1)CentOS 6.4发行版64位,uname -a 显演示样例如以下: Linux localhost.localdomain 3.11.6 #1 SMP Sat Nov 2 ...

  8. 打开Eclipse出现 parsesdkcontent failed 的解决办法

    出现这个问题是由于系统曾安装过SDK和AVD,所以需要删除.android和相应的workspace文件夹,然后进入我的电脑->高级系统设置->环境变量,在系统变量里,更新ANDROID_ ...

  9. Hacker(20)----手动修复Windows系统漏洞

    Win7系统中存在漏洞时,用户需要采用各种办法来修复系统中存在的漏洞,既可以使用Windows Update修复,也可使用360安全卫士来修复. 一.使用Windows Update修复系统漏洞 Wi ...

  10. C#基础:集合

        C#中的数组实现为 System.Array 类的实例,它们只是集合类(Collection Classes)中的一种类型.集合类一般用于处理对象列表,其功能比简单数组要多,功能大多是通过实现 ...