aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-11-25 23:42:34 +1100
committerSimon Green <mail@simon.green>2021-11-25 23:42:34 +1100
commit1eaf078bb734a5160b8b57ac8e491a91b4cf710b (patch)
tree3c1f13a932e488ed06fbc8ac42a5a20aee4d0e6e
parente57e8ba97ca974deeadbd7137390e99a38d8304d (diff)
downloadperlweeklychallenge-club-1eaf078bb734a5160b8b57ac8e491a91b4cf710b.tar.gz
perlweeklychallenge-club-1eaf078bb734a5160b8b57ac8e491a91b4cf710b.tar.bz2
perlweeklychallenge-club-1eaf078bb734a5160b8b57ac8e491a91b4cf710b.zip
sgreen solutions to challenge 140
-rw-r--r--challenge-140/sgreen/README.md4
-rw-r--r--challenge-140/sgreen/blog.txt1
-rwxr-xr-xchallenge-140/sgreen/perl/ch-1.pl60
-rwxr-xr-xchallenge-140/sgreen/perl/ch-2.pl30
-rwxr-xr-xchallenge-140/sgreen/python/ch-1.py46
-rwxr-xr-xchallenge-140/sgreen/python/ch-2.py25
6 files changed, 164 insertions, 2 deletions
diff --git a/challenge-140/sgreen/README.md b/challenge-140/sgreen/README.md
index 1fc7e36add..c691a6f2d0 100644
--- a/challenge-140/sgreen/README.md
+++ b/challenge-140/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 139
+# The Weekly Challenge 140
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-139-2ja1)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-140-a50)
diff --git a/challenge-140/sgreen/blog.txt b/challenge-140/sgreen/blog.txt
new file mode 100644
index 0000000000..5e0cbd0fdb
--- /dev/null
+++ b/challenge-140/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-140-a50
diff --git a/challenge-140/sgreen/perl/ch-1.pl b/challenge-140/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..847f946fa0
--- /dev/null
+++ b/challenge-140/sgreen/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+package BinaryDecimal;
+use overload '+' => '_add';
+
+sub new {
+ my ( $class, $value ) = @_;
+
+ # Check we were supplied with a binary string
+ if ( $value !~ /^[01]+$/ ) {
+ die "The value $value is not binary\n";
+ }
+ my $self = { value => $value };
+ bless $self, $class;
+ return $self;
+}
+
+sub _add {
+ my ( $self, $other ) = @_;
+ my $value1 = $self->{value};
+ my $value2 = $other->{value};
+ my $result = 0;
+ my $power = 0;
+ my $carry = 0;
+
+ while ( $carry or $value1 or $value2 ) {
+ my $digit = $value1 % 10 + $value2 % 10 + $carry;
+ if ( $digit >= 2 ) {
+ $carry = 1;
+ $digit -= 2;
+ }
+ else {
+ $carry = 0;
+ }
+
+ $result += 10**$power
+ if $digit == 1;
+
+ $value1 = int( $value1 / 10 );
+ $value2 = int( $value2 / 10 );
+ $power++;
+ }
+
+ return $result;
+}
+
+package main;
+
+sub main {
+ my $value1 = BinaryDecimal->new(shift);
+ my $value2 = BinaryDecimal->new(shift);
+ say $value1 + $value2;
+}
+
+main(@ARGV);
+
diff --git a/challenge-140/sgreen/perl/ch-2.pl b/challenge-140/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..b016c95a72
--- /dev/null
+++ b/challenge-140/sgreen/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use List::Util 'min';
+
+sub main {
+ my ( $i, $j, $k ) = @_;
+ my @numbers = ();
+
+ foreach my $m ( 1 .. $i ) {
+ # Calculate the number of products required. If we already have
+ # $k values, then we only need to calculate values whose product
+ # is smaller, otherwise the lesser of $j or $k
+ my $max = @numbers >= $k ? int( $numbers[ $k - 1 ] / $m ) : min( $j, $k );
+
+ # Hack if the $i > $k[-1]
+ last if $max == 0;
+
+ # Add to the array, sort it, trim it
+ push @numbers, map { $_ * $m } ( 1 .. $max );
+ @numbers = sort { $a <=> $b } @numbers;
+ splice( @numbers, $k );
+ }
+
+ say $numbers[-1];
+}
+
+main(@ARGV);
diff --git a/challenge-140/sgreen/python/ch-1.py b/challenge-140/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..6309e048bd
--- /dev/null
+++ b/challenge-140/sgreen/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+import re
+import sys
+
+
+class BinaryDecimal:
+ def __init__(self, value):
+ # Check we were supplied with a binary string
+ if not re.match(r'^[01]+$', value):
+ raise Exception(f'The value {value} is not binary')
+ self.value = int(value)
+
+ def __add__(self, other):
+ value1 = self.value
+ value2 = other.value
+ result = 0
+ power = 0
+ carry = 0
+
+ while (carry or value1 or value2):
+ digit = value1 % 10 + value2 % 10 + carry
+ if digit >= 2:
+ carry = 1
+ digit -= 2
+ else:
+ carry = 0
+
+ if digit == 1:
+ result += 10 ** power
+
+ value1 = int(value1 / 10)
+ value2 = int(value2 / 10)
+ power += 1
+
+ return result
+
+
+def main(inputs):
+ value1 = BinaryDecimal(inputs[0])
+ value2 = BinaryDecimal(inputs[1])
+ print(value1 + value2)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-140/sgreen/python/ch-2.py b/challenge-140/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d27ce9f168
--- /dev/null
+++ b/challenge-140/sgreen/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(inputs):
+ i = int(inputs[0])
+ j = int(inputs[1])
+ k = int(inputs[2])
+ numbers = []
+
+ # Ensure we don't do something stupid
+ if (k > i * j):
+ raise Exception('Not enough elements')
+
+ for m in range(1, i+1):
+ for n in range(1, j+1):
+ numbers.append(m*n)
+
+ numbers.sort()
+ print(numbers[k - 1])
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])