diff options
| author | Andrey Shitov <ash@Andreys-iMac.local> | 2025-07-07 12:49:12 +0300 |
|---|---|---|
| committer | Andrey Shitov <ash@Andreys-iMac.local> | 2025-07-07 12:49:12 +0300 |
| commit | 4e7df4158a2ddce14748bb9acda5bd9576137111 (patch) | |
| tree | 9197991a4342286857c1c57df1f6df0bad85fa26 | |
| parent | bd7fce4bd5d085c209a213f2daca1e79799c9e87 (diff) | |
| download | perlweeklychallenge-club-4e7df4158a2ddce14748bb9acda5bd9576137111.tar.gz perlweeklychallenge-club-4e7df4158a2ddce14748bb9acda5bd9576137111.tar.bz2 perlweeklychallenge-club-4e7df4158a2ddce14748bb9acda5bd9576137111.zip | |
Week 329, solutions by @ash in Raku
| -rw-r--r-- | challenge-329/ash/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-329/ash/raku/ch-2.raku | 38 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-329/ash/raku/ch-1.raku b/challenge-329/ash/raku/ch-1.raku new file mode 100644 index 0000000000..593f5af080 --- /dev/null +++ b/challenge-329/ash/raku/ch-1.raku @@ -0,0 +1,14 @@ +# Task 1 of The Weekly Challenge 329 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/#TASK1 + +say find-distinct-numbers('the1weekly2challenge2'); +say find-distinct-numbers('go21od1lu5c7k'); +say find-distinct-numbers('4p3e2r1l'); + +sub find-distinct-numbers($str) { + ( + ( + $str ~~ m:g/ \d+ / + )>>.Str + ).unique +} diff --git a/challenge-329/ash/raku/ch-2.raku b/challenge-329/ash/raku/ch-2.raku new file mode 100644 index 0000000000..29bdff0469 --- /dev/null +++ b/challenge-329/ash/raku/ch-2.raku @@ -0,0 +1,38 @@ +# Task 2 of The Weekly Challenge 329 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/#TASK2 + +say longest-substring('YaaAh'); # aaA +say longest-substring('cC'); # cC +say longest-substring('A'); # <None> + +# Some additional tests: +say longest-substring('xuWwcC'); # WwcC +say longest-substring('YaaAhbbBB'); # bbBB +say longest-substring('YaaAhbBB'); # aaA being the first one, but bBB also matches + +sub longest-substring($str) { + my @nice; + + for 0 ..^ $str.chars -> $start { + for $start + 1 .. $str.chars -> $end { + my $substr = $str.substr($start, $end - $start); + + push @nice, $substr if is-nice($substr); + } + } + + return '<None>' unless @nice; + + my $max-len = @nice.map(*.chars).max; + + return (@nice.grep: *.chars == $max-len)[0]; # Only returns a single match + + sub is-nice($str) { + my $chars = $str.comb.Bag; + my @lc = $chars.keys.grep: * ~~ /<:Lower>/; + my @uc = $chars.keys.grep: * ~~ /<:Upper>/; + @uc>>.=lc; + + return @lc.Set eqv @uc.Set; + } +} |
