diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-08 19:14:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-08 19:14:58 +0100 |
| commit | b96cb50042c3641c0bdb54bd72ecb8604f9f53ee (patch) | |
| tree | b2d99f99ccf6db9d0dd1d42594b9d14c2eb9f073 | |
| parent | 9202ef5fc10f3b7cec88ca58fd0d7cecc2d88e03 (diff) | |
| parent | 905f0a61d0a14014b087a85e67e537333d675ff3 (diff) | |
| download | perlweeklychallenge-club-b96cb50042c3641c0bdb54bd72ecb8604f9f53ee.tar.gz perlweeklychallenge-club-b96cb50042c3641c0bdb54bd72ecb8604f9f53ee.tar.bz2 perlweeklychallenge-club-b96cb50042c3641c0bdb54bd72ecb8604f9f53ee.zip | |
Merge pull request #8825 from 0rir/237
237
| -rw-r--r-- | challenge-236/0rir/raku/ch-1.raku | 87 | ||||
| -rw-r--r-- | challenge-236/0rir/raku/ch-2.raku | 62 | ||||
| -rw-r--r-- | challenge-237/0rir/raku/ch-1.raku | 102 | ||||
| -rw-r--r-- | challenge-237/0rir/raku/ch-2.raku | 84 |
4 files changed, 335 insertions, 0 deletions
diff --git a/challenge-236/0rir/raku/ch-1.raku b/challenge-236/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..847210a824 --- /dev/null +++ b/challenge-236/0rir/raku/ch-1.raku @@ -0,0 +1,87 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # π¦ β
βͺβ©βββ β‘ β’ Β«β€ Β» β΄ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +236-1: Exact Change Submitted by: Mohammad S Anwar +You are asked to sell juice each costs $5. You are given an array of bills. You can only sell ONE juice to each customer but make sure you return exact change back. You only have $5, $10 and $20 notes. You do not have any change in hand at first. + +Write a script to find out if it is possible to sell to each customers with correct change. + +Example 1 +Input: @bills = (5, 5, 5, 10, 20) +Output: true + +From the first 3 customers, we collect three $5 bills in order. +From the fourth customer, we collect a $10 bill and give back a $5. +From the fifth customer, we give a $10 bill and a $5 bill. +Since all customers got correct change, we output true. +Example 2 +Input: @bills = (5, 5, 10, 10, 20) +Output: false + +From the first two customers in order, we collect two $5 bills. +For the next two customers in order, we collect a $10 bill and give back a $5 bill. +For the last customer, we can not give the change of $15 back because we only have two $10 bills. +Since not every customer received the correct change, the answer is false. +Example 3 +Input: @bills = (5, 5, 5, 20) +Output: true + +=end comment + +my @Test = + (5, 5, 5, 20), True, + (5, 5, 10, 10, 20), False, + (5, 5, 5, 10, 20), True, + + (10,), False, + (20,), False, + (5,5,5,5,5,5,10,20,20,20), False, + (5,5,5,5,5,5,5,10,20,20), True, + (5,10), True, + (5,5,5,5,5,5,10,20,20), True, + (5,5,5,5,5,5,10,20,10,10), True, +; +plan @Test Γ· 2; + + +# Return True if correct change can be given all customers, else False. +# We have a juice bar and no cash on hand (cash only exists as 5s, 10s, +# and 20s. All sales are for 1 juice @ 5. The @a array is an ordered +# sequence of thπ bills the customers present. + +sub func( @a --> Bool ) { # ? + my UInt %h = 5 => 0, 10 => 0, 20 => 0; + for @a { + when 5 { ++ %h<5> } + when 10 { + return False unless %h<5> > 0; + -- %h<5>; + ++ %h<10>; + } + when 20 { + return False if %h<5> == 0; + when %h<10> > 0 { + -- %h<5>; + -- %h<10>; + } + return False unless %h<5> > 2; + %h<5> -= 3; + ++ %h<20>; + } + } + True; +} + +for @Test -> @in, $exp { + is func(@in), $exp, "$exp <- @in.raku()"; +} +my @bill = (5,5,5,5,5,5,10,20,10,10); +say "\nInput: @bill = @bill.raku()\nOutput: &func(@bill)"; + +done-testing; +exit; + diff --git a/challenge-236/0rir/raku/ch-2.raku b/challenge-236/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..c9061ab357 --- /dev/null +++ b/challenge-236/0rir/raku/ch-2.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # π¦ β
βͺβ©βββ β‘ β’ Β«β€ Β» β΄ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +236-2: Array Loops +Submitted by: Mark Anderson +You are given an array of unique integers. + +Write a script to determine how many loops are in the given array. + + +To determine a loop: Start at an index and take the number at array[index] and then proceed to that index and continue this until you end up at the starting index. + + +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 + +To determine the 1st loop, start at index 0, the number at that index is 4, proceed to index 4, the number at that index is 15, proceed to index 15 and so on until you're back at index 0. + +Loops are as below: +[4 15 1 6 13 5 0] +[3 8 7 18 9 16 12 17 2] +[14 11 19 10] +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 + +Loop is as below: +[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] + + +=end comment + +my @Test = + +; +plan @Test; + +sub func( $a) { β¦ } + +for @Test -> $in, $exp { + is func($in), $exp, 'comment'; +} + +done-testing; +exit; + diff --git a/challenge-237/0rir/raku/ch-1.raku b/challenge-237/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..1172caecd5 --- /dev/null +++ b/challenge-237/0rir/raku/ch-1.raku @@ -0,0 +1,102 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # π¦ β
βͺβ©βββ β‘ β’ Β«β€ Β» β΄ +use v6.d; +use Test; + +=begin comment +236-1: Seize The Day Submitted by: Mark Anderson +Given a year, a month, a weekday of month, and a day of +week (1 (Mon) .. 7 (Sun)), print the day. + +Example 1 +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: 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: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 +Output: 0 + +There isn't a 5th Wed in Aug 2026 +=end comment + +my @Test = + #yr mo occur dow day + 2024, 4, 1, 1, 1, + 2024, 4, 1, 2, 2, + 2024, 4, 1, 3, 3, + 2024, 4, 1, 4, 4, + 2024, 4, 1, 5, 5, + 2024, 4, 1, 6, 6, + 2024, 4, 1, 7, 7, + + 2024, 4, 2, 2, 9, + 2024, 4, 3, 2, 16, + 2024, 4, 4, 2, 23, + 2024, 4, 5, 2, 30, + 2024, 4, 6, 2, Int, + + 2025, 10, 2, 4, 9, + 2026, 8, 5, 3, Int, + 2024, 3, 3, 1, 18, +; + +plan 33 + @Test Γ· 5; + +# Argument $date must only be in the correct month and year. + +sub nth-day-or-zed( Date $date, UInt $dow, UInt $nth --> Int ) { + $_ = nth-day-of-week-in-month( $date, $dow, $nth ); + .defined ?? .day !! Int +} + +sub nth-day-of-week-in-month( Date $date, + UInt $dow, + UInt $nth is copy --> Date ) { + my $ret = first-day-of-week-in-month( $date, $dow) + ($nth Γ 7) -7; + return Date if $date.month β $ret.month; + $ret; +} + +sub first-day-of-week-in-month( Date $date is copy, + UInt $dow where 0 < * < 8 --> Date ) { + my $start-day-of-week = ( $date.=first-date-in-month ).day-of-week; + return do given $start-day-of-week <=> $dow { + when Less { $date += $dow - $start-day-of-week; } + when More { $date += 7 - $start-day-of-week + $dow; } + when Same { $date; } + } +} + +for @Test -> $y,$m, $nth, $dow, $exp { + if $nth == 1 { + is first-day-of-week-in-month( Date.new( $y,$m,14), $dow).day-of-week, + $exp, + "first-day-o-wk-in-mo"; + } + my $x = Date.new( $y, $m, 15); + + my $res = nth-day-of-week-in-month( $x, $dow, $nth); + if $res.defined { + is $res.day, $exp, "day is expected"; + is $res.weekday-of-month == $nth, True, "weekday match"; + } + + is nth-day-or-zed( $x, $dow, $nth), + $exp, "yr/mo: $y/$m occur: $nth dow: $dow "; +} +done-testing; + +my ($Year, $Month, $Weekday-of-month, $day-of-week) = 2024, 4, 3, 2; +say "\nInput: Year = $Year, Month = $Month," + ~ " Weekday of month = $Weekday-of-month, day-of-week = $day-of-week" + ~ "\nOutput: ", + nth-day-or-zed( + Date.new( $Year, $Month, 7), $day-of-week, $Weekday-of-month); +exit; + diff --git a/challenge-237/0rir/raku/ch-2.raku b/challenge-237/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..5871348c1c --- /dev/null +++ b/challenge-237/0rir/raku/ch-2.raku @@ -0,0 +1,84 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # π¦ β
βͺβ©βββ β‘ β’ Β«β€ Β» β΄ +use v6.d; +use Test; + +=begin comment +236-2: Maximise Greatness Submitted by: Mohammad S Anwar + +Given an array of integers, permute the given array such that you get +the maximum possible greatness. + +To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length + +Example 1 +Input: @nums = (1, 3, 5, 2, 1, 3, 1) +Output: 4 + +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 = (1, 2, 3, 4) +Output: 3 + +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] + +=end comment + +my @Test = + (1,), 0, + (1,1,1,1), 0, + (1,2), 1, + (3,2,1), 2, + (1,1,2), 1, + (1,3,2,4), 3, + (1,1,1,2), 1, + (1,1,1,1,2), 1, + (1,1,1,1,2,2), 2, + (1,1,1,1,2,2,2), 3, + (1,1,1,1,2,2,2,2), 4, + (1,1,1,1,2,2,2,2,2), 4, + (1,1,1,1,2,2,2,2,2,2,2), 4, + (1,3,5,2,1,3,1), 4, + (1,1,3,5,2,1,3,1), 4, + (1,3,5,2,1,3,1,3,3,5), 6, + (1,3,5,2,1,3,1,3,3,3), 5, + (1,3,5,2,1,3,1,5,5,5), 6, + (1,3,5,2,1,3,1,5,5,5,4), 7, +; + +plan @Test Γ· 2; + +sub func( @in is copy --> Int) { + + return 0 if @in.end == 0; + + my ($head, $tail, $work) = 0, 1, 0; + @in.=sort; + + while $tail β€ @in.end { + if @in[$tail] == @in[$head] { + ++$tail; + next; + } + ++$tail; + ++$head; + } + $head; +} + +for @Test -> @in, $exp { + is func(@in), $exp, "$exp <- in: @in.sort()"; +} +done-testing; +my @int = (1,3,5,2,1,3,1,5,5,5,4).sort; +say "\nInput: @int = @int.raku()\nOutput: &func( @int)"; +exit; + |
