python 算法題:Sum of two lowest positive integers

Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 integers. No floats or empty arrays will be passed.

For example, when an array is passed like [19, 5, 42, 2, 77], the output should be 7.

[10, 343445353, 3453445, 3453545353453] should return 3453455.


這個思路比較清晰:

#list中兩個最小的數的和
def sum_two_smallest_numbers(numbers):
    i=min(numbers)
    numbers.remove(i)
    j=min(numbers)
    return i+j

發佈了36 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章