题目:

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(排序)的更多相关文章

  1. sort如何按指定的列排序·百家电脑学院

    sort如何按指定的列排序·百家电脑学院 sort如何按指定的(9php.com)列排序 0000            27189           41925425065f           ...

  2. js 排序:sort()方法、冒泡排序、二分法排序。

    js中的排序,这里介绍三种,sort()方法.冒泡排序.二分法排序. 1.sort方法 写法:  数组.sort(); 返回排好序的数组,如果数组里是数字,则由小到大,如果是字符串,就按照第一个字符的 ...

  3. 【Kata Daily 190929】Password Hashes(密码哈希)

    题目: When you sign up for an account somewhere, some websites do not actually store your password in ...

  4. 【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 ...

  5. 【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 ...

  6. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  7. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. #ifdef _DEBUG/ #define new DEBUG_NEW/ #endif的作用

    转载:https://blog.csdn.net/minghui_/article/details/80748142 转自:#ifdef _DEBUG #define new DEBUG_NEW #e ...

  2. 数字PLL,什么是数字PLL

    来源:http://www.elecfans.com/baike/bandaoti/bandaotiqijian/20100323203306.html 数字PLL,什么是数字PLL 数字PLL PL ...

  3. aptitude命令

    命令 作用 aptitude update 更新可用的包列表 aptitude upgrade 升级可用的包 aptitude dist-upgrade 将系统升级到新的发行版 aptitude in ...

  4. MySql 关闭 bin 和 log 日志

    mysql 的 bin 和 .log 日志文件会非常占用磁盘空间和 IO,修改 mysql 配置文件可以关闭这两种日志的记录. 关闭 bin 日志,将下面三项配置注释掉: #log_bin = mys ...

  5. 手把手教你使用 Prometheus 监控 JVM

    概述 当你的 Java 业务容器化上 K8S 后,如果对其进行监控呢?Prometheus 社区开发了 JMX Exporter 来导出 JVM 的监控指标,以便使用 Prometheus 来采集监控 ...

  6. 实验四  用PS制作横幅广告

    实验四  用PS制作横幅广告 [实验目的] ⑴.了解广告设计应用 ⑵.学会利用PS制作简单的横幅广告 [实验条件] ⑴.个人计算机一台 ⑵.个人计算机中预装Windows7操作系统和浏览器及Photo ...

  7. 【C++学习笔记】C++经典十二道笔试题!你能做出几道?

    1. 运行下面的C++代码,得到的结果是什么? #include "stdafx.h" #include<iostream> using namespace std; ...

  8. go内建方法 make方法

    package main import "fmt" func main() { // make函数 makeSlice() // 创建切片 makeMap() // 创建集合 ma ...

  9. 数据库备份作业的T-SQL语句

    1.关于大容量数据导入导出的一些方法SQL SERVER提供多种工具用于各种数据源的数据导入导出,这些数据源包括本文文件.ODBC数据源.OLE DB数据源.ASCII文本文件和EXCEL电子表格.2 ...

  10. proto文件生成对应的js和ts文件

    安装protobuf 先要安装node.js,然后用npm安装protobuf npm install -g protobufjs 生成js文件 单个文件 pbjs -t static-module ...