【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知识系统回顾整理01基础01第一个程序07Eclipse使用----找不到类如何解决?
一.现象 有时候会碰到如图所示的问题,分明有Hello这个类,并且也有主方法,可是运行就会出现找不到或者无法加载类Hello,或者Class Not Found 异常. 出现这个状况,有多种原因造成, ...
- Java知识系统回顾整理01基础03变量05变量命名规则
一.命名规则 变量命名只能使用字母 .数字. $. _ 变量第一个字符 只能使用: 字母. $. _ 变量第一个字符 不能使用数字 注:_ 是下划线,不是-减号或者-- 破折号 int a= 5; i ...
- C++(VS2015)模板显式特化之template语法深入理解
首先说下遇到的情况: 这里在vc++6.0上建立了一个自定义模板类,再去覆盖这个类,分别使用部分覆盖,整体覆盖 但在vs2015上去整体覆盖类会报错. 错误如下: 错误原因:个人感觉是新版本的vs更接 ...
- LiteOS-任务篇-源码分析-任务调度函数
目录 前言 笔录草稿 核心源码分析 osTaskSchedule函数源码分析 osPendSV函数源码分析 TaskSwitch函数源码分析 调度上层源码分析 osSchedule函数源码分析 LOS ...
- Python基础笔记1-Python读写yaml文件(使用PyYAML库)
最近在搭建自动化测试项目过程中经常遇到yaml文件的读写,为了方便后续使用,决定记下笔记. 一,YAML 简介 YAML,Yet Another Markup Language的简写,通常用来编写项目 ...
- English 介词
English 介词 Create Time : 2019-06-27 表示时间的介词称为时间介词.表示时间的介词有:at, on, in, before, after等. 一.at, on和in ① ...
- 快速解读linq语法
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- C++里面类和对象是什么意思?
本文章向大家介绍C++类和对象到底是什么意思?,主要包括C++类和对象到底是什么意思?使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下. C++ 是一门 ...
- 最近集训的图论(思路+实现)题目汇总(内容包含tarjan、分层图、拓扑、差分、奇怪的最短路):
(集训模拟赛2)抢掠计划(tarjan强) 题目:给你n个点,m条边的图,每个点有点权,有一些点是"酒吧"点,终点只能在"酒吧",起点给定,路可以重复经过,但点 ...
- 灵魂拷问:你真的理解System.out.println()执行原理吗?
原创/朱季谦 灵魂拷问,这位独秀同学,你会这道题吗? 请说说,"System.out.println()"原理...... 这应该是刚开始学习Java时用到最多一段代码,迄今为止 ...