aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-07-10 20:34:06 +1000
committerSimon Green <mail@simon.green>2022-07-10 20:34:06 +1000
commit72aca2620e73ef772c725fd927e56960ef2fa964 (patch)
treec0ffce8d23c68b41920308ea78585894726f4884
parent0359ead5987568ba01e6a3977aee9406f9b90818 (diff)
downloadperlweeklychallenge-club-72aca2620e73ef772c725fd927e56960ef2fa964.tar.gz
perlweeklychallenge-club-72aca2620e73ef772c725fd927e56960ef2fa964.tar.bz2
perlweeklychallenge-club-72aca2620e73ef772c725fd927e56960ef2fa964.zip
sgreen solutions to challenge 172
-rw-r--r--challenge-172/sgreen/README.md4
-rw-r--r--challenge-172/sgreen/blog.txt1
-rwxr-xr-xchallenge-172/sgreen/perl/ch-1.pl47
-rwxr-xr-xchallenge-172/sgreen/perl/ch-2.pl35
-rwxr-xr-xchallenge-172/sgreen/python/ch-1.py36
-rwxr-xr-xchallenge-172/sgreen/python/ch-2.py34
6 files changed, 156 insertions, 1 deletions
diff --git a/challenge-172/sgreen/README.md b/challenge-172/sgreen/README.md
index 512411c9b5..327abfe81e 100644
--- a/challenge-172/sgreen/README.md
+++ b/challenge-172/sgreen/README.md
@@ -1 +1,3 @@
-# The Weekly Challenge 170
+# The Weekly Challenge 172
+
+[Blog](https://dev.to/simongreennet/weekly-challenge-172-1h8i)
diff --git a/challenge-172/sgreen/blog.txt b/challenge-172/sgreen/blog.txt
new file mode 100644
index 0000000000..30f3a52cdd
--- /dev/null
+++ b/challenge-172/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-172-1h8i
diff --git a/challenge-172/sgreen/perl/ch-1.pl b/challenge-172/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..30fa1e424a
--- /dev/null
+++ b/challenge-172/sgreen/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'sum';
+use Algorithm::Combinatorics 'combinations';
+
+sub is_prime ($number) {
+ # Return true or false if the number is a prime
+ if ( $number < 2 ) {
+ return;
+ }
+
+ foreach my $i ( 2 .. sqrt($number) ) {
+ if ( $number % $i == 0 ) {
+ return;
+ }
+ }
+
+ # It's a prime
+ return 1;
+}
+
+sub main ( $m, $n ) {
+ # Retrieve a list of all prime numbers <= m
+ my @primes = ();
+ for ( my $i = $m ; $i > 1 ; $i-- ) {
+ push @primes, $i if is_prime($i);
+ }
+
+ # Go through each combination of n length, and see if we have a solution
+ my $iter = combinations( \@primes, $n );
+ while ( my $c = $iter->next ) {
+ if ( sum(@$c) == $m ) {
+ say join ', ', @$c;
+ return;
+ }
+ }
+
+ # It is possible that no solution is found
+ say 'No solution!';
+}
+
+main(@ARGV);
diff --git a/challenge-172/sgreen/perl/ch-2.pl b/challenge-172/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..d55ed5c7f5
--- /dev/null
+++ b/challenge-172/sgreen/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub get_value ( $l, $quart ) {
+ # Calculate the position of the value we require
+ my $pos = ($#$l) * $quart / 100;
+ my $i = int($pos);
+
+ if ( $i == $pos ) {
+ # It is a single value
+ return $l->[$i];
+ }
+
+ # We need the average of two numbers
+ return ( $l->[$i] + $l->[ $i + 1 ] ) / 2;
+}
+
+sub main (@l) {
+ # Sort the list
+ @l = sort { $a <=> $b } @l;
+ my @solution = ();
+
+ # Calculate the five values
+ foreach my $quart ( 0, 25, 50, 75, 100 ) {
+ push @solution, get_value( \@l, $quart );
+ }
+
+ say join ', ', @solution;
+}
+
+main(@ARGV);
diff --git a/challenge-172/sgreen/python/ch-1.py b/challenge-172/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..125ec64815
--- /dev/null
+++ b/challenge-172/sgreen/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import sys
+import math
+from itertools import combinations
+
+
+def is_prime(number):
+ '''Return true or false if the number is a prime'''
+ if number < 2:
+ return False
+
+ for i in range(2, int(math.sqrt(number)) + 1):
+ if number % i == 0:
+ return False
+
+ # It's a prime
+ return True
+
+
+def main(m, n):
+ # Retrieve a list of all prime numbers <= m
+ primes = [x for x in range(m, 1, -1) if is_prime(x)]
+
+ # Go through each combination of n length, and see if we have a solution
+ for l in combinations(primes, n):
+ if sum(l) == m:
+ print(l, sep=', ')
+ return
+
+ # It is possible that no solution is found
+ print('No solution!')
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]), int(sys.argv[2]))
diff --git a/challenge-172/sgreen/python/ch-2.py b/challenge-172/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..c9162d1604
--- /dev/null
+++ b/challenge-172/sgreen/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def get_value(l, quart):
+ # Calculate the position of the value we require
+ pos = (len(l)-1) * quart / 100
+ i = int(pos)
+
+ if i == pos:
+ # It is a single value
+ return l[i]
+
+ # We need the average of two numbers
+ val = (l[i] + l[i+1]) / 2
+ return int(val) if int(val) == val else val
+
+
+def main(l):
+
+ # Sort the list
+ l = sorted([int(x) for x in l])
+ solution = []
+
+ # Calculate the five values
+ for quart in range(0, 101, 25):
+ solution.append(get_value(l, quart))
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])