diff options
Diffstat (limited to 'challenge-252/lubos-kolouch/python/ch-2.py')
| -rw-r--r-- | challenge-252/lubos-kolouch/python/ch-2.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-252/lubos-kolouch/python/ch-2.py b/challenge-252/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..6bdc162db2 --- /dev/null +++ b/challenge-252/lubos-kolouch/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + + +def unique_sum_zero(n: int) -> list: + """ + Generates a list of `n` unique integers that sum to zero. + + Parameters: + n (int): The size of the list. + + Returns: + list: A list of `n` unique integers that add up to zero. + """ + # For even n, use symmetric pairs; for odd n, include 0 and symmetric pairs + return ( + [i for i in range(1, n // 2 + 1)] + + [-i for i in range(1, n // 2 + 1)] + + ([0] if n % 2 != 0 else []) + ) + + +# Tests +assert sum(unique_sum_zero(5)) == 0 +assert sum(unique_sum_zero(3)) == 0 +assert sum(unique_sum_zero(1)) == 0 |
