【Kata Daily 190919】Sort Out The Men From Boys(排序)
题目:
Scenario
Now that the competition gets tough it will Sort out the men from the boys .
Men are the Even numbers and Boys are the odd

Task
Given an array/list [] of n integers , Separate The even numbers from the odds , or Separate the men from the boys

Notes
Return an array/list where Even numbers come first then odds
Since , Men are stronger than Boys , Then Even numbers in ascending order While odds in descending .
Array/list size is at least *4*** .
Array/list numbers could be a mixture of positives , negatives .
Have no fear , It is guaranteed that no Zeroes will exists .

Repetition of numbers in the array/list could occur , So (duplications are not included when separating).
Input >> Output Examples:
menFromBoys ({7, 3 , 14 , 17}) ==> return ({14, 17, 7, 3})
Explanation:
Since , { 14 } is the even number here , So it came first , then the odds in descending order {17 , 7 , 3} .
menFromBoys ({-94, -99 , -100 , -99 , -96 , -99 }) ==> return ({-100 , -96 , -94 , -99})
Explanation:
Since ,
{ -100, -96 , -94 }is the even numbers here , So it came first in ascending order , *then** *the odds in descending order{ -99 }Since , (Duplications are not included when separating) , then you can see only one (-99) was appeared in the final array/list .
menFromBoys ({49 , 818 , -282 , 900 , 928 , 281 , -282 , -1 }) ==> return ({-282 , 818 , 900 , 928 , 281 , 49 , -1})
Explanation:
Since ,
{-282 , 818 , 900 , 928 }is the even numbers here , So it came first in ascending order , then the odds in descending order{ 281 , 49 , -1 }Since , (Duplications are not included when separating) , then you can see only one (-282) was appeared in the final array/list .
题目很长,其实大意就是:找出list中的偶数和奇数的不重复组合,偶数进行升序,奇数进行降序排列
解题办法:
def men_from_boys(arr):
return sorted([i for i in set(arr) if i%2==0]) + sorted([i for i in set(arr) if i%2!=0], reverse=True)
其他解题思路:
def men_from_boys(arr):
men = []
boys = []
for i in sorted(set(arr)):
if i % 2 == 0:
men.append(i)
else:
boys.append(i)
return men + boys[::-1]
知识点:
1、去除重复项使用set()
2、排序使用sorted(list, reverse=False)
3、奇数偶数获取
【Kata Daily 190919】Sort Out The Men From Boys(排序)的更多相关文章
- sort如何按指定的列排序·百家电脑学院
sort如何按指定的列排序·百家电脑学院 sort如何按指定的(9php.com)列排序 0000 27189 41925425065f ...
- js 排序:sort()方法、冒泡排序、二分法排序。
js中的排序,这里介绍三种,sort()方法.冒泡排序.二分法排序. 1.sort方法 写法: 数组.sort(); 返回排好序的数组,如果数组里是数字,则由小到大,如果是字符串,就按照第一个字符的 ...
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【Kata Daily 190909】The Supermarket Queue(超市队列)
题目: There is a queue for the self-checkout tills at the supermarket. Your task is write a function t ...
- 【Kata Daily 191012】Find numbers which are divisible by given number
题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
- 【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)
题目: In this simple exercise, you will create a program that will take two lists of integers, a and b ...
- 【Kata Daily 190923】Odder Than the Rest(找出奇数)
题目: Create a method that takes an array/list as an input, and outputs the index at which the sole od ...
随机推荐
- java转python代码
今天发现一个好玩的工具:可以直接将java转成python 1. 安装工具(windows 环境下面) 先下载antlr: http://www.antlr3.org/download/antlr-3 ...
- weblogic 安装+部署(一)
昨天刚接触weblogic,在windows下搭建了一个weblogic,没什么技术,留个笔记. 1.首先要有jdk,添加环境变量这个没什么好说的. 2.下载weblogic:可以去官网下:http: ...
- Win32控制台、Win32项目、MFC项目、CLR控制台、CLR空项目、空项目区别
转载:https://blog.csdn.net/zfmss/article/details/79244696 1.Win32控制台 初始代码模版以main为程序入口,默认情况下,只链接C++运行时库 ...
- C++中cstring.h和string.h的区别
转载:https://blog.csdn.net/qian_chun_qiang/article/details/80648691 1.string与cstring有什么区别 <string&g ...
- Oracle Database XE 11gR2 SQL 命令行的显示调整
操作系统:Windows 10 x64 Oracle Database XE 11gR2 参考:在cmd命令行使用sqlplus时的页面显示问题 通过 cmd 命令行或运行 SQL 命令行查看一张表的 ...
- const、define 和 static 的区别
目录 define.const static define.const 在 C++ 中,const 和 define 都可以用来定义常量.但是这二者之间有很大的区别: define 的作用 用 def ...
- Rust之路(2)——数据类型 上篇
[未经书面同意,严禁转载] -- 2020-10-13 -- Rust是系统编程语言.什么意思呢?其主要领域是编写贴近操作系统的软件,文件操作.办公工具.网络系统,日常用的各种客户端.浏览器.记事本. ...
- 安装Node,创建vue项目,运行及打包
1.安装node js 下载地址:http://nodejs.cn/download/ 2.安装完成后运行Node.js command prompt(node -v查看安装版本) 3.安装npm(由 ...
- js实现自定义弹窗
众所周知,浏览器自带的原生弹窗很不美观,而且功能比较单一,绝大部分时候我们都会按照设计图自定义弹窗或者直接使用注入layer的弹窗等等.前段时间在慕课网上看到了一个自定义弹窗的实现,自己顺便就学习尝试 ...
- Docker安装以及运行第一个HelloWorld
Docker安装以及运行第一个HelloWorld