aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-02 08:06:40 +0100
committerGitHub <noreply@github.com>2023-10-02 08:06:40 +0100
commit6fb071a270b4eef7096fe7a9d73f3937747ead15 (patch)
tree96f3088c2b5169f6841c700eb477f26ec5f6b9fd
parent2ffd1d11591aab513cbc6883646521135f7840eb (diff)
parent67e6e09433423467b69c4e6e117e9e18724a7aa7 (diff)
downloadperlweeklychallenge-club-6fb071a270b4eef7096fe7a9d73f3937747ead15.tar.gz
perlweeklychallenge-club-6fb071a270b4eef7096fe7a9d73f3937747ead15.tar.bz2
perlweeklychallenge-club-6fb071a270b4eef7096fe7a9d73f3937747ead15.zip
Merge pull request #8797 from simongreen-net/master
Simon's solution to challenge 236
-rw-r--r--challenge-236/sgreen/README.md4
-rw-r--r--challenge-236/sgreen/blog.txt1
-rwxr-xr-xchallenge-236/sgreen/perl/ch-1.pl49
-rwxr-xr-xchallenge-236/sgreen/perl/ch-2.pl42
-rwxr-xr-xchallenge-236/sgreen/python/ch-1.py42
-rwxr-xr-xchallenge-236/sgreen/python/ch-2.py38
6 files changed, 174 insertions, 2 deletions
diff --git a/challenge-236/sgreen/README.md b/challenge-236/sgreen/README.md
index f06edcdb27..c3256674bf 100644
--- a/challenge-236/sgreen/README.md
+++ b/challenge-236/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 235
+# The Weekly Challenge 236
-Blog: [Adding and removing integers](https://dev.to/simongreennet/adding-and-removing-integers-3i4g)
+Blog: [Juicy loops](https://dev.to/simongreennet/juicy-loops-2di2)
diff --git a/challenge-236/sgreen/blog.txt b/challenge-236/sgreen/blog.txt
new file mode 100644
index 0000000000..d76a60ea88
--- /dev/null
+++ b/challenge-236/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/juicy-loops-2di2 \ No newline at end of file
diff --git a/challenge-236/sgreen/perl/ch-1.pl b/challenge-236/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..246983d32d
--- /dev/null
+++ b/challenge-236/sgreen/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'min';
+
+sub main (@notes) {
+ # Store what is in the wallet, key is the dollar value, value is the
+ # number of notes we have
+ my %wallet = ();
+
+ C: foreach my $customer_note (@notes) {
+ # Add the amount the person has given us
+ $wallet{$customer_note}++;
+
+ if ( $customer_note == 5 ) {
+ # No change required :)
+ next;
+ }
+
+ my $change = $customer_note - 5;
+
+ # We try the bigger notes first
+ foreach my $wallet_note ( sort { $b <=> $a } keys %wallet ) {
+ my $note_count =
+ min( $wallet{$wallet_note}, int( $change / $wallet_note ) );
+ if ( $note_count > 0 ) {
+ # Take the note from the wallet, and reduce the change still
+ # to give
+ $wallet{$wallet_note} -= $note_count;
+ $change -= $wallet_note * $note_count;
+
+ if ( $change == 0 ) {
+ next C;
+ }
+ }
+ }
+
+ say 'false';
+ return;
+ }
+
+ say 'true';
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-236/sgreen/perl/ch-2.pl b/challenge-236/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..db13ccb43d
--- /dev/null
+++ b/challenge-236/sgreen/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'any';
+
+sub main (@ints) {
+ # Record all the loops we find
+ my $loops = 0;
+
+ F: foreach my $first ( 0 .. $#ints ) {
+ my @loop = ();
+ my $pos = $first;
+
+ while ( $pos >= 0 and $pos <= $#ints and defined( $ints[$pos] ) ) {
+ push @loop, $pos;
+
+ # What is the next number
+ my $next_pos = $ints[$pos];
+
+ # Mark this position as used
+ $ints[$pos] = undef;
+
+ if ( any { $next_pos == $_ } @loop ) {
+ # We have a loop
+ ++$loops;
+ next F;
+ }
+
+ # Continue with the next position
+ $pos = $next_pos;
+ }
+ }
+
+ # Print the loops we found
+ say $loops;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-236/sgreen/python/ch-1.py b/challenge-236/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..80ff481963
--- /dev/null
+++ b/challenge-236/sgreen/python/ch-1.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(notes):
+ # Store what is in the wallet, key is the dollar value, value is the
+ # number of notes we have
+ wallet = {}
+
+ for customer_note in notes:
+ # Add the amount the person has given us
+ wallet[customer_note] = wallet.get(customer_note, 0) + 1
+
+ if customer_note == 5:
+ # No change required :)
+ continue
+
+ change = customer_note - 5
+
+ # We try the bigger notes first
+ for wallet_note in sorted(wallet, reverse=True):
+ note_count = min(wallet[wallet_note], change // wallet_note)
+ if note_count > 0:
+ # Take the note from the wallet, and reduce the change still
+ # to give
+ wallet[wallet_note] -= note_count
+ change -= wallet_note * note_count
+
+ if change == 0:
+ break
+ else:
+ print('false')
+ return
+
+ print('true')
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)
diff --git a/challenge-236/sgreen/python/ch-2.py b/challenge-236/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..c0b0911a80
--- /dev/null
+++ b/challenge-236/sgreen/python/ch-2.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(ints):
+ # Record all the loops we find
+ loops = 0
+
+ for first in range(len(ints)):
+ loop = []
+ pos = first
+
+ while pos >= 0 and pos < len(ints) and ints[pos] is not None:
+ loop.append(pos)
+
+ # What is the next number
+ next_pos = ints[pos]
+
+ # Mark this position as used
+ ints[pos] = None
+
+ if next_pos in loop:
+ # We have a loop
+ loops += 1
+ break
+
+ # Continue with the next position
+ pos = next_pos
+
+ # Print the loops we found
+ print(loops)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)