Created
August 5, 2024 09:35
-
-
Save rjvitorino/f0449b96f89b897701b12c78b5fb37fd to your computer and use it in GitHub Desktop.
Cassidoo's interview question of the week: a function that should take one argument n, a positive integer, and return the sum of all squared positive integers between 1 and n, inclusive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Union | |
def squares(n: int) -> Union[int, None]: | |
""" | |
Calculate the sum of all squared positive integers from 1 to n, inclusive. | |
Args: | |
n (int): A positive integer. | |
Returns: | |
Union[int, None]: The sum of squares from 1 to n if n is positive; otherwise, None. | |
""" | |
if n <= 0: | |
return None | |
return sum(number**2 for number in range(1, n + 1)) | |
assert squares(5) == 55 | |
assert squares(10) == 385 | |
assert squares(25) == 5525 | |
assert squares(100) == 338350 | |
assert squares(1) == 1 | |
assert squares(0) is None | |
assert squares(-5) is None | |
assert squares(50) == 42925 | |
assert squares(15) == 1240 | |
print("All assertions passed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment