Skip to content

Commit 12b4374

Browse files
author
joseguilhermefmoura
committed
💡 Add Even the Last
1 parent c38dea3 commit 12b4374

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

02_Home/02.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
# Even the Last
3+
4+
## You are given an array of integers.
5+
## You should find the sum of the integers with even indexes (0th, 2nd, 4th...).
6+
## Then multiply this summed number and the final element of the array together.
7+
## Don't forget that the first element has an index of 0.
8+
## For an empty array, the result will always be 0 (zero).
9+
10+
- Input: A list of integers.
11+
- Output: The number as an integer.
12+
13+
>> EXAMPLE:
14+
15+
checkio([0, 1, 2, 3, 4, 5]) == 30
16+
checkio([1, 3, 5]) == 30
17+
checkio([6]) == 36
18+
checkio([]) == 0
19+
20+
<<
21+
"""
22+
23+
24+
def checkio(array: list) -> int:
25+
if not array:
26+
return 0
27+
else:
28+
return sum([array[i] for i in range(0, len(array), 2)]) * array[-1]
29+
30+
31+
# These "asserts" are used for self-checking and not for an auto-testing
32+
assert checkio([0, 1, 2, 3, 4, 5]) == 30, "(0+2+4)*5=30"
33+
assert checkio([1, 3, 5]) == 30, "(1+5)*5=30"
34+
assert checkio([6]) == 36, "(6)*6=36"
35+
assert checkio([]) == 0, "An empty array = 0"

0 commit comments

Comments
 (0)