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
Jelentkezz be a beküldési előzmények megtekintéséhez
BejelentkezésJelentkezz be az MI-mentor használatához
BejelentkezésVisible tests
1
Input
[1,2,3]Expected
32
Input
[1,1]Expected
23
Input
[1,1,1,1]Expected
24
Input
[1]Expected
15
Input
[]Expected
06
Input
[2,3,4]Expected
5Focus radio
Paused · SomaFM · Fluid