题目:

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、奇数偶数获取

最新文章

  1. SQL Server ---(CDC)监控表数据(转译)
  2. 使用socket方式连接Nginx优化php-fpm性能
  3. 微软今日发布汇总:VS2015, .NET 4.6, C# 6.0, F# 4.0等重量级产品正式上线
  4. 常用js代码
  5. Java多线程之join
  6. ionic中input框禁止输入问题
  7. 简单C# 验证类
  8. [置顶] 有关ListIterator接口的add与remove方法探究
  9. clear伪类使用
  10. UVa 1292 - Strategic game (树形dp)
  11. 【甘道夫】HBase开发环境搭建过程中可能遇到的异常:No FileSystem for scheme: hdfs
  12. hdu 4542 数论 + 约数个数相关 腾讯编程马拉松复赛
  13. Python学习--20 Web开发
  14. Spring 切入点配置
  15. https遇到自签名证书/信任证书
  16. loj #116. 有源汇有上下界最大流
  17. 多线程——继承Thread类实现一个多线程
  18. spring boot(二)web综合开发
  19. 使用Akka构建集群(一)
  20. Mybatis 自动生成代码,数据库postgresql

热门文章

  1. 07 C语言常量
  2. python 给IDLE添加行号
  3. 【暂咕咕咕】SuffixTree
  4. 【题解】CF1324F
  5. ESP8266 玩板记
  6. WebStrom配置TypeScript开发环境
  7. 关于android和Linux的一些问题
  8. JProfiler 教程 使用说明
  9. cmake引入三方库
  10. html学习(3)