【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 ...
随机推荐
- BSGS算法解析
前置芝士: 1.快速幂(用于求一个数的幂次方) 2.STL里的map(快速查找) 详解 BSGS 算法适用于解决高次同余方程 \(a^x\equiv b (mod p)\) 由费马小定理可得 x &l ...
- mac操作liunx
mkdir demo //创建一个文件夹 touch index.html // 创建一个html文件 rm rouch index.html //删除找个index.html文件 rmdir dem ...
- 第2天 | 12天搞定Python,运行环境(超详细步骤)
倘若有人告诉你,他在学习Python编程,却没有安装运行环境,那你赶紧叫他滚,并离他远点,因为他在欺骗你的感情. 没有安装运行环境,程序根本无法跑起来,对错不能知根知底,试问是在学编程,还是在跟空气对 ...
- Springboot集成logback,控制台日志打印两次,并且是不同的线程打印的
背景 在搭建一个新项目的时候,从公司别的项目搞了个logback-spring.xml的配置过来,修改一下启动项目的时候发现 所有的日志都输出了两次 并且来自于不同的线程,猜测是配置重复了,但是仔细检 ...
- git检出某文件的指定版本
比如当时文件所处的版本id是27e6266d86de3e6da6e1e7a8c43a8b51d6a87032 文件名是system/models/waimai/huodongdiscount.mdl. ...
- 如何从0到1的构建一款Java数据生成器-第二章
前提 在上一章我们提到了并且解决了几只拦路虎,承上启下,下面我们一起来实现一款数据生成器. 对外API /** * @description: 本地数据生成API * @author: peter * ...
- pytest文档51-内置fixture之cache使用
前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...
- mysql锁 转
参考文章:https://blog.csdn.net/puhaiyang/article/details/72284702 一.mysql锁的结构图 如上图所示,针对mysql的innodb存储引擎, ...
- CentOS8平台nginx日志的定时切分
一,编写bash脚本: [root@yjweb crontab]# vi split_nginx_logs.sh 代码: #!/bin/bash # 备份nginx的日志 # 昨天的日期 file_d ...
- selenium自动登陆
import osfrom selenium import webdriverimport time,jsonclass Cookie(object): def __init__(self,drive ...