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. BZOJ2393: Cirno的完美算数教室

    2393: Cirno的完美算数教室 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 83[Submit][Status] D ...

  2. SQL Server 通过一个表和另一个表联合 批量更新这个表的字段

    UPDATE OutPzPersonSet SET cPerson = a.AAA --SELECT * FROM OutPzPersonSet d INNER JOIN AAAA a ON d.cz ...

  3. Delphi流的操作 转

    一.流的概念 流简单说是建立在面向对象基础上的一种抽象的处理数据的工具,它定义了一些处理数据的基本操作,如读取数据,写入数据等,程序员只需掌握对流进行操作,而不用关心流的另一头数据的真正流向.其实,流 ...

  4. Hive 6、Hive DML(Data Manipulation Language)

    DML主要是对Hive 表中的数据进行操作的(增 删 改),但是由于Hadoop的特性,所以单条的修改.删除,其性能会非常的低所以不支持进行级操作: 主要说明一下最常用的批量插入数据较为常用的方法: ...

  5. MVC 控制器详解

    Controller: Controllers 文件夹包含负责处理用户输入和响应的控制器类. MVC 要求所有控制器的名称必须以 "Controller" 结尾. 控制器的职责: ...

  6. struts配置,略记

    <!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener ...

  7. SegmentFault 巨献 1024 程序猿游戏「红岸的呼唤」第三天任务攻略

    第三关也不是一般的难呐,那么继续写一下解题过程(第四关会是什么样呢?). 高速传送门:http://segmentfault.com/game/3 在用我想到的方法(booth算法.矩阵变换.各种CP ...

  8. JavaLearning:JAVA IO Piped流

    package org.fun.io; import java.io.IOException; import java.io.PipedInputStream; import java.io.Pipe ...

  9. Android开源代码解读のOnScrollListener实现ListView滚屏时不加载数据

    使用ListView过程中,如果滚动加载数据的操作比较费时,很容易在滚屏时出现屏幕卡住的现象,一个解决的办法就是不要在滚动时加载数据,而是等到滚动停止后再进行数据的加载.这同样要实现OnScrollL ...

  10. ORACLE查看数据文件-控制文件-日志文件-表空间信息

    1.查看当前数据库中的所有用户:select username from dba_users; 2.查看当前会话登录的用户:show user或select username from user_us ...