From a4595e41ab0e9db124dbaee1b437f879857047df Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 6 Jul 2022 21:43:34 +0100 Subject: - Added Python solution to the task "Prime Partition" of week 172. --- challenge-172/mohammad-anwar/perl/ch-1.pl | 0 challenge-172/mohammad-anwar/python/ch-1.py | 62 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) mode change 100755 => 100644 challenge-172/mohammad-anwar/perl/ch-1.pl create mode 100644 challenge-172/mohammad-anwar/python/ch-1.py diff --git a/challenge-172/mohammad-anwar/perl/ch-1.pl b/challenge-172/mohammad-anwar/perl/ch-1.pl old mode 100755 new mode 100644 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() -- cgit