aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-04-11 22:08:10 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-04-11 22:08:10 +0800
commite2c8b4bec3b682df8597f57ec627508a07582d31 (patch)
tree219b3533a58b360cd25504c6ee8c2dbc9f0ac265
parent6b7fd386eb2e4b8b700fb12f6a7ca043baa6164c (diff)
downloadperlweeklychallenge-club-e2c8b4bec3b682df8597f57ec627508a07582d31.tar.gz
perlweeklychallenge-club-e2c8b4bec3b682df8597f57ec627508a07582d31.tar.bz2
perlweeklychallenge-club-e2c8b4bec3b682df8597f57ec627508a07582d31.zip
challenge 212, raku solutions
-rwxr-xr-xchallenge-212/feng-chang/raku/ch-1.raku27
-rwxr-xr-xchallenge-212/feng-chang/raku/ch-2.raku21
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-212/feng-chang/raku/ch-1.raku b/challenge-212/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..601fa71045
--- /dev/null
+++ b/challenge-212/feng-chang/raku/ch-1.raku
@@ -0,0 +1,27 @@
+#!/bin/env raku
+
+unit sub MAIN(*@N);
+
+sub USAGE() {
+ print Q:c:to/END/;
+ Usage: {$*PROGRAM-NAME} <word> <integer> ...
+ Word should contain alphabetic characters only, and
+ the number of integers should be equal to word length.
+ END
+}
+
+if +@N == 0 or
+ @N[0] !~~ /^ <[a..z A..Z]>+ $/ or
+ @N[0].chars !== +@N - 1 {
+ USAGE();
+ exit 1;
+}
+
+my method jump-characters(Str:D $c : UInt:D $n --> Str:D) {
+ given $c {
+ when 'a'..'z' { chr((ord($c) - ord('a') + $n) % 26 + ord('a')) }
+ when 'A'..'Z' { chr((ord($c) - ord('A') + $n) % 26 + ord('A')) }
+ }
+}
+
+put @N[0].comb.pairs.map({ .value.&jump-characters(@N[.key + 1]) }).join;
diff --git a/challenge-212/feng-chang/raku/ch-2.raku b/challenge-212/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..100c4e13b1
--- /dev/null
+++ b/challenge-212/feng-chang/raku/ch-2.raku
@@ -0,0 +1,21 @@
+#!/bin/env raku
+
+unit sub MAIN(*@N, Int:D :$size);
+
+my $bag = @N».Int.Bag;
+my @groups;
+
+while +$bag {
+ my $k = $bag.keys.min;
+ my $g = ($k .. ($k + $size - 1)).Bag;
+
+ if $g (<=) $bag {
+ $bag (-)= $g;
+ @groups.push($g.keys.sort);
+ } else {
+ put -1;
+ exit 1;
+ }
+}
+
+put @groups.map({ '(' ~ .join(',') ~ ')' }).join(', ');