diff options
| author | Mark Anderson <mark@andemark.io> | 2025-05-19 15:43:03 +0000 |
|---|---|---|
| committer | Mark Anderson <mark@andemark.io> | 2025-05-19 15:43:03 +0000 |
| commit | 174dc182320b7103cc835481f798c0284af14d81 (patch) | |
| tree | 8f49ea0abef22a793bc34b2c995f763fd4a4dd56 | |
| parent | e69cf5c18c93538495bd4f040febc0f0fe9e3440 (diff) | |
| download | perlweeklychallenge-club-174dc182320b7103cc835481f798c0284af14d81.tar.gz perlweeklychallenge-club-174dc182320b7103cc835481f798c0284af14d81.tar.bz2 perlweeklychallenge-club-174dc182320b7103cc835481f798c0284af14d81.zip | |
Challenge 322 Solutions (Raku)
| -rw-r--r-- | challenge-322/mark-anderson/raku/ch-1.raku | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/challenge-322/mark-anderson/raku/ch-1.raku b/challenge-322/mark-anderson/raku/ch-1.raku index d40f05735a..d5fe719531 100644 --- a/challenge-322/mark-anderson/raku/ch-1.raku +++ b/challenge-322/mark-anderson/raku/ch-1.raku @@ -5,7 +5,22 @@ is format-string("ABC-D-E-F", 3), "ABC-DEF"; is format-string("A-BC-D-E", 2), "A-BC-DE"; is format-string("-A-B-CD-E", 4), "A-BCDE"; -sub format-string($str is copy, $int) +sub format-string($str, $int) +{ + # This is the version I like but it doesn't always work. + # The problem is with the {$int}. If it's hardcoded with + # the appropriate int then it works. Not sure what I'm + # doing wrong. 🤔 🤷 + + $str ~~ /^ + ( \-? <upper> \-? ) ** {0..$int} + ([ \-? <upper> \-? ] ** {$int} )* + $/; + + (flat $/[0]>><upper>.join, $/[1]>><upper>>>.join).join("-") +} + +sub format-string-v2($str is copy, $int) { $str ~~ s:g/"-"//; my $m = "." x ($str.chars mod $int or $int); @@ -13,7 +28,7 @@ sub format-string($str is copy, $int) S:g/<?after <$m>> (<$i>)/-$0/ given $str } -sub format-string-v2($str, $int) +sub format-string-v3($str, $int) { my @chars = $str.comb(/<upper>/); my $list = flat (@chars mod $int or Empty), ($int, $int...*); |
