diff options
| author | Mark <53903062+andemark@users.noreply.github.com> | 2022-08-28 11:01:43 +0000 |
|---|---|---|
| committer | Mark <53903062+andemark@users.noreply.github.com> | 2022-08-28 11:01:43 +0000 |
| commit | 83a865161a5b298e85b2145b41e6ca14558af214 (patch) | |
| tree | 4254827751e4fc620d805c74be842b45fbb5b2b4 | |
| parent | 3501c9c0a78d3510cc692e56dc279adb4d7a0ba1 (diff) | |
| download | perlweeklychallenge-club-83a865161a5b298e85b2145b41e6ca14558af214.tar.gz perlweeklychallenge-club-83a865161a5b298e85b2145b41e6ca14558af214.tar.bz2 perlweeklychallenge-club-83a865161a5b298e85b2145b41e6ca14558af214.zip | |
Decided to go with the module
| -rw-r--r-- | challenge-179/mark-anderson/raku/ch-1.raku | 69 |
1 files changed, 4 insertions, 65 deletions
diff --git a/challenge-179/mark-anderson/raku/ch-1.raku b/challenge-179/mark-anderson/raku/ch-1.raku index 9f25e464ac..947de6ad1c 100644 --- a/challenge-179/mark-anderson/raku/ch-1.raku +++ b/challenge-179/mark-anderson/raku/ch-1.raku @@ -1,68 +1,7 @@ #!/usr/bin/env raku +use Lingua::EN::Numbers; use Test; -is ordinal(11), 'eleventh'; -is ordinal(62), 'sixty-second'; -is ordinal(99), 'ninety-ninth'; -is ordinal(100), 'one-hundredth'; -is ordinal(4320), 'four thousand, three-hundred-twentieth'; -is ordinal(11009029), 'eleven million, nine thousand, twenty-ninth'; - -multi ordinal($n where * ~~ UInt) -{ - ordinal(number2string($n)) -} - -multi ordinal($s) -{ - my $ord; - - my %h = :one('first'), :two('second'), :three('third'), :five('fifth'), - :eight('eighth'), :nine('ninth'), :twelve('twelfth'); - - my $m = $s ~~ / <[a..z]>+ $ /; - - given $m - { - when %h{$_}:exists { $ord = %h{$m} } - when .ends-with('y') { $ord = $m.subst(/ y $ /, 'ieth') } - default { $ord = $m ~ 'th' } - } - - $s.subst(/ $m $ /, $ord); -} - -sub number2string($num) -{ - my @n = $num.polymod(1000 xx *); - - my @lt-twenty = < one two three four five six seven eight nine ten eleven twelve - thirteen fourteen fifteen sixteen seventeen eighteen nineteen > andthen .unshift: ''; - - my @tens = < twenty thirty forty fifty sixty seventy eighty ninety > andthen .prepend: '', ''; - - my @magnitude = < thousand million billion trillion quadrillion quintillion > andthen .unshift: ''; - - return .reverse.join(', ') given gather for @n.kv -> $k, $v - { - given $v.chars - { - when 1 { take (@lt-twenty[$v] ~ ' ' ~ @magnitude[$k]).trim-trailing if $v } - when 2 { take (lt-hundred($v) ~ ' ' ~ @magnitude[$k]).trim-trailing } - default - { - my $h = @lt-twenty[$v.substr(0,1)] ~ '-hundred'; - my $t = lt-hundred($v.substr(1,2)); - take (not $t) ?? $h !! ($h ~ '-' ~ $t ~ ' ' ~ @magnitude[$k]).trim-trailing; - } - } - } - - sub lt-hundred($n) - { - return @lt-twenty[$n] if $n < 20; - my @d = $n.comb; - return @tens[@d[0]] unless +@d[1]; - return @tens[@d[0]] ~ '-' ~ @lt-twenty[@d[1]] - } -} +is ordinal(11), 'eleventh'; +is ordinal(62), 'sixty-second'; +is ordinal(99), 'ninety-ninth'; |
