aboutsummaryrefslogtreecommitdiff
path: root/challenge-285/sgreen
diff options
context:
space:
mode:
authorConor Hoekstra <codereport@outlook.com>2024-09-09 16:34:38 -0400
committerConor Hoekstra <codereport@outlook.com>2024-09-09 16:34:38 -0400
commit2f3cf4f7cfa808d89e183b77f61dac80db4ab751 (patch)
tree9dcefb9d7860406bce98f0666cd99d7b608dd758 /challenge-285/sgreen
parent156e3186ef5b8f929e3589f8469463132e0fa996 (diff)
parent148ad068f27ef5bbdcac38b14685b2273b0d02ec (diff)
downloadperlweeklychallenge-club-2f3cf4f7cfa808d89e183b77f61dac80db4ab751.tar.gz
perlweeklychallenge-club-2f3cf4f7cfa808d89e183b77f61dac80db4ab751.tar.bz2
perlweeklychallenge-club-2f3cf4f7cfa808d89e183b77f61dac80db4ab751.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-285/sgreen')
-rw-r--r--challenge-285/sgreen/README.md4
-rw-r--r--challenge-285/sgreen/blog.txt1
-rwxr-xr-xchallenge-285/sgreen/perl/ch-1.pl40
-rwxr-xr-xchallenge-285/sgreen/perl/ch-2.pl36
-rwxr-xr-xchallenge-285/sgreen/python/ch-1.py35
-rwxr-xr-xchallenge-285/sgreen/python/ch-2.py30
-rwxr-xr-xchallenge-285/sgreen/python/test.py22
7 files changed, 166 insertions, 2 deletions
diff --git a/challenge-285/sgreen/README.md b/challenge-285/sgreen/README.md
index e5f731d3d7..9259df2b60 100644
--- a/challenge-285/sgreen/README.md
+++ b/challenge-285/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 284
+# The Weekly Challenge 285
-Blog: [The lucky sort](https://dev.to/simongreennet/the-lucky-sort-1648)
+Blog: [Making connections](https://dev.to/simongreennet/making-connections-13p8)
diff --git a/challenge-285/sgreen/blog.txt b/challenge-285/sgreen/blog.txt
new file mode 100644
index 0000000000..8d4f57a57a
--- /dev/null
+++ b/challenge-285/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/making-connections-13p8 \ No newline at end of file
diff --git a/challenge-285/sgreen/perl/ch-1.pl b/challenge-285/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..5b09b7a7e7
--- /dev/null
+++ b/challenge-285/sgreen/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub no_connection($routes) {
+ # Calculate a list of origins (first value) and destinations (second value)
+ my @origins = map { $_->[0] } @$routes;
+ my @destinations = map { $_->[1] } @$routes;
+
+ # Find route destinations that aren't also an origin
+ my @dead_ends = grep {
+ my $c = $_;
+ ! grep { $c eq $_ } @origins
+ } @destinations;
+
+ if ( $#dead_ends > 0 ) {
+ die "There are multiple routes with no outgoing connection\n";
+ }
+
+ if ( $#dead_ends == -1 ) {
+ die "All routes have an outgoing connection\n";
+ }
+
+ return $dead_ends[0];
+}
+
+sub main (@args) {
+ # Convert input into pairs. The first value is the start of route.
+ # The second value is the destination
+ my @routes = ();
+ for ( my $i = 0 ; $i <= $#args ; $i += 2 ) {
+ push @routes, [ $args[$i], $args[ $i + 1 ] ];
+ }
+ say no_connection( \@routes );
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-285/sgreen/perl/ch-2.pl b/challenge-285/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..e5776a4fb2
--- /dev/null
+++ b/challenge-285/sgreen/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub making_change( $remaining, $last_coin ) {
+ my $combinations = 0;
+
+ foreach my $coin ( 1, 5, 10, 25, 50 ) {
+ if ( $last_coin and $last_coin < $coin ) {
+ # We can't use a larger coin that the last one
+ next;
+ }
+
+ if ( $coin == $remaining ) {
+ # We have found a solution
+ $combinations++;
+ }
+
+ if ( $coin < $remaining ) {
+ # Call the function again, taking away the value of the coin
+ $combinations += making_change( $remaining - $coin, $coin );
+ }
+ }
+
+ # This value is returned upstream as we go
+ return $combinations;
+}
+
+sub main ($change) {
+ say making_change( $change, undef );
+}
+
+main( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-285/sgreen/python/ch-1.py b/challenge-285/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..f0cbf0030e
--- /dev/null
+++ b/challenge-285/sgreen/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def no_connection(routes: list) -> str:
+ # Calculate a list of origins (first value) and destinations (second value)
+ origins = [v[0] for v in routes]
+ destinations = [v[1] for v in routes]
+
+ # Find route destinations that aren't also an origin
+ dead_ends = [d for d in destinations if d not in origins]
+
+ if len(dead_ends) > 1:
+ raise ValueError(
+ 'There are multiple routes with no outgoing connection')
+
+ if len(dead_ends) == 0:
+ raise ValueError('All routes have an outgoing connection')
+
+ return dead_ends[0]
+
+
+def main():
+ # Convert input into pairs. The first value is the start of route.
+ # The second value is the destination
+ routes = []
+ for i in range(1, len(sys.argv), 2):
+ routes.append([sys.argv[i], sys.argv[i+1]])
+ result = no_connection(routes)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-285/sgreen/python/ch-2.py b/challenge-285/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..6ea25dabac
--- /dev/null
+++ b/challenge-285/sgreen/python/ch-2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def making_change(remaining: int, last_coin: int | None = None) -> int:
+ combinations = 0
+
+ for coin in [1, 5, 10, 25, 50]:
+ if last_coin and last_coin < coin:
+ # We can't use a larger coin that the last one
+ continue
+ if coin == remaining:
+ # We have found a solution
+ combinations += 1
+ if coin < remaining:
+ # Call the function again, taking away the value of the coin
+ combinations += making_change(remaining-coin, coin)
+
+ # This value is returned upstream as we go
+ return combinations
+
+
+def main():
+ result = making_change(int(sys.argv[1]))
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-285/sgreen/python/test.py b/challenge-285/sgreen/python/test.py
new file mode 100755
index 0000000000..5ac058a985
--- /dev/null
+++ b/challenge-285/sgreen/python/test.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(
+ ch_1.no_connection([['B', 'C'], ['D','B'], ['C','A']]), 'A'
+ )
+ self.assertEqual(ch_1.no_connection([['A', 'Z']]), 'Z')
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.making_change(9), 2)
+ self.assertEqual(ch_2.making_change(15), 6)
+ self.assertEqual(ch_2.making_change(100), 292)
+
+
+if __name__ == '__main__':
+ unittest.main()