diff options
| -rwxr-xr-x | challenge-115/feng-chang/raku/ch-1.raku | 12 | ||||
| -rwxr-xr-x | challenge-115/feng-chang/raku/ch-2.raku | 23 |
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-115/feng-chang/raku/ch-1.raku b/challenge-115/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..0634f261fc --- /dev/null +++ b/challenge-115/feng-chang/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/bin/env raku + +sub MAIN(*@S) { + for @S.permutations -> @s { + if (^@s.elems).map({ @s[$_].substr(*-1) eq @s[($_+1) % @s.elems].substr(0,1) }).all { + put 1; + exit; + } + } + + put 0; +} diff --git a/challenge-115/feng-chang/raku/ch-2.raku b/challenge-115/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..228ab113ea --- /dev/null +++ b/challenge-115/feng-chang/raku/ch-2.raku @@ -0,0 +1,23 @@ +#!/bin/env raku + +sub largest-even(UInt:D $n --> UInt:D) { + my @d = $n.combĀ».UInt; # get all digits + my $last-digit = @d.grep(* %% 2).min; # find the smallest even digit + + my Bool $done = False; + @d .= grep({ $done or $_ != $last-digit ?? True !! do { $done = True; False }}); # filter out the found even digit, only once + + (@d.sort({ $^b <=> $^a }).join ~ $last-digit).UInt # 'print' the array, largest to smallest, at last, 'print' the found even digit +} + +multi MAIN('test') { + use Test; + + is largest-even(1026), 6210, '1,0,2,6 -> 6210'; + is largest-even(1428), 8412, '1,4,2,8 -> 8412'; + is largest-even(4176), 7614, '4,1,7,6 -> 7614'; + + done-testing; +} + +multi MAIN(UInt:D $n) { put largest-even($n) } |
