1. List/sum(list)
@spec List/sum(List[Number]) -> Number
Calculates the sum of all numbers in the given list.
Parameters
list: A list of numbers (integers or floats)
Returns the sum of all numbers in the list.
Possible (but not limited to) errors:
InvalidType: If the list contains non-numeric elements
Examples
# Sum an empty list
result = List/sum([])
# 0
# Sum a list of integers
result = List/sum([1, 2, 3, 4, 5])
# 15
# Sum a list of floats
result = List/sum([1.5, 2.7, 3.2])
# 7.4
# Attempt to sum a list with non-numeric elements
result = List/sum([1, 2, "3", 4])
# InvalidType: List contains non-numeric elements
2. List/range(start, stop, step=1)
@spec List/range(int, int, int) -> List[int]
Generates a list of integers from start to stop (exclusive) with a given step.
Parameters
start: The starting value of the range (inclusive)
stop: The ending value of the range (exclusive)
step: The difference between each number in the range (default is 1)
Returns a list of integers.
Possible (but not limited to) errors:
InvalidValue: If step is 0, or if the range would never terminate
Examples
# Generate a simple range
result = List/range(0, 5)
# [0, 1, 2, 3, 4]
# Generate a range with a custom step
result = List/range(0, 10, 2)
# [0, 2, 4, 6, 8]
# Generate a descending range
result = List/range(5, 0, -1)
# [5, 4, 3, 2, 1]
# Attempt to create a range with a step 0
result = List/range(0, 5, 0)
# `InvalidValue`: Step cannot be zero
# Create an empty range
result = List/range(0, 0)
# []
Considerations
- Ensure efficient implementation, especially for
List/range with large ranges
- For
List/sum, consider using an appropriate numeric type to avoid overflow for large sums
- Implement proper error checking and handling
- Ensure compatibility with both integer and floating-point numbers for
List/sum
- For
List/range, consider memory efficiency for large ranges
- Implement appropriate type checking to ensure arguments are of the correct type
Test cases to implement
- Sum a list of positive integers
- Sum a list of negative integers
- Sum a list of mixed positive and negative integers
- Sum a list of floating-point numbers
- Attempt to sum an empty list
- Attempt to sum a list with non-numeric elements
- Generate a range with only positive integers
- Generate a range with negative integers
- Generate a range with a custom step (both positive and negative)
- Generate a range that results in an empty list
- Attempt to generate a range with a step of 0
- Generate a very large range and check for memory efficiency
- Sum a very large list and check for numeric overflow handling
- Generate ranges with various combinations of start, stop, and step values
1.
List/sum(list)Calculates the sum of all numbers in the given list.
Parameters
list: A list of numbers (integers or floats)Returns the sum of all numbers in the list.
Possible (but not limited to) errors:
InvalidType: If the list contains non-numeric elementsExamples
2.
List/range(start, stop, step=1)Generates a list of integers from
starttostop(exclusive) with a givenstep.Parameters
start: The starting value of the range (inclusive)stop: The ending value of the range (exclusive)step: The difference between each number in the range (default is 1)Returns a list of integers.
Possible (but not limited to) errors:
InvalidValue: If step is 0, or if the range would never terminateExamples
Considerations
List/rangewith large rangesList/sum, consider using an appropriate numeric type to avoid overflow for large sumsList/sumList/range, consider memory efficiency for large rangesTest cases to implement