From 4e7df4158a2ddce14748bb9acda5bd9576137111 Mon Sep 17 00:00:00 2001 From: Andrey Shitov Date: Mon, 7 Jul 2025 12:49:12 +0300 Subject: Week 329, solutions by @ash in Raku --- challenge-329/ash/raku/ch-1.raku | 14 ++++++++++++++ challenge-329/ash/raku/ch-2.raku | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-329/ash/raku/ch-1.raku create mode 100644 challenge-329/ash/raku/ch-2.raku 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'); # + +# 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 '' 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; + } +} -- cgit