【每日一题1227】最大年龄

:woman_mage: 已知一个包含表示年龄的数字列表,它至少会包含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]
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]]
def solution(ages: list) -> list:
    ages.sort()
    ages = ages[-2:]
    return ages
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]
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:]
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]
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]

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]预先格式化的文本

关闭