First Two Total

When working with lists, you often need to take the first items safely without breaking on short input. Write a function sum2 that takes a list nums and returns the sum of its first two elements. If the list has only one element, return that element; if the list is empty, return 0.

Примеры

Ввод
[1, 2, 3]
Вывод
3
Ввод
[1, 1]
Вывод
2
Ввод
[1, 1, 1, 1]
Вывод
2

Нужно реализовать

def sum2(nums: list[int]) -> int:

Теги

listsarraymath
PostgreSQLv16

Tukaj bo rezultat izvajanja kode

Visible tests
1
Input
[1,2,3]
Expected3
2
Input
[1,1]
Expected2
3
Input
[1,1,1,1]
Expected2
4
Input
[1]
Expected1
5
Input
[]
Expected0
6
Input
[2,3,4]
Expected5
Focus radio
Paused · SomaFM · Fluid