已知一个包含表示年龄的数字列表,它至少会包含2个元素,并且是无序的。请编写一个函数,返回其中最大的两个年龄列表,并且按照生序排列。
示例:
输入:[1, 3, 10, 0],返回:[3, 10]
题目难度:简单
题目来源:Codewars: Two Oldest Ages
def solution(ages: list) -> list:
# your code
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
nobugs
2021 年12 月 27 日 02:29
2
def solution(ages: list) -> list:
return sorted(ages, reverse=False)[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
def solution(ages: list) -> list:
ages = set(ages)
sortages = list(ages)
sortages.sort()
return [sortages[-2],sortages[-1]]
Kawi
2021 年12 月 27 日 02:43
4
def solution(ages: list) -> list:
ages.sort()
ages = ages[-2:]
return ages
Mooii
2021 年12 月 27 日 09:23
5
def solution(ages: list) -> list:
ages.sort()
return ages[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
def solution9(ll:list):
ll.sort(reverse=True)
return [ll[1],ll[0]]
assert solution9([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution9([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution9([10, 1]) == [1, 10]
def solution(ages: list) -> list:
return sorted(ages)[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
def solution(ages: list) -> list:
return sorted(ages)[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
Huis
2021 年12 月 28 日 08:09
9
def solution(ages: list) -> list:
if len(ages)<2: return ages
result = [float("-inf")]
for i in range(len(ages)):
if i == 0: result.append(ages[i])
else:
if ages[i] > result[len(result)-1]:
result[0]= result[len(result)-1]
result[1]= ages[i]
elif ages[i] > result[0]: result[0]=ages[i]
return result
def solution(ages: list) -> list:
return sorted(ages)[-2:]
kary
2021 年12 月 30 日 04:14
12
def solution(ages: list) -> list:
# your code
ages.sort()
return ages[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
abyss
(abyss)
2022 年1 月 5 日 06:33
13
def solution(age: list):
return sorted(age)[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]
wangke
(CR7)
2022 年1 月 5 日 08:50
14
def solution(ages):
ages.sort()
return ages[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]预先格式化的文本
def solution(ages: list) -> list:
return sorted(ages)[-2:]
assert solution([1, 5, 87, 45, 8, 8]) == [45, 87]
assert solution([6, 5, 83, 5, 3, 18]) == [18, 83]
assert solution([10, 1]) == [1, 10]