aboutsummaryrefslogtreecommitdiff
path: root/challenge-172/mohammad-anwar/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-172/mohammad-anwar/python/ch-1.py')
-rw-r--r--challenge-172/mohammad-anwar/python/ch-1.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-172/mohammad-anwar/python/ch-1.py b/challenge-172/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..6a5149a3dc
--- /dev/null
+++ b/challenge-172/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python3
+
+'''
+
+Week 172:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-172
+
+Task #1: Prime Partition
+
+ You are given two positive integers, $m and $n.
+
+ Write a script to find out the Prime Partition of the given
+ number. No duplicates allowed.
+
+'''
+
+import math
+import unittest
+from itertools import combinations
+
+def is_prime(n):
+ if n == 1:
+ return False
+
+ i = 3
+ while (i <= int(math.sqrt(n))):
+ if ((n % i) == 0):
+ return False
+ i += 2
+
+ return True
+
+def prime_upto(m):
+ primes = []
+ i = 1
+ while i <= m:
+ i = i + 2
+ if is_prime(i):
+ primes.append(i)
+
+ return primes
+
+def prime_partition(m, n):
+ primes = prime_upto(m)
+ for combination in combinations(primes, n):
+ if sum(combination) == m:
+ return combination
+
+#
+#
+# Unit test class
+
+class TestPrimePartition(unittest.TestCase):
+
+ def test_example_1(self):
+ self.assertEqual(prime_partition(18, 2), (5, 13), 'Example 1')
+
+ def test_example_2(self):
+ self.assertEqual(prime_partition(19, 3), (3, 5, 11), 'Example 2')
+
+unittest.main()