diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-08 13:56:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-08 13:56:54 +0100 |
| commit | 02aa2cddcb560fe9843129749f849975f80c8c2d (patch) | |
| tree | 5e4ec2686185e31412cde9d5b8dd09cf367da6c7 | |
| parent | 61e2a29a7e3c0100cbf1560dab538a9fb8e78a66 (diff) | |
| parent | 2b419446ab484d93efadd0271caa060960fe08c8 (diff) | |
| download | perlweeklychallenge-club-02aa2cddcb560fe9843129749f849975f80c8c2d.tar.gz perlweeklychallenge-club-02aa2cddcb560fe9843129749f849975f80c8c2d.tar.bz2 perlweeklychallenge-club-02aa2cddcb560fe9843129749f849975f80c8c2d.zip | |
Merge pull request #8813 from packy/challenge-237
Challenge 237 solutions by Packy Anderson
| -rw-r--r-- | challenge-237/packy-anderson/README.md | 112 | ||||
| -rw-r--r-- | challenge-237/packy-anderson/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-237/packy-anderson/java/Ch1.java | 108 | ||||
| -rw-r--r-- | challenge-237/packy-anderson/java/Ch2.java | 121 | ||||
| -rwxr-xr-x | challenge-237/packy-anderson/perl/ch-1.pl | 97 | ||||
| -rwxr-xr-x | challenge-237/packy-anderson/perl/ch-2.pl | 72 | ||||
| -rwxr-xr-x | challenge-237/packy-anderson/python/ch-1.py | 98 | ||||
| -rwxr-xr-x | challenge-237/packy-anderson/python/ch-2.py | 71 | ||||
| -rwxr-xr-x | challenge-237/packy-anderson/raku/ch-1.raku | 106 | ||||
| -rwxr-xr-x | challenge-237/packy-anderson/raku/ch-2.raku | 68 |
10 files changed, 797 insertions, 57 deletions
diff --git a/challenge-237/packy-anderson/README.md b/challenge-237/packy-anderson/README.md index 54468a7f26..047c0f6bd7 100644 --- a/challenge-237/packy-anderson/README.md +++ b/challenge-237/packy-anderson/README.md @@ -8,16 +8,22 @@ Sample output ``` $ perl/ch-1.pl Example 1: -Input: @bills = (5, 5, 5, 10, 20) -Output: true +Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2 +Output: 16 + +The 3rd Tue of Apr 2024 is the 16th Example 2: -Input: @bills = (5, 5, 10, 10, 20) -Output: false +Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4 +Output: 9 + +The 2nd Thu of Oct 2025 is the 9th Example 3: -Input: @bills = (5, 5, 5, 20) -Output: true +Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 +Output: 0 + +There isn't a 5th Wed in Aug 2026 ``` * [Task 2](perl/ch-2.pl) @@ -26,32 +32,25 @@ Sample output ``` $ perl/ch-2.pl Example 1: -Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) -Output: 3 +Input: @ints = (1, 3, 5, 2, 1, 3, 1) +Output: 4 -Loops are as below: -[4 15 1 6 13 5 0] -[3 8 7 18 9 16 12 17 2] -[14 11 19 10] +One possible permutation: (2, 5, 1, 3, 3, 1, 1) +which returns 4 greatness as below: +nums[0] < perm[0] +nums[1] < perm[1] +nums[3] < perm[3] +nums[4] < perm[4] Example 2: -Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) -Output: 6 - -Loops are as below: -[0] -[1] -[13 9 14 17 18 15 5 8 2] -[7 11 4 6 10 16 3] -[12] -[19] - -Example 3: -Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) -Output: 1 +Input: @ints = (1, 2, 3, 4) +Output: 3 -Loop is as below: -[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +One possible permutation: (2, 3, 4, 1) +which returns 3 greatness as below: +nums[0] < perm[0] +nums[1] < perm[1] +nums[2] < perm[2] ``` ## Raku @@ -62,16 +61,22 @@ Sample output ``` $ raku/ch-1.raku Example 1: -Input: @bills = (5, 5, 5, 10, 20) -Output: true +Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2 +Output: 16 + +The 3rd Tuesday of 4 2024 is the 16th Example 2: -Input: @bills = (5, 5, 10, 10, 20) -Output: false +Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4 +Output: 9 + +The 2nd Thursday of 10 2025 is the 9th Example 3: -Input: @bills = (5, 5, 5, 20) -Output: true +Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 +Output: 0 + +There isn't a 5th Wednesday in 8 2026 ``` * [Task 2](raku/ch-2.raku) @@ -80,32 +85,25 @@ Sample output ``` $ raku/ch-2.raku Example 1: -Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) -Output: 3 +Input: @ints = (1, 3, 5, 2, 1, 3, 1) +Output: 4 -Loops are as below: -[4 15 1 6 13 5 0] -[3 8 7 18 9 16 12 17 2] -[14 11 19 10] +One possible permutation: (2, 5, 1, 3, 3, 1, 1) +which returns 4 greatness as below: +nums[0] < perm[0] +nums[1] < perm[1] +nums[3] < perm[3] +nums[4] < perm[4] Example 2: -Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) -Output: 6 - -Loops are as below: -[0] -[1] -[13 9 14 17 18 15 5 8 2] -[7 11 4 6 10 16 3] -[12] -[19] - -Example 3: -Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) -Output: 1 +Input: @ints = (1, 2, 3, 4) +Output: 3 -Loop is as below: -[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +One possible permutation: (2, 3, 4, 1) +which returns 3 greatness as below: +nums[0] < perm[0] +nums[1] < perm[1] +nums[2] < perm[2] ``` ## Guest Language: Python @@ -118,4 +116,4 @@ Loop is as below: ## Blog Post -[Perl Weekly Challenge: Exact Change and Array Loops](https://packy.dardan.com/2023/09/25/perl-weekly-challenge-exact-change-and-array-loops/) +[Perl Weekly Challenge: Minute by minute, that’s how you win it!](https://packy.dardan.com/2023/10/04/perl-weekly-challenge-minute-by-minute-thats-how-you-win-it/) diff --git a/challenge-237/packy-anderson/blog.txt b/challenge-237/packy-anderson/blog.txt new file mode 100644 index 0000000000..132d2d5fa4 --- /dev/null +++ b/challenge-237/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2023/10/04/perl-weekly-challenge-minute-by-minute-thats-how-you-win-it/
\ No newline at end of file diff --git a/challenge-237/packy-anderson/java/Ch1.java b/challenge-237/packy-anderson/java/Ch1.java new file mode 100644 index 0000000000..2a9ab8dc49 --- /dev/null +++ b/challenge-237/packy-anderson/java/Ch1.java @@ -0,0 +1,108 @@ +import java.util.Calendar; +import java.text.SimpleDateFormat; + +class seizeTheDay { + public int day; + public String description; + + public seizeTheDay( + int year, + int month, + int weekday_of_month, + int day_of_week + ) { + // object for the first day of the specified month + Calendar t = Calendar.getInstance(); + t.set(year, month - 1, 1); + + // find the FIRST instance of the desired day of the week + while (t.get(Calendar.DAY_OF_WEEK) != day_of_week+1) { + t.add(Calendar.DATE, 1); + } + + String month_str = + new SimpleDateFormat("MMM").format(t.getTime()); + String dow_str = + new SimpleDateFormat("EEE").format(t.getTime()); + int count = 1; + + // now roll forward through the month until the desired + // weekday of the month + while ( + // we're still in the desired month + t.get(Calendar.MONTH) == month - 1 + && + // we haven't reached the desired weekday of the month + count < weekday_of_month + ) { + t.add(Calendar.DATE, 7); + count++; + } + if (t.get(Calendar.MONTH) != month - 1) { + this.day = 0; + this.description = String.format( + "There isn't a %s %s in %s %d", + this.ord_suffix(weekday_of_month), + dow_str, + month_str, + year + ); + } + else { + this.day = t.get(Calendar.DATE); + this.description = String.format( + "The %s %s of %s %d is the %s", + this.ord_suffix(weekday_of_month), + dow_str, + month_str, + year, + this.ord_suffix(this.day) + ); + } + } + + private String ord_suffix(int num) { + // quick function to add an ordinal suffix + // to a number + if (num == 11 || num == 12 | num == 13) { + return num + "th"; + } + else { + switch (num % 10) { + case 1: return num + "st"; + case 2: return num + "nd"; + case 3: return num + "rd"; + default: return num + "th"; + } + } + } +} + +public class Ch1 { + public static void solution( + int year, + int month, + int weekday_of_month, + int day_of_week + ) { + String format = "Input: Year = %d, Month = %d, " + + "Weekday of month = %d, day of week = %d"; + System.out.println(String.format( + format, year, month, weekday_of_month, day_of_week + )); + seizeTheDay d = new seizeTheDay(year, month, weekday_of_month, day_of_week); + System.out.println(String.format("Output: %d", d.day)); + System.out.println(String.format("\n%s", d.description)); + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(2024, 4, 3, 2); + + System.out.println("\nExample 2:"); + solution(2025, 10, 2, 4); + + System.out.println("\nExample 3:"); + solution(2026, 8, 5, 3); + } +} diff --git a/challenge-237/packy-anderson/java/Ch2.java b/challenge-237/packy-anderson/java/Ch2.java new file mode 100644 index 0000000000..4768d8d7ec --- /dev/null +++ b/challenge-237/packy-anderson/java/Ch2.java @@ -0,0 +1,121 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +public class Ch2 { + public static String joined(int[] ints) { + // we're using it more than once, make it a method + return Arrays.stream(ints) + .mapToObj(String::valueOf) + .collect(Collectors.joining(", ")); + } + + public static int greatness(int[] nums, int[] perm) { + // determine the "greatness" of a permutation + // relative to the original array; accepts two + // arrays to do the comparison + int greatness_num = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < perm[i]) { + greatness_num++; + } + } + return greatness_num; + } + + public static int[] greatestPermutation(int[] nums) { + // first, count up how many of each num we have + HashMap<Integer, Integer> num_count = + new HashMap<Integer, Integer>(); + for (int i = 0; i < nums.length; i++) { + num_count.put( + nums[i], + num_count.getOrDefault(nums[i], 0) + 1 + ); + } + + // make a list of the available numbers + // to put in a permutation + List<Integer> available = + new ArrayList<>(num_count.keySet()); + Collections.sort(available); + + // now, build a permutation that maximizes "greatness" + List<Integer> perm = new ArrayList<>(); + for (Integer num : nums) { + // default to the smallest available number + int num_to_add = available.get(0); + for (int i = 0; i < available.size(); i++) { + int this_num = available.get(i); + if (num < this_num) { + num_to_add = this_num; + break; + } + } + perm.add(num_to_add); + + // decrement the count of that number available + num_count.put( + num_to_add, + num_count.get(num_to_add) - 1 + ); + + // if there are no more of that number, remove it + // from available list + if ( num_count.get(num_to_add) == 0 ) { + // filter array to not include $num + int size = available.size(); + for (int i = 1; i < size; i++) { + int this_num = available.get(i); + if (num_to_add == this_num) { + available.remove(i); + break; + } + } + } + } + + // because we built the permutations in a List, + // convert the list to an int array for return + int[] perm_return = new int[perm.size()]; + for (int i = 0; i < perm.size(); i++) { + perm_return[i] = perm.get(i); + } + return perm_return; + } + + public static void solution(int[] nums) { + System.out.println(String.format( + "Input: @nums = (%s)", joined(nums) + )); + int[] greatest = greatestPermutation(nums); + int greatness_num = greatness(nums, greatest); + System.out.println(String.format( + "Output: %d", greatness_num + )); + System.out.println(String.format( + "\nOne possible permutation: (%s)", joined(greatest) + )); + System.out.println(String.format( + "which returns %d greatness as below:", greatness_num + )); + for (int i = 0; i < nums.length; i++) { + if (nums[i] < greatest[i]) { + System.out.println(String.format( + "nums[%d] < perm[%d]", i, i + )); + } + } + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(new int[] {1, 3, 5, 2, 1, 3, 1}); + + System.out.println("\nExample 2:"); + solution(new int[] {1, 2, 3, 4}); + } +} diff --git a/challenge-237/packy-anderson/perl/ch-1.pl b/challenge-237/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..5f23f1792e --- /dev/null +++ b/challenge-237/packy-anderson/perl/ch-1.pl @@ -0,0 +1,97 @@ +#!/usr/bin/env perl +use v5.38; + +# let's use the core modules for date manipulation +use Time::Piece; +use Time::Seconds qw( ONE_DAY ); +use Lingua::EN::Inflexion qw( noun ); + +sub seizeTheDay { + my %params = @_; + # build the first day of the specified month + my $start = $params{year} . '-' . $params{month} . '-01'; + # create an object for that day + my $t = Time::Piece->strptime($start, "%Y-%m-%d") + ->truncate(to => 'day'); + # in Time::Piece->wday, 1 = Sun, 2 = Mon, 3 = Tue, but our + # input is 0 = Sun, 1 = Mon, 2 = Tue, so adjust our input + $params{day_of_week}++; + + # find the FIRST instance of the desired day of the week + while ( $t->wday != $params{day_of_week} ) { + $t += ONE_DAY; # add 1 day + } + + # take note of some values that won't change + # for our description + my $year = $t->year; + my $month = $t->month; + my $dow = $t->wdayname; + my $count = 1; + + my $ord_weekday_of_month = + noun($params{weekday_of_month})->ordinal(0); + + # now roll forward through the month until the desired + # weekday of the month + while ( + # we're still in the desired month + $t->mon == $params{month} + && + # we haven't reached the desired weekday of the month + $count < $params{weekday_of_month} + ) { + # add a week to the date + $t += ONE_DAY * 7; + # add to the weekday of the month count + $count++; + } + + # if we rolled out of the month, return an error condition + if ($t->mon != $params{month}) { + return 0, "There isn't a $ord_weekday_of_month $dow " + . "in $month $year"; + } + else { + # take note of what the day of the month is + my $day = $t->day_of_month; + my $ord_day_of_month = noun($day)->ordinal(0); + return $day, "The $ord_weekday_of_month $dow " + . "of $month $year is the $ord_day_of_month"; + } +} + +sub solution { + my %params = @_; + say 'Input: Year = ' . $params{year} + . ', Month = ' . $params{month} + . ', Weekday of month = ' . $params{weekday_of_month} + . ', day of week = ' . $params{day_of_week}; + my ($day, $desc) = seizeTheDay(%params); + say 'Output: ' . $day; + say "\n$desc"; +} + +say "Example 1:"; +solution( + year => 2024, + month => 4, + weekday_of_month => 3, + day_of_week => 2 +); + +say "\nExample 2:"; +solution( + year => 2025, + month => 10, + weekday_of_month => 2, + day_of_week => 4 +); + +say "\nExample 3:"; +solution( + year => 2026, + month => 8, + weekday_of_month => 5, + day_of_week => 3 +); diff --git a/challenge-237/packy-anderson/perl/ch-2.pl b/challenge-237/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..310082cd72 --- /dev/null +++ b/challenge-237/packy-anderson/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +use v5.38; + +sub greatness { + # determine the "greatness" of a permutation + # relative to the original array; accepts two + # array references to do the comparison + my ($nums, $perm) = @_; + my $greatness = 0; + foreach my $i ( 0 .. $#$nums ) { + $greatness++ if $nums->[$i] < $perm->[$i]; + } + return $greatness; +} + + +sub greatestPermutation { + my @nums = @_; + # first, count up how many of each num we have + my %num_count; + foreach my $num ( @nums ) { + $num_count{$num}++; + } + + # now, build a permutation that maximizes "greatness" + my @perm; + my @available = sort keys %num_count; # do the sort once + foreach my $num ( @nums ) { + # default to the smallest available number + my $num_to_add = $available[0]; + + AVAIL: foreach my $avail ( @available ) { + if ( $avail > $num ) { + # make this the number we're putting in the permutation + $num_to_add = $avail; + last AVAIL; + } + } + push @perm, $num_to_add; + + # decrement the count of that number available + $num_count{$num_to_add}--; + + # if there are no more of that number, remove it + # from @available array + if ( $num_count{$num_to_add} == 0 ) { + # filter array to not include $num + @available = grep { $_ != $num_to_add } @available; + } + } + + return @perm; +} + +sub solution { + my @nums = @_; + say 'Input: @ints = (' . join(', ', @nums) . ')'; + my @greatest = greatestPermutation(@nums); + my $greatness = greatness(\@nums, \@greatest); + say 'Output: ' . $greatness; + say "\nOne possible permutation: (" . join(', ', @greatest) . ')'; + say "which returns $greatness greatness as below:"; + foreach my $i ( 0 .. $#nums ) { + say "nums[$i] < perm[$i]" if $nums[$i] < $greatest[$i]; + } +} + +say "Example 1:"; +solution(1, 3, 5, 2, 1, 3, 1); + +say "\nExample 2:"; +solution(1, 2, 3, 4);
\ No newline at end of file diff --git a/challenge-237/packy-anderson/python/ch-1.py b/challenge-237/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..44fd89e63b --- /dev/null +++ b/challenge-237/packy-anderson/python/ch-1.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python + +from datetime import date, timedelta +from num2words import num2words + +def seizeTheDay(year, month, weekday_of_month, day_of_week): + """ + Function to determine, given a year, month, weekday of + month and day of week, whether such a date exists and, + if so, what day of the month it is. + """ + # object for the first day of the specified month + t = date(year, month, 1) + + # datetime.date.isoweekday returns 1 = Mon, 2 = Tue, etc., + # which matches our input, so no adjustment is needed + + # find the FIRST instance of the desired day of the week + while ( t.isoweekday() != day_of_week ): + t += timedelta(days = 1) # add 1 day + + # take note of some values that won't change + # for our description + month_name = t.strftime('%b') + dow = t.strftime('%a') + count = 1 + + ord_weekday_of_month = num2words( + weekday_of_month, to="ordinal_num" + ) + + # now roll forward through the month until the desired + # weekday of the month + while ( + # we're still in the desired month + t.month == month + and + # we haven't reached the desired weekday of the month + count < weekday_of_month + ): + # add a week to the date + t += timedelta(days = 7) + # add to the weekday of the month count + count += 1 + + # if we rolled out of the month, return an error condition + if (t.month != month): + return( + 0, + f"There isn't a {ord_weekday_of_month} {dow} " + + f"in {month_name} {year}" + ) + else: + # take note of what the day of the month is + day = t.day + ord_day_of_month = num2words(day, to="ordinal_num") + return( + day, + f"The {ord_weekday_of_month} {dow} " + + f"of {month_name} {year} is the {ord_day_of_month}" + ) + +def solution(year, month, weekday_of_month, day_of_week): + print( + f'Input: Year = {year}, ' + + f'Month = {month}, ' + + f'Weekday of month = {weekday_of_month}, ' + + f'day of week = {day_of_week}' + ) + day, desc = seizeTheDay( + year, month, weekday_of_month, day_of_week + ) + print(f'Output: {day}\n\n{desc}') + + +print('Example 1:') +solution( + year = 2024, + month = 4, + weekday_of_month = 3, + day_of_week = 2 +) + +print('\nExample 2:') +solution( + year = 2025, + month = 10, + weekday_of_month = 2, + day_of_week = 4 +) + +print('\nExample 3:') +solution( + year = 2026, + month = 8, + weekday_of_month = 5, + day_of_week = 3 +) diff --git a/challenge-237/packy-anderson/python/ch-2.py b/challenge-237/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..afceb6aa65 --- /dev/null +++ b/challenge-237/packy-anderson/python/ch-2.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +def greatness(nums, perm): + """ + Function to enumerate the greatness of + the list perm relative to the list nums + """ + greatness_num = 0 + for i in range(0, len(nums) - 1): + if nums[i] < perm[i]: + greatness_num += 1 + return greatness_num + +def greatestPermutation(nums): + """ + Function to generate a permutation of the list nums + which has the largest relative "greatness" to nums + """ + + # first, count up how many of each num we have + num_count = {} + for num in nums: + num_count[num] = num_count.get(num, 0) + 1 + + # now, build a permutation that maximizes "greatness" + perm = [] + available = sorted(num_count.keys()) # only sort once + for num in nums: + # default to the smallest available number + num_to_add = available[0] + + # but now look for the smallest available number + # that's GREATER than the current number + for avail in available: + if avail > num: + num_to_add = avail + break + + # add num_to_add to the permutation + perm.append(num_to_add) + + # decrement its count in num_count + num_count[num_to_add] -= 1 + + # if there are no more, remove it from available + if num_count[num_to_add] == 0: + available = [ + x for x in available if x != num_to_add + ] + + return perm + + +def solution(nums): + as_list = ', '.join(map(lambda i: str(i), nums)) + print(f'Input: @nums = ({as_list})') + greatest = greatestPermutation(nums) + greatness_num = greatness(nums, greatest) + as_list = ', '.join(map(lambda i: str(i), greatest)) + print(f'Output: {greatness_num}\n') + print(f'One possible permutation: ({as_list})\n' + + 'which returns $greatness greatness as below:') + for i in range(0, len(nums) - 1): + if nums[i] < greatest[i]: + print(f'nums[{i}] < perm[{i}]') + +print('Example 1:') +solution([1, 3, 5, 2, 1, 3, 1]) + +print('\nExample 2:') +solution([1, 2, 3, 4])
\ No newline at end of file diff --git a/challenge-237/packy-anderson/raku/ch-1.raku b/challenge-237/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..f4590dfc13 --- /dev/null +++ b/challenge-237/packy-anderson/raku/ch-1.raku @@ -0,0 +1,106 @@ +#!/usr/bin/env raku +use v6; + +use Date::Names; +use Lingua::EN::Numbers; # for ordinal-digit() + +sub seizeTheDay( + Int :$year, + Int :$month, + Int :$weekday_of_month, + Int :$day_of_week +) { + # object for the first day of the specified month + my $t = Date.new($year, $month, 1); + + # in Date.day-of-week, 0 = Sun, 1 = Mon, 2 = Tue, + # which matches our input, so no adjustment is needed + + # find the FIRST instance of the desired day of the week + while ( $t.day-of-week != $day_of_week ) { + $t++; # add 1 day + } + + # instantiate a Date::Names object + my $dn = Date::Names.new; + + # take note of some values that won't change + # for our description + my $month_name = $dn.mon($t.month); + my $dow = $dn.dow($t.day-of-week); + my $count = 1; + + my $ord_weekday_of_month = ordinal-digit($weekday_of_month); + + # now roll forward through the month until the desired + # weekday of the month + while ( + # we're still in the desired month + $t.month == $month + && + # we haven't reached the desired weekday of the month + $count < $weekday_of_month + ) { + # add a week to the date + $t += 7; + # add to the weekday of the month count + $count++; + } + + # if we rolled out of the month, return an error condition + if ($t.month != $month) { + return 0, "There isn't a $ord_weekday_of_month $dow " + ~ "in $month $year"; + } + else { + # take note of what the day of the month is + my $day = $t.day; + my $ord_day_of_month = ordinal-digit($day); + return $day, "The $ord_weekday_of_month $dow " + ~ "of $month $year is the $ord_day_of_month"; + } +} + +sub solution( + Int :$year, + Int :$month, + Int :$weekday_of_month, + Int :$day_of_week +) { + say 'Input: Year = ' ~ $year + ~ ', Month = ' ~ $month + ~ ', Weekday of month = ' ~ $weekday_of_month + ~ ', day of week = ' ~ $day_of_week; + my ($day, $desc) = seizeTheDay( + year => $year, + month => $month, + weekday_of_month => $weekday_of_month, + day_of_week => $day_of_week + ); + say 'Output: ' ~ $day; + say "\n$desc"; +} + +say "Example 1:"; +solution( + year => 2024, + month => 4, + weekday_of_month => 3, + day_of_week => 2 +); + +say "\nExample 2:"; +solution( + year => 2025, + month => 10, + weekday_of_month => 2, + day_of_week => 4 +); + +say "\nExample 3:"; +solution( + year => 2026, + month => 8, + weekday_of_month => 5, + day_of_week => 3 +); diff --git a/challenge-237/packy-anderson/raku/ch-2.raku b/challenge-237/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..268641c95a --- /dev/null +++ b/challenge-237/packy-anderson/raku/ch-2.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +use v6; + +sub greatness(@nums, @perm) { + # determine the "greatness" of a permutation + # relative to the original array; accepts two + # arrays to do the comparison + my $greatness = 0; + for 0 .. @nums.elems - 1 -> $i { + $greatness++ if @nums[$i] < @perm[$i]; + } + return $greatness; +} + +sub greatestPermutation(@nums) { + # first, count up how many of each num we have + my %num_count; + for @nums -> $num { + %num_count{$num}++; + } + + # now, build a permutation that maximizes "greatness" + my @perm; + my @available = %num_count.keys().sort(); # do the sort once + for @nums -> $num { + # default to the smallest available number + my $num_to_add = @available[0]; + + AVAIL: for @available -> $avail { + if ( $avail > $num ) { + # make this the number we're putting in the permutation + $num_to_add = $avail; + last AVAIL; + } + } + @perm.push($num_to_add); + + # decrement the count of that number available + %num_count{$num_to_add}--; + + # if there are no more of that number, remove it + # from @available array + if ( %num_count{$num_to_add} == 0 ) { + # filter array to not include $num + @available = @available.grep( { $_ != $num_to_add } ); + } + } + + return @perm; +} + +sub solution(*@nums) { + say 'Input: @ints = (' ~ @nums.join(', ') ~ ')'; + my @greatest = greatestPermutation(@nums); + my $greatness = greatness(@nums, @greatest); + say 'Output: ' ~ $greatness; + say "\nOne possible permutation: (" ~ @greatest.join(', ') ~ ')'; + say "which returns $greatness greatness as below:"; + for 0 .. @nums.elems - 1 -> $i { + say "nums[$i] < perm[$i]" if @nums[$i] < @greatest[$i]; + } +} + +say "Example 1:"; +solution(1, 3, 5, 2, 1, 3, 1); + +say "\nExample 2:"; +solution(1, 2, 3, 4);
\ No newline at end of file |
