aboutsummaryrefslogtreecommitdiff
path: root/challenge-172
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-06 21:43:34 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-06 21:43:34 +0100
commita4595e41ab0e9db124dbaee1b437f879857047df (patch)
tree8ad7723e94feadd611291e7016c297061784cde6 /challenge-172
parent3ae846f978ccc384ea35823fb2f8b225c83e6dc8 (diff)
downloadperlweeklychallenge-club-a4595e41ab0e9db124dbaee1b437f879857047df.tar.gz
perlweeklychallenge-club-a4595e41ab0e9db124dbaee1b437f879857047df.tar.bz2
perlweeklychallenge-club-a4595e41ab0e9db124dbaee1b437f879857047df.zip
- Added Python solution to the task "Prime Partition" of week 172.
Diffstat (limited to 'challenge-172')
-rw-r--r--[-rwxr-xr-x]challenge-172/mohammad-anwar/perl/ch-1.pl0
-rw-r--r--challenge-172/mohammad-anwar/python/ch-1.py62
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-172/mohammad-anwar/perl/ch-1.pl b/challenge-172/mohammad-anwar/perl/ch-1.pl
index 5e0e9c7731..5e0e9c7731 100755..100644
--- a/challenge-172/mohammad-anwar/perl/ch-1.pl
+++ b/challenge-172/mohammad-anwar/perl/ch-1.pl
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()