From 81695c46183ba0154c05412705e0e85a632d32a0 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 2 Oct 2023 17:25:37 +1100 Subject: Simon's solution to challenge 236 --- challenge-236/sgreen/README.md | 4 +-- challenge-236/sgreen/blog.txt | 1 + challenge-236/sgreen/perl/ch-1.pl | 49 +++++++++++++++++++++++++++++++++++++ challenge-236/sgreen/perl/ch-2.pl | 42 +++++++++++++++++++++++++++++++ challenge-236/sgreen/python/ch-1.py | 42 +++++++++++++++++++++++++++++++ challenge-236/sgreen/python/ch-2.py | 38 ++++++++++++++++++++++++++++ 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 challenge-236/sgreen/blog.txt create mode 100755 challenge-236/sgreen/perl/ch-1.pl create mode 100755 challenge-236/sgreen/perl/ch-2.pl create mode 100755 challenge-236/sgreen/python/ch-1.py create mode 100755 challenge-236/sgreen/python/ch-2.py 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) -- cgit From 79ea77999ba709f30b32931b73e8520202ba78b9 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 8 Oct 2023 23:02:48 +1100 Subject: Simon's solution to challenge 237 --- challenge-237/sgreen/README.md | 4 ++-- challenge-237/sgreen/blog.txt | 1 + challenge-237/sgreen/perl/ch-1.pl | 35 +++++++++++++++++++++++++++++++++++ challenge-237/sgreen/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ challenge-237/sgreen/python/ch-1.py | 35 +++++++++++++++++++++++++++++++++++ challenge-237/sgreen/python/ch-2.py | 29 +++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 challenge-237/sgreen/blog.txt create mode 100755 challenge-237/sgreen/perl/ch-1.pl create mode 100755 challenge-237/sgreen/perl/ch-2.pl create mode 100755 challenge-237/sgreen/python/ch-1.py create mode 100755 challenge-237/sgreen/python/ch-2.py diff --git a/challenge-237/sgreen/README.md b/challenge-237/sgreen/README.md index f06edcdb27..8aa6cc5481 100644 --- a/challenge-237/sgreen/README.md +++ b/challenge-237/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 235 +# The Weekly Challenge 237 -Blog: [Adding and removing integers](https://dev.to/simongreennet/adding-and-removing-integers-3i4g) +Blog: [Seize the greatness](https://dev.to/simongreennet/seize-the-greatness-38n0) diff --git a/challenge-237/sgreen/blog.txt b/challenge-237/sgreen/blog.txt new file mode 100644 index 0000000000..2ef81294f5 --- /dev/null +++ b/challenge-237/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/seize-the-greatness-38n0 \ No newline at end of file diff --git a/challenge-237/sgreen/perl/ch-1.pl b/challenge-237/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..03a4f5674c --- /dev/null +++ b/challenge-237/sgreen/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Date::Calc qw(Day_of_Week Days_in_Month); + +sub main ( $year, $month, $week, $dofw ) { + # Day of the week for the first of the month (1 - Monday, 7 - Sunday) + my $first_weekday = Day_of_Week( $year, $month, 1 ); + + # To get the first day of week we take the diff between the two values. If + # it is negative, add 7 days + my $day_of_month = 1 + $dofw - $first_weekday; + if ( $day_of_month < 1 ) { + $day_of_month += 7; + } + + # Now add the weeks + $day_of_month += ( $week - 1 ) * 7; + + if ( $day_of_month > Days_in_Month( $year, $month ) ) { + # This date does not exist + say 0; + } + else { + # Print the day of month + say $day_of_month; + } + +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-237/sgreen/perl/ch-2.pl b/challenge-237/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..1a5428cef8 --- /dev/null +++ b/challenge-237/sgreen/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(min max); +use List::MoreUtils 'firstidx'; + +sub main (@ints) { + my @sorted_ints = sort { $b <=> $a } @ints; + my $count = 0; + + foreach my $i (@sorted_ints) { + # If there isn't a solution, exit the loop + if (min(@ints) >= $i) { + last; + } + + # Find the position of the maximum value < i, and delete it + my $m = max(grep {$_ < $i} @ints); + my $idx = firstidx { $_ == $m } @ints; + splice(@ints, $idx, 1); + + ++$count; + } + + say $count; +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-237/sgreen/python/ch-1.py b/challenge-237/sgreen/python/ch-1.py new file mode 100755 index 0000000000..3c13419a0b --- /dev/null +++ b/challenge-237/sgreen/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +import sys +from datetime import date, timedelta + + +def main(year, month, week, dofw): + # Get the first of the month + dte = date(year, month, 1) + + # Day of the week for the first of the month (1 - Monday, 7 - Sunday) + first_weekday = dte.isoweekday() + + # To get the first day of week we take the diff between the two values. If + # it is negative, add 7 days + add_days = dofw - first_weekday + if add_days < 0: + add_days += 7 + + # Now add the weeks + add_days += (week-1) * 7 + + new_date = dte + timedelta(days=add_days) + if new_date.month != dte.month or new_date.year != dte.year: + # This date does not exist + print(0) + else: + # Print the day of month + print(new_date.day) + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(*array) diff --git a/challenge-237/sgreen/python/ch-2.py b/challenge-237/sgreen/python/ch-2.py new file mode 100755 index 0000000000..51dbdc3f26 --- /dev/null +++ b/challenge-237/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def main(ints): + sorted_ints = sorted(ints, reverse=True) + count = 0 + + for i in sorted_ints: + # If there isn't a solution, exit the loop + if min(ints) >= i: + break + + # Find the position of the maximum value < i, and delete it + m = max(j for j in ints if j < i) + idx = ints.index(m) + del ints[idx] + + count += 1 + + # Print the solution + print(count) + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) -- cgit