From ad9fb0c47a8eb28587f94a2087595a0f06c2387e Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Mon, 22 Sep 2025 20:13:38 +0000 Subject: [PATCH 1/7] [Sync Iteration] python/all-your-base/1 --- .../python/all-your-base/1/all_your_base.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 solutions/python/all-your-base/1/all_your_base.py diff --git a/solutions/python/all-your-base/1/all_your_base.py b/solutions/python/all-your-base/1/all_your_base.py new file mode 100644 index 0000000..93e5e30 --- /dev/null +++ b/solutions/python/all-your-base/1/all_your_base.py @@ -0,0 +1,33 @@ +""" +Convert a sequence of digits in one base, representing a number, +into a sequence of digits in another base, representing the same number. +""" + + +def rebase(input_base: int, digits: list[int], output_base: int): + """ + Convert a non-negative integer represented as digits in one base to digits in another base. + + :param int input_base: Base of the input digits; must be >= 2. + :param list[int] digits: Sequence of digits where each d satisfies 0 <= d < input_base. + Leading zeros are allowed; an empty list denotes 0. + :param int output_base: Base for the output digits; must be >= 2. + :returns: Digits of the same number in ``output_base``, without leading zeros + (except ``[0]`` for zero). + :rtype: list[int] + :raises ValueError: If ``input_base < 2``, if any digit violates ``0 <= d < input_base``, + or if ``output_base < 2``. + """ + + # for input. + if input_base < 2: + raise ValueError("input base must be >= 2") + + # another example for input. + for d in digits: + if not 0 <= d < input_base: + raise ValueError("all digits must satisfy 0 <= d < input base") + + # or, for output. + if output_base < 2: + raise ValueError("output base must be >= 2") From 44818cfd97cc0bcc89d1c549050150c003e8ef98 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Mon, 22 Sep 2025 18:03:07 -0700 Subject: [PATCH 2/7] Update sublist.py --- sublist/sublist.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sublist/sublist.py b/sublist/sublist.py index 5edb527..5fcbd59 100644 --- a/sublist/sublist.py +++ b/sublist/sublist.py @@ -34,10 +34,7 @@ def sublist(list_one: list, list_two: list) -> int: :rtype: int """ - len1: int = len(list_one) - len2: int = len(list_two) - - if len1 == len2: + if len(list_one) == len(list_two): if list_one == list_two: return EQUAL return UNEQUAL From 5dd7b8237e2ebf94d754b97fc4915569b8a6f9fd Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 23 Sep 2025 01:10:00 +0000 Subject: [PATCH 3/7] [Sync Iteration] python/sublist/7 --- solutions/python/sublist/7/sublist.py | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 solutions/python/sublist/7/sublist.py diff --git a/solutions/python/sublist/7/sublist.py b/solutions/python/sublist/7/sublist.py new file mode 100644 index 0000000..5fcbd59 --- /dev/null +++ b/solutions/python/sublist/7/sublist.py @@ -0,0 +1,51 @@ +""" +This exercise stub and the test suite contain several enumerated constants. + +Enumerated constants can be done with a NAME assigned to an arbitrary, +but unique value. An integer is traditionally used because it’s memory +efficient. + +It is a common practice to export both constants and functions that work with +those constants (ex. the constants in the os, subprocess and re modules). + +You can learn more here: https://en.wikipedia.org/wiki/Enumerated_type +""" + +# Possible sublist categories. +# Change the values as you see fit. +SUBLIST = 0 +SUPERLIST = 1 +EQUAL = 2 +UNEQUAL = 3 + + +def sublist(list_one: list, list_two: list) -> int: + """ + Classify the relationship between two lists. + + Determines whether ``list_one`` and ``list_two`` are equal, or whether one + is a contiguous sublist of the other, and returns the appropriate constant. + + :param list_one: First list to compare. + :type list_one: list + :param list_two: Second list to compare. + :type list_two: list + :returns: One of ``EQUAL``, ``SUBLIST``, ``SUPERLIST``, or ``UNEQUAL``. + :rtype: int + """ + + if len(list_one) == len(list_two): + if list_one == list_two: + return EQUAL + return UNEQUAL + + l1: str = ",".join(str(i) for i in list_one) + l2: str = ",".join(str(i) for i in list_two) + + if l2 in l1: + return SUPERLIST + + if l1 in l2: + return SUBLIST + + return UNEQUAL From d42ebe702942b5ae4c55c3c1d2d21022c3814933 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Mon, 22 Sep 2025 19:36:36 -0700 Subject: [PATCH 4/7] Update sublist_test.py --- sublist/sublist_test.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sublist/sublist_test.py b/sublist/sublist_test.py index 2ca0cb9..a80a5b6 100644 --- a/sublist/sublist_test.py +++ b/sublist/sublist_test.py @@ -89,3 +89,13 @@ def test_spread_sublist(self): self.assertEqual( sublist(list(range(3, 200, 3)), list(range(15, 200, 15))), UNEQUAL ) + + +if __name__ == '__main__': + import time + + start_time = time.time() + unittest.main(verbosity=2) # Verbosity=2 for detailed output; adjust if needed + end_time = time.time() + duration = end_time - start_time + print(f"\nAll tests completed in {duration:.4f} seconds.") From b581ae362038b79c2c281c2214d53c3f8d501f63 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Mon, 22 Sep 2025 21:01:37 -0700 Subject: [PATCH 5/7] Update sublist.py --- sublist/sublist.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sublist/sublist.py b/sublist/sublist.py index 5fcbd59..6af686b 100644 --- a/sublist/sublist.py +++ b/sublist/sublist.py @@ -33,19 +33,19 @@ def sublist(list_one: list, list_two: list) -> int: :returns: One of ``EQUAL``, ``SUBLIST``, ``SUPERLIST``, or ``UNEQUAL``. :rtype: int """ - + # Step #1: test list with same length if len(list_one) == len(list_two): if list_one == list_two: return EQUAL return UNEQUAL - + # Step #2: convert list to a string l1: str = ",".join(str(i) for i in list_one) l2: str = ",".join(str(i) for i in list_two) - + # Step #3: check for substring if l2 in l1: return SUPERLIST if l1 in l2: return SUBLIST - + # Step #4: Return default return UNEQUAL From 3b28d4f28a577b762aa449d301e475723f05be8c Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 23 Sep 2025 05:39:38 +0000 Subject: [PATCH 6/7] [Sync Iteration] python/all-your-base/2 --- .../python/all-your-base/2/all_your_base.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 solutions/python/all-your-base/2/all_your_base.py diff --git a/solutions/python/all-your-base/2/all_your_base.py b/solutions/python/all-your-base/2/all_your_base.py new file mode 100644 index 0000000..ab9209b --- /dev/null +++ b/solutions/python/all-your-base/2/all_your_base.py @@ -0,0 +1,60 @@ +""" +Convert a sequence of digits in one base, representing a number, +into a sequence of digits in another base, representing the same number. +""" + + +def rebase(input_base: int, digits: list[int], output_base: int) -> list[int]: + """ + Convert a non-negative integer represented as digits in one base + to digits in another base. + + :param int input_base: Base of the input digits; must be >= 2. + :param list[int] digits: Sequence of digits where each d satisfies + 0 <= d < input_base. Leading zeros are allowed; + an empty list denotes 0. + :param int output_base: Base for the output digits; must be >= 2. + :returns: Digits of the same number in ``output_base``, without leading + zeros (except ``[0]`` for zero). + :rtype: list[int] + :raises ValueError: If ``input_base < 2``, if any digit violates + ``0 <= d < input_base``, or if ``output_base < 2``. + """ + # Step 1: Validate the Inputs (Before Any Calculations) + # Validate input_base >= 2 + if input_base < 2: + raise ValueError("input base must be >= 2") + + # Validate each digit is 0 <= d < input_base + for d in digits: + if not 0 <= d < input_base: + raise ValueError("all digits must satisfy 0 <= d < input base") + + # Validate output_base >= 2 + if output_base < 2: + raise ValueError("output base must be >= 2") + + # Step 2: Calculate the Intermediate Value Using input_base and digits + # Start from the least significant number (from the right) + # -> hence reverse the list + intermediate_val: int = sum( + digit * (input_base**i) for i, digit in enumerate(reversed(digits)) + ) + + # Step 3: Convert the Intermediate Value to digits in output_base + answer: list[int] = [] + while True: + # Compute quotient (new intermediate_val) + # and remainder (least significant digit) + intermediate_val, remainder = ( + intermediate_val // output_base, + intermediate_val % output_base, + ) + # Add the least significant number into a new list of digits + answer.append(remainder) + # Break the loop since reach zero, no more calculation needed + if intermediate_val == 0: + break + + # Reverse the list to place the most significant digit first (leftmost). + return answer[::-1] From 9c8bb8fcc99f820d96df449b9dd7dd1a49e15a25 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Mon, 22 Sep 2025 22:48:38 -0700 Subject: [PATCH 7/7] Update all_your_base.py --- all-your-base/all_your_base.py | 35 +++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/all-your-base/all_your_base.py b/all-your-base/all_your_base.py index ff8d59c..678e9ea 100644 --- a/all-your-base/all_your_base.py +++ b/all-your-base/all_your_base.py @@ -4,7 +4,7 @@ """ -def rebase(input_base: int, digits: list[int], output_base: int): +def rebase(input_base: int, digits: list[int], output_base: int) -> list[int]: """ Convert a non-negative integer represented as digits in one base to digits in another base. @@ -20,16 +20,41 @@ def rebase(input_base: int, digits: list[int], output_base: int): :raises ValueError: If ``input_base < 2``, if any digit violates ``0 <= d < input_base``, or if ``output_base < 2``. """ - - # for input. + # Step 1: Validate the Inputs (Before Any Calculations) + # Validate input_base >= 2 if input_base < 2: raise ValueError("input base must be >= 2") - # another example for input. + # Validate each digit is 0 <= d < input_base for d in digits: if not 0 <= d < input_base: raise ValueError("all digits must satisfy 0 <= d < input base") - # or, for output. + # Validate output_base >= 2 if output_base < 2: raise ValueError("output base must be >= 2") + + # Step 2: Calculate the Intermediate Value Using input_base and digits + # Start from the least significant number (from the right in original list) + # -> hence reverse the list + intermediate_val: int = sum( + digit * (input_base**i) for i, digit in enumerate(digits[::-1]) + ) + + # Step 3: Convert the Intermediate Value to digits in output_base + answer: list[int] = [] + while True: + # Compute quotient (new intermediate_val) + # and remainder (least significant digit) + intermediate_val, remainder = ( + intermediate_val // output_base, + intermediate_val % output_base, + ) + # Add the next least significant number into a new list of digits + # Start always from the right + answer.insert(0, remainder) + # Break the loop since reach zero, no more calculation needed + if intermediate_val == 0: + break + + return answer