From 9380fe143cc5ce16d97cd6fcc2f2f8d18151e4d3 Mon Sep 17 00:00:00 2001 From: rir Date: Tue, 4 Feb 2025 22:26:07 -0500 Subject: 307 --- challenge-307/0rir/raku/ch-1.raku | 60 +++++++++++++++++++++++++++++++++++++++ challenge-307/0rir/raku/ch-2.raku | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 challenge-307/0rir/raku/ch-1.raku create mode 100644 challenge-307/0rir/raku/ch-2.raku diff --git a/challenge-307/0rir/raku/ch-1.raku b/challenge-307/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..28c320c3b2 --- /dev/null +++ b/challenge-307/0rir/raku/ch-1.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +Task 1: Check Order +Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints. + +Write a script to re-arrange the given array in an increasing order and return the indices where it differs from the original array. + +Example 1 +Input: @ints = (5, 2, 4, 3, 1) +Output: (0, 2, 3, 4) + +Before: (5, 2, 4, 3, 1) +After : (1, 2, 3, 4, 5) + +Difference at indices: (0, 2, 3, 4) +Example 2 +Input: @ints = (1, 2, 1, 1, 3) +Output: (1, 3) + +Before: (1, 2, 1, 1, 3) +After : (1, 1, 1, 2, 3) + +Difference at indices: (1, 3) +Example 3 +Input: @ints = (3, 1, 3, 2, 3) +Output: (0, 1, 3) + +Before: (3, 1, 3, 2, 3) +After : (1, 2, 3, 3, 3) + +Difference at indices: (0, 1, 3) + +=end comment + +my @Test = + (), (), + (1,), (), + (1,2), (), + (2,1), (0,1), + (1, 2, 1, 1, 3), (1, 3), + (5, 2, 4, 3, 1), (0, 2, 3, 4), + (3, 1, 3, 2, 3), (0, 1, 3), + (1, 204, 9, 7, 172, 238, 727, 319, 102), (1, 3, 5, 6, 8), +; +plan @Test ÷ 2; + +sub task( @in) { (@in.sort Z== @in) .grep: !*, :k } + +for @Test -> @in, $exp { + is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()" +} +done-testing; + +my @int = 1, 204, 9, 7, 172, 238, 727, 102, 319; +print "\nInput: @int = ( ", @int, " )\nOutput: ", task(@int); diff --git a/challenge-307/0rir/raku/ch-2.raku b/challenge-307/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..99912d1c2b --- /dev/null +++ b/challenge-307/0rir/raku/ch-2.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +307-Task 2: Find Anagrams Submitted by: Mohammad Sajid Anwar +You are given a list of words, @words. + +Write a script to find any two consecutive words and if they are anagrams, drop the first word and keep the second. You continue this until there is no more anagrams in the given list and return the count of final list. + +Example 1 +Input: @words = ("acca", "dog", "god", "perl", "repl") +Output: 3 + +Step 1: "dog" and "god" are anagrams, so dropping "dog" and keeping "god" => ("acca", "god", "perl", "repl") +Step 2: "perl" and "repl" are anagrams, so dropping "perl" and keeping "repl" => ("acca", "god", "repl") +Example 2 +Input: @words = ("abba", "baba", "aabb", "ab", "ab") +Output: 2 + +Step 1: "abba" and "baba" are anagrams, so dropping "abba" and keeping "baba" => ("baba", "aabb", "ab", "ab") +Step 2: "baba" and "aabb" are anagrams, so dropping "baba" and keeping "aabb" => ("aabb", "ab", "ab") +Step 3: "ab" and "ab" are anagrams, so dropping "ab" and keeping "ab" => ("aabb", "ab") + +=end comment + +my @Test = + (), 0, + ('ab',), 1, + ('ba', 'ab'), 1, + ('ac', 'ba'), 2, + ("abba", "baba", "aabb", "ab", "ab"), 2, + ("acca", "dog", "god", "perl", "repl"), 3, + ('fussy', 'fuzzy', 'fussy', 'furry',), 4, + ('fuzzy', 'fussy', 'fussy', 'furry',), 3, + ('ab', 'ba', 'ab', 'ba', 'da'), 2, + ('ab', 'da', 'ba', 'ab', 'ba'), 3, + ('ab', 'ba', 'ab', 'da', 'ba'), 3, +; +plan @Test ÷ 2; + +only task( @a is copy -->Int) { + my $deleted = 0; + @a.=map({.split('').sort.join }); + for 1..^@a -> $i { + ++$deleted if @a[$i-1] eq @a[$i]; + } + @a.elems - $deleted; +} + +for @Test -> @a, $exp { + is task( @a), $exp, "{$exp // $exp.^name()} <- @a.raku()" +} +done-testing; + +my @word = "acca", "dog", "god", "perl", "repl"; +say "\nInput: @word = ( ", @word.map('"' ~ * ~ '"' ).join(' ,'), ")"; +say "Output: ", @word.&task; + -- cgit