diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-11 13:51:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-11 13:51:22 +0000 |
| commit | 0710804b5656a43eb382a1468397526837e717d5 (patch) | |
| tree | cd7f2973c279ea8dea3b6735db88c26843fcb070 | |
| parent | 2b98e6b19e592e33ad84c2e3600f882235ec545d (diff) | |
| parent | 494c84a4c826ae08c1eff6dd6c8cd693bc83472a (diff) | |
| download | perlweeklychallenge-club-0710804b5656a43eb382a1468397526837e717d5.tar.gz perlweeklychallenge-club-0710804b5656a43eb382a1468397526837e717d5.tar.bz2 perlweeklychallenge-club-0710804b5656a43eb382a1468397526837e717d5.zip | |
Merge pull request #13015 from 0rir/work
347
| -rw-r--r-- | challenge-347/0rir/raku/ch-1.raku | 64 | ||||
| -rw-r--r-- | challenge-347/0rir/raku/ch-2.raku | 60 |
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-347/0rir/raku/ch-1.raku b/challenge-347/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..f12d4dd3e2 --- /dev/null +++ b/challenge-347/0rir/raku/ch-1.raku @@ -0,0 +1,64 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.d; +use Test; +use Lingua::EN::Numbers; + +=begin comment +May be edited for space. +347-1: Format Date Submitted by: Mohammad Sajid Anwar +You are given a date in the form: 10th Nov 2025. +Write a script to format the given date in the form: 2025-11-10 using the set below. + +@DAYS = ("1st", "2nd", "3rd", ....., "30th", "31st") +@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov", "Dec") +@YEARS = (1900..2100) + +=end comment + +my @Test = + "1st Jan 2025", "2025-01-01", + "22nd Feb 2025", "2025-02-22", + "15th Apr 2025", "2025-04-15", + "23rd Oct 2025", "2025-10-23", + "31st Dec 2025", "2025-12-31", +; +my @Dead = "31st Dec 2225", + "31st Dec 1825", + "32st Dec 1925", + "11st Dec 1925", + "30st Feb 2025", + "31nd Dec 2025", +; +plan +@Dead + +@Test ÷ 2; + +my @month = <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec>; +my %month-number = @month [Z=>] [1...12]; +my @day-of-month = (1...31)».&ordinal-digit; +constant \year-min = 1900; +constant \year-max = 2100; + +# strict about the input spec +multi task( Str:D $a -->Str:D) { + my $m = $a ~~ m/ ^ (<[123]>? <[1..9]>) (<:L>**2) + \s (@month) + \s (\d\d\d\d) $ /; + my $candi = "$m[3]-%month-number{$m[2]}.fmt('%02s')-" ~ "$m[0].fmt('%02s')"; + die "Input '$a' does not represent a valid date" + unless $candi.Date ~~ Date:D + and year-min ≤ $m[3] ≤ year-max + and $m[0] ~ $m[1] eq $m[0].&ordinal-digit; + return $candi; +} + +for @Test -> $in, $exp { + is task( $in), $exp, "{$exp // $exp.^name()} <- $in.raku()"; +} +for @Dead -> $in { + dies-ok { task $in }, "$in.raku() dies"; +} +done-testing; + +my $str = "31st Dec 2025"; + +say qq{\nInput: \$str = "$str"\nOutput: }, task $str; diff --git a/challenge-347/0rir/raku/ch-2.raku b/challenge-347/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..e8d748e4c9 --- /dev/null +++ b/challenge-347/0rir/raku/ch-2.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.d; +use Test; + +=begin comment +May be edited for space. +147-Task 2: Format Phone Number Submitted by: Mohammad Sajid Anwar +You are given a phone number as a string containing digits, space and dash only. + +Write a script to format the given phone number using the below rules: + +1. Removing all spaces and dashes +2. Grouping digits into blocks of length 3 from left to right +3. Handling the final digits (4 or fewer) specially: + - 2 digits: one block of length 2 + - 3 digits: one block of length 3 + - 4 digits: two blocks of length 2 +4. Joining all blocks with dashes + +=end comment + +my @Test = + '12', '12', + '1 2', '12', + '1 2 - 3 4', '12-34', + "1-23-45-6", "123-456", + "1234", "12-34", + "12 345-6789", "123-456-789", + "123 4567", "123-45-67", + "123 456-78", "123-456-78", +; +my @Dead = ' -1', '', ' ', '1', '1+1'; +plan +@Dead + +@Test ÷ 2; + +sub task( Str:D $a is copy where / ^ [ <[- 0..9]> | ' ' ]+ $ / -->Str:D) { + my @w = $a.trans( [0..9] => '', :complement).comb; + die "Multiple digits required got '$a'" if @w < 2; + given +@w { + when 2 { return @w.join } + when 4 { return (@w[0..1].join, @w[2..3].join).join: '-' } + default { + @w.=rotor( 3, :partial); + if 1 == +@w[*-1] { @w[*-1].=Array.unshift: @w[*-2].=Array.pop; } + return (@w».join).join: '-'; + } + } +} + +for @Test -> $in, $exp { + is task( $in), $exp, "{$exp // $exp.^name()} <- $in"; +} +for @Dead -> $in { + dies-ok { task( $in) }, "$in.raku() dies"; +} +done-testing; + +my $phone = "123 456-78"; +say qq{\nInput: \$phone = "$phone"\nOutput: "&task($phone)"}; + |
