diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-13 01:10:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-13 01:10:07 +0000 |
| commit | 841101dd5b67b8788db152215749860f94940aea (patch) | |
| tree | 2b729ff36cf962efbbd0cc15692bd97b7d89d4da | |
| parent | c3ee94e77ddd1bed45905c1c98a12f0fc2fd2998 (diff) | |
| parent | 1a7c1c982a6c1105ced55cae4cf2d58e49e9c67b (diff) | |
| download | perlweeklychallenge-club-841101dd5b67b8788db152215749860f94940aea.tar.gz perlweeklychallenge-club-841101dd5b67b8788db152215749860f94940aea.tar.bz2 perlweeklychallenge-club-841101dd5b67b8788db152215749860f94940aea.zip | |
Merge pull request #9572 from ash/ash-256
Week 256, solutions in Raku by ash
| -rw-r--r-- | challenge-256/ash/raku/ch-1.raku | 25 | ||||
| -rw-r--r-- | challenge-256/ash/raku/ch-2.raku | 27 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-256/ash/raku/ch-1.raku b/challenge-256/ash/raku/ch-1.raku new file mode 100644 index 0000000000..afab0079c5 --- /dev/null +++ b/challenge-256/ash/raku/ch-1.raku @@ -0,0 +1,25 @@ +# Solution to the Task 1 of the Weekly Challenge #256 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK1 + +# Test run: +# % raku ch-1.raku +# 1 +# 0 +# 2 + +my @tests = + ('ab', 'de', 'ed', 'bc'), + ('aa', 'ba', 'cd', 'ed'), + ('uv', 'qp', 'st', 'vu', 'mn', 'pq'); + +for @tests -> @words { + say count-maximum-pairs(@words); +} + +sub count-maximum-pairs(@words) { + my @rev = @words.map: *.flip; + my $words = (@words, @rev).Bag; + + my $pairs = $words.grep: *.value == 2; # Each pair is counted twice + return $pairs.elems div 2; +} diff --git a/challenge-256/ash/raku/ch-2.raku b/challenge-256/ash/raku/ch-2.raku new file mode 100644 index 0000000000..e6915815e4 --- /dev/null +++ b/challenge-256/ash/raku/ch-2.raku @@ -0,0 +1,27 @@ +# Solution to the Task 2 of the Weekly Challenge #256 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK2 + +# Test run: +# $ raku ch-2.raku +# a1b2c3d4 +# a1b2c345 +# a1b2c3de + +my @tests = + ('abcd', '1234'), + ('abc', '12345'), + ('abcde', '123'); + +for @tests -> ($str1, $str2) { + say merge-strings($str1, $str2); +} + +sub merge-strings($str1, $str2) { + my $merged = ($str1.comb Z~ $str2.comb).join; + + given $str1.chars - $str2.chars { + return $merged ~ $str1.substr($str2.chars) when * > 0; + return $merged ~ $str2.substr($str1.chars) when * < 0; + return $merged; + } +} |
