简述网站建设主要流程,做豆制品的网站,长春电商网站建设价格,win2008 挂网站 404CheckiO 是面向初学者和高级程序员的编码游戏#xff0c;使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务#xff0c;从而提高你的编码技能#xff0c;本博客主要记录自己用 Python 在闯关时的做题思路和实现代码#xff0c;同时也学习学习其他大神写的代码。
Chec…
CheckiO 是面向初学者和高级程序员的编码游戏使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务从而提高你的编码技能本博客主要记录自己用 Python 在闯关时的做题思路和实现代码同时也学习学习其他大神写的代码。
CheckiO 官网https://checkio.org/
我的 CheckiO 主页https://py.checkio.org/user/TRHX/
CheckiO 题解系列专栏https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有题解源代码https://github.com/TRHX/Python-CheckiO-Exercise 题目描述
【Median】给定一个数组查找其中位数如果数组的元素个数是偶数则返回中间两个元素的平均值。
【链接】https://py.checkio.org/mission/median/
【输入】由整数组成的数组list
【输出】数组的中位数int or float
【前提】1 len(data) ≤ 1000all(0 ≤ x 10 ** 6 for x in data)
【范例】
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解题思路
先用 sort() 方法将数组元素按照从小到大排序利用数组的长度除以 2 来判断其元素个数是奇数还是偶数。
代码实现
from typing import Listdef checkio(data: List[int]) - [int, float]:data.sort()if len(data) % 2 0:return (data[int(len(data)/2) - 1] data[int(len(data)/2)])/2else:return data[int(len(data)/2)]# These asserts using only for self-checking and not necessary for auto-testing
if __name__ __main__:print(Example:)print(checkio([1, 2, 3, 4, 5]))assert checkio([1, 2, 3, 4, 5]) 3, Sorted listassert checkio([3, 1, 2, 5, 3]) 3, Not sorted listassert checkio([1, 300, 2, 200, 1]) 2, Its not an averageassert checkio([3, 6, 20, 99, 10, 15]) 12.5, Even lengthprint(Start the long test)assert checkio(list(range(1000000))) 499999.5, Long.print(Coding complete? Click Check to earn cool rewards!)大神解答 大神解答 NO.1 from typing import List
from statistics import mediandef checkio(data: List[int]) - [int, float]:return median(data)statistics 模块的 median 方法可以直接求中位数 大神解答 NO.2 from typing import Listdef checkio(data: List[int]) - [int, float]:data sorted(data)l len(data) return [(data[l//2]data[l//2-1])/2, data[l//2]][l%2]大神解答 NO.3 from typing import Listdef checkio(data):data.sort()half len(data) // 2return (data[half] data[~half]) / 2