aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2025-10-23 12:19:44 +0200
committerLubos Kolouch <lubos@kolouch.net>2025-10-23 12:19:44 +0200
commit4021c92072eea23184738a405fc1fdfd3451feaa (patch)
treedd77c01b2556e3e41b1ee2d796c2dc2bdb4c3fe2
parent7c0847cc89f19213cb933727c8a775e5409c5d4f (diff)
downloadperlweeklychallenge-club-4021c92072eea23184738a405fc1fdfd3451feaa.tar.gz
perlweeklychallenge-club-4021c92072eea23184738a405fc1fdfd3451feaa.tar.bz2
perlweeklychallenge-club-4021c92072eea23184738a405fc1fdfd3451feaa.zip
Add Python ch-1 solution for challenge 344
-rw-r--r--challenge-344/lubos-kolouch/python/ch-1.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-344/lubos-kolouch/python/ch-1.py b/challenge-344/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..136fd38515
--- /dev/null
+++ b/challenge-344/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+"""
+Perl Weekly Challenge: Task 1 - Array Form Compute
+Add an integer to its array-form representation and return the resulting digits.
+"""
+
+from __future__ import annotations
+
+import unittest
+
+
+def array_form_compute(ints: list[int], x: int) -> list[int]:
+ """
+ Add the integer ``x`` to the number represented by ``ints`` in array form.
+
+ Args:
+ ints (list[int]): Digits of the number ordered from most to least significant.
+ x (int): The value to add.
+
+ Returns:
+ list[int]: Digits of the resulting sum in array form.
+ """
+ result: list[int] = []
+ carry = x
+ index = len(ints) - 1
+
+ while index >= 0 or carry > 0:
+ if index >= 0:
+ carry += ints[index]
+ index -= 1
+ result.append(carry % 10)
+ carry //= 10
+
+ if not result:
+ return [0]
+
+ result.reverse()
+ return result
+
+
+class TestArrayFormCompute(unittest.TestCase):
+ """Unit tests for the array_form_compute function."""
+
+ def test_example_1(self) -> None:
+ """Example 1: (1, 2, 3, 4) + 12 -> (1, 2, 4, 6)"""
+ self.assertEqual(array_form_compute([1, 2, 3, 4], 12), [1, 2, 4, 6])
+
+ def test_example_2(self) -> None:
+ """Example 2: (2, 7, 4) + 181 -> (4, 5, 5)"""
+ self.assertEqual(array_form_compute([2, 7, 4], 181), [4, 5, 5])
+
+ def test_example_3(self) -> None:
+ """Example 3: (9, 9, 9) + 1 -> (1, 0, 0, 0)"""
+ self.assertEqual(array_form_compute([9, 9, 9], 1), [1, 0, 0, 0])
+
+ def test_example_4(self) -> None:
+ """Example 4: (1, 0, 0, 0, 0) + 9999 -> (1, 9, 9, 9, 9)"""
+ self.assertEqual(array_form_compute([1, 0, 0, 0, 0], 9999),
+ [1, 9, 9, 9, 9])
+
+ def test_example_5(self) -> None:
+ """Example 5: (0) + 1000 -> (1, 0, 0, 0)"""
+ self.assertEqual(array_form_compute([0], 1000), [1, 0, 0, 0])
+
+
+if __name__ == "__main__":
+ unittest.main()