CheckIO 中位数
来源:互联网 发布:淘宝外围女孙佑怡 编辑:程序博客网 时间:2024/06/07 10:28
中位数是一个可将数值集合划分为相等的上下两部分的一个数值。如果列表数据的个数是奇数,则列表中间那个数据就是列表数据的中位数;如果列表数据的个数是偶数,则列表中间那2个数据的算术平均值就是列表数据的中位数。在这个任务里,你将得到一个含有自然数的非空数组(X)。你必须把它分成上下两部分,找到中位数。输入: 一个作为数组的整数(int)列表(list)的。输出: 数组的中位数(int, float).范例:checkio([1, 2, 3, 4, 5]) == 3
checkio([3, 1, 2, 5, 3]) == 3
checkio([1, 300, 2, 200, 1]) == 2
checkio([3, 6, 20, 99, 10, 15]) == 12.5
def checkio(data): data.sort() if len(data)%2 == 0: return (data[len(data)//2 - 1]+data[len(data)//2])/2 return data[len(data)//2] #replace this for solution # off = len(data) // 2 # data.sort() # med = data[off] + data[-(off + 1)] # return med / 2#These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == '__main__': assert checkio([1, 2, 3, 4, 5]) == 3, "Sorted list" assert checkio([3, 1, 2, 5, 3]) == 3, "Not sorted list" assert checkio([1, 300, 2, 200, 1]) == 2, "It's not an average" assert checkio([3, 6, 20, 99, 10, 15]) == 12.5, "Even length" print("Start the long test") assert checkio(list(range(1000000))) == 499999.5, "Long." print("The local tests are done.")排名第一的解法:def checkio(data): data.sort()off = len(data) // 2 med = data[off] + data[-(off + 1)] return med / 2#replace this for solution
1 0
- CheckIO 中位数
- checkio--
- checkio (Brackets)
- Python Checkio
- Checkio--Friends
- checkio-medium
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- JavaScript window、location对象基础知识整理
- android Notification 入门
- JSON.NET 简单的使用
- Glide-Target
- Java压缩和解压文件工具类ZipUtil
- CheckIO 中位数
- 在VS2013中运行程序时如何传递实参
- Git版本控制学习与使用一
- Hellow
- json(http://www.asp.net/whitepapers/request-validation)
- window GIT GUI 保存账号密码 不用每次请求都输入
- 2016/08/16
- 一次azkaban源码阅读经历
- [Web前端工具篇]SubLime 3基础技巧与编程