013.两个最大数字:给定一个数字列表,请编写一个函数,找出其中最大的两个不同数字,并且返回结果按照从大到小排列。
题目来源:codcwars
# @-*- coding: utf-8 -*-
# @File: python每日一练1017.py
# @Time: 2021/10/17 14:00
# @Author: 师玉幺叔
# @Software: Pycharm
"""
给定一个数字列表,请编写一个函数,找出其中最大的两个不同数字,并且返回结果按照从大到小排列。
示例:
输入: [4, 10, 10, 9],输出:[10, 9]
题目难度:简单
"""
def two_highest(nums: list) -> list:
max_value1 = nums[0]
max_value2 = nums[0]
result_list = []
for i in range(1, len(nums)):
if max_value1 < nums[i]:
max_value1 = nums[i]
for i in range(1, len(nums)):
if max_value2 < nums[i] < max_value1:
max_value2 = nums[i]
result_list.append(max_value1)
result_list.append(max_value2)
return result_list
if __name__ == '__main__':
assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]