From 02dc4be6a3ce32e2ca351ea011d9be5e88e0b730 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 14 Oct 2025 17:00:50 +0100 Subject: add solution week 343 task 1 in python --- challenge-343/steven-wilson/python/ch-1.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-343/steven-wilson/python/ch-1.py diff --git a/challenge-343/steven-wilson/python/ch-1.py b/challenge-343/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..de75719b2c --- /dev/null +++ b/challenge-343/steven-wilson/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + + +def zero_friend(numbers): + """ Given a list of numbers, find the number that is closest to zero and + return its distance to zero. + + >>> zero_friend([4, 2, -1, 3, -2]) + 1 + >>> zero_friend([-5, 5, -3, 3, -1, 1]) + 1 + >>> zero_friend([7, -3, 0, 2, -8]) + 0 + >>> zero_friend([-2, -5, -1, -8]) + 1 + >>> zero_friend([-2, 2, -4, 4, -1, 1]) + 1 + """ + return min(abs(x) for x in numbers) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit