diff options
| -rw-r--r-- | challenge-237/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-237/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-237/sgreen/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-237/sgreen/perl/ch-2.pl | 32 | ||||
| -rwxr-xr-x | challenge-237/sgreen/python/ch-1.py | 35 | ||||
| -rwxr-xr-x | challenge-237/sgreen/python/ch-2.py | 29 |
6 files changed, 134 insertions, 2 deletions
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) |
