aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-10 20:57:52 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-10 20:57:52 +0100
commitbf34f8834b17f2a099c7c98c555e8ba2c019ee02 (patch)
tree3560c6f3b1ddc5f5706e0a4edb5122e6730cdc93
parent5a2982a8f1d62966cd529867c57530531c7d421a (diff)
downloadperlweeklychallenge-club-bf34f8834b17f2a099c7c98c555e8ba2c019ee02.tar.gz
perlweeklychallenge-club-bf34f8834b17f2a099c7c98c555e8ba2c019ee02.tar.bz2
perlweeklychallenge-club-bf34f8834b17f2a099c7c98c555e8ba2c019ee02.zip
- Added Python solution to the task "Five-number Summary" of week 172.
-rw-r--r--challenge-172/mohammad-anwar/python/ch-2.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-172/mohammad-anwar/python/ch-2.py b/challenge-172/mohammad-anwar/python/ch-2.py
new file mode 100644
index 0000000000..ffc590a7b8
--- /dev/null
+++ b/challenge-172/mohammad-anwar/python/ch-2.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python3
+
+'''
+
+Week 172:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-172
+
+Task #2: Five-number Summary
+
+ You are given an array of integers.
+
+ Write a script to compute the five-number summary of the given
+ set of integers.
+
+'''
+
+import sys
+import unittest
+
+def median(n):
+ if len(n) % 2 == 1:
+ return n[int((len(n)-1)/2)]
+
+ return (n[int(len(n)/2)-1] + n[int(len(n)/2)]) / 2
+
+def five_number_summary(n):
+ n = sorted(n)
+ _min = n[0]
+ _max = n[-1]
+ _med = median(n)
+ _q1 = median([i for i in n if i < _med])
+ _q3 = median([i for i in n if i > _med])
+
+ return [_min, _q1, _med, _q3, _max]
+
+#
+#
+# Unit test class
+
+class TestPrimePartition(unittest.TestCase):
+
+ def test_example(self):
+ self.assertEqual(
+ five_number_summary([0,0,1,2,63,61,27,13]),
+ [0,0.5,7.5,44,63],
+ 'Example')
+
+unittest.main()