aboutsummaryrefslogtreecommitdiff
path: root/challenge-343/steven-wilson/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-343/steven-wilson/python/ch-1.py')
-rw-r--r--challenge-343/steven-wilson/python/ch-1.py25
1 files changed, 25 insertions, 0 deletions
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)