blob: fa9ce37b44ecb31309ccbfac6eb9cb0317f062d4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/usr/bin/env python3
import sys
def range_sum(ints: list, x: int, y: int) -> int:
"""Calculate the sum of a range in a list of integers.
Args:
ints (list): A list of integers.
x (int): The starting index of the range (inclusive).
y (int): The ending index of the range (inclusive).
Returns:
int: The sum of the integers from index x to index y (inclusive).
"""
# Sanity checks for x and y
if x < 0 or x >= len(ints):
raise ValueError(f"x must be between 0 and {len(ints) - 1}, got {x}")
if y < 0 or y >= len(ints):
raise ValueError(f"y must be between 0 and {len(ints) - 1}, got {y}")
if x > y:
raise ValueError(
f"x must be less than or equal to y, got x={x}, y={y}")
# Return the sum of the range from x to y (inclusive)
return sum(ints[x:y + 1])
def main():
# Convert input into integers
array = [int(n) for n in sys.argv[1:]]
y = array.pop() # Last element is y
x = array.pop() # Second last element is x
result = range_sum(array, x, y)
print(result)
if __name__ == '__main__':
main()
|