aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-14 09:34:13 +0100
committerGitHub <noreply@github.com>2025-10-14 09:34:13 +0100
commitd91151c44ebc37f922a2eb9a897cbdb56e8ee81c (patch)
tree53e8a14d0c16db2e98237c1ea2840bc4d0d8fbd7
parent2983fb67f029aecc5fd4bf3f5ebabe4387390291 (diff)
parent03a7443b97e6c1f78abe3f5227123ff81d6cadab (diff)
downloadperlweeklychallenge-club-d91151c44ebc37f922a2eb9a897cbdb56e8ee81c.tar.gz
perlweeklychallenge-club-d91151c44ebc37f922a2eb9a897cbdb56e8ee81c.tar.bz2
perlweeklychallenge-club-d91151c44ebc37f922a2eb9a897cbdb56e8ee81c.zip
Merge pull request #12848 from LubosKolouch/lubos/python-ch-1
Add Python solution for challenge 343 task 1
-rw-r--r--challenge-343/lubos-kolouch/python/ch-1.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-343/lubos-kolouch/python/ch-1.py b/challenge-343/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..eb3f668ecf
--- /dev/null
+++ b/challenge-343/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python3
+"""
+Perl Weekly Challenge: Task 1 - Zero Friend
+Find the number closest to zero and return its distance to zero.
+"""
+
+import unittest
+
+
+def zero_friend(nums: list[int]) -> int:
+ """
+ Find the distance to zero of the number closest to zero in the list.
+
+ Args:
+ nums (list[int]): List of integers.
+
+ Returns:
+ int: The absolute value (distance to zero) of the number closest to zero.
+ Returns 0 if the list is empty.
+ """
+ if not nums:
+ return 0
+
+ return min(abs(num) for num in nums)
+
+
+class TestZeroFriend(unittest.TestCase):
+ """Unit tests for the zero_friend function."""
+
+ def test_case_1(self):
+ """Test Case 1: (4, 2, -1, 3, -2) -> 1"""
+ self.assertEqual(zero_friend([4, 2, -1, 3, -2]), 1)
+
+ def test_case_2(self):
+ """Test Case 2: (-5, 5, -3, 3, -1, 1) -> 1"""
+ self.assertEqual(zero_friend([-5, 5, -3, 3, -1, 1]), 1)
+
+ def test_case_3(self):
+ """Test Case 3: (7, -3, 0, 2, -8) -> 0"""
+ self.assertEqual(zero_friend([7, -3, 0, 2, -8]), 0)
+
+ def test_case_4(self):
+ """Test Case 4: (-2, -5, -1, -8) -> 1"""
+ self.assertEqual(zero_friend([-2, -5, -1, -8]), 1)
+
+ def test_case_5(self):
+ """Test Case 5: (-2, 2, -4, 4, -1, 1) -> 1"""
+ self.assertEqual(zero_friend([-2, 2, -4, 4, -1, 1]), 1)
+
+
+if __name__ == '__main__':
+ unittest.main()