aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-10-03 21:26:06 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-10-03 21:26:06 +0100
commitfc1f87d72095b4e31e6b5ad6bdb03d925b0af189 (patch)
treefd19944bcb38e0dd4c9570a0b75677bf3cbc8748
parent7e66febccdc320ea321a9b1fa2b709b25599d5dc (diff)
downloadperlweeklychallenge-club-fc1f87d72095b4e31e6b5ad6bdb03d925b0af189.tar.gz
perlweeklychallenge-club-fc1f87d72095b4e31e6b5ad6bdb03d925b0af189.tar.bz2
perlweeklychallenge-club-fc1f87d72095b4e31e6b5ad6bdb03d925b0af189.zip
Add Python solution to challenge 172
-rw-r--r--challenge-172/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-172/paulo-custodio/perl/ch-2.pl4
-rw-r--r--challenge-172/paulo-custodio/python/ch-1.py90
-rw-r--r--challenge-172/paulo-custodio/python/ch-2.py32
-rw-r--r--challenge-172/paulo-custodio/t/test-2.yaml2
5 files changed, 126 insertions, 4 deletions
diff --git a/challenge-172/paulo-custodio/perl/ch-1.pl b/challenge-172/paulo-custodio/perl/ch-1.pl
index 6753187f1a..1a28191ff2 100644
--- a/challenge-172/paulo-custodio/perl/ch-1.pl
+++ b/challenge-172/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 172
#
diff --git a/challenge-172/paulo-custodio/perl/ch-2.pl b/challenge-172/paulo-custodio/perl/ch-2.pl
index a7a6852c2e..1f4bf144c9 100644
--- a/challenge-172/paulo-custodio/perl/ch-2.pl
+++ b/challenge-172/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 172
#
@@ -40,4 +40,4 @@ sub five_number_summary {
return ($n[0], lower_quartile(@n), median(@n), upper_quartile(@n), $n[-1]);
}
-say join ", ", five_number_summary(@ARGV);
+say join ", ", map {sprintf("%.1f", $_)} five_number_summary(@ARGV);
diff --git a/challenge-172/paulo-custodio/python/ch-1.py b/challenge-172/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..7bc35bc760
--- /dev/null
+++ b/challenge-172/paulo-custodio/python/ch-1.py
@@ -0,0 +1,90 @@
+#!/usr/bin/env python3
+
+# Challenge 172
+#
+# Task 1: Prime Partition
+# Submitted by: Mohammad S Anwar
+#
+# 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.
+#
+# For example,
+#
+# Input: $m = 18, $n = 2
+# Output: 5, 13 or 7, 11
+#
+# Input: $m = 19, $n = 3
+# Output: 3, 5, 11
+
+import sys
+from itertools import count
+from functools import reduce
+
+def is_prime(n):
+ if n <= 1:
+ return False
+ if n <= 3:
+ return True
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n**0.5) + 1, 6):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+def next_prime(n):
+ if n <= 1:
+ return 2
+ p = n
+ while True:
+ p += 1
+ if is_prime(p):
+ return p
+
+def primes(n):
+ p = 2
+ prime_list = []
+ while p <= n:
+ prime_list.append(p)
+ p = next_prime(p)
+ return prime_list
+
+def odometer_increment(limit, n):
+ i = len(n) - 1
+ while True:
+ n[i] += 1
+ if n[i] >= limit:
+ n[i] = 0
+ i -= 1
+ if i < 0:
+ n.insert(0, 0)
+ i = 0
+ else:
+ break
+ return n
+
+def has_duplicates(n):
+ seen = set()
+ for num in n:
+ if num in seen:
+ return True
+ seen.add(num)
+ return False
+
+def prime_partition(m, n):
+ prime_list = primes(m)
+ idx = [0] * n
+ while len(idx) == n:
+ if not has_duplicates(idx):
+ terms = [prime_list[i] for i in idx]
+ if sum(terms) == m:
+ return terms
+ idx = odometer_increment(len(prime_list), idx)
+ return []
+
+if len(sys.argv) != 3:
+ raise ValueError("usage: ch-1.py m n")
+m, n = map(int, sys.argv[1:])
+print(", ".join(map(str, prime_partition(m, n))))
diff --git a/challenge-172/paulo-custodio/python/ch-2.py b/challenge-172/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..d6de8fcc5b
--- /dev/null
+++ b/challenge-172/paulo-custodio/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+# Challenge 172
+#
+# Task 2: Five-number Summary
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers.
+#
+# Write a script to compute the five-number summary of the given set of integers.
+#
+# You can find the definition and example in the wikipedia page.
+
+def median(n):
+ if len(n) % 2 == 0:
+ i = len(n) // 2
+ return (n[i - 1] + n[i]) / 2
+ else:
+ return n[len(n) // 2]
+
+def lower_quartile(n):
+ return median(n[:len(n) // 2])
+
+def upper_quartile(n):
+ return median(n[len(n) // 2:])
+
+def five_number_summary(n):
+ n.sort()
+ return (n[0], lower_quartile(n), median(n), upper_quartile(n), n[-1])
+
+import sys
+print(", ".join([f"{x:.1f}" for x in five_number_summary(list(map(float, sys.argv[1:])))]))
diff --git a/challenge-172/paulo-custodio/t/test-2.yaml b/challenge-172/paulo-custodio/t/test-2.yaml
index 7271a491ad..c840c8ef46 100644
--- a/challenge-172/paulo-custodio/t/test-2.yaml
+++ b/challenge-172/paulo-custodio/t/test-2.yaml
@@ -2,4 +2,4 @@
cleanup:
args: 0 0 1 2 63 61 27 13
input:
- output: 0, 0.5, 7.5, 44, 63
+ output: 0.0, 0.5, 7.5, 44.0, 63.0