From 761b98f87b5980a347074650487d0351b97eeca1 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 4 Jan 2021 17:27:02 +0100 Subject: Tasks 1 and 2 done. --- challenge-094/luca-ferrari/raku/ch-1.p6 | 41 +++++++++++++++++++++++++++++++++ challenge-094/luca-ferrari/raku/ch-2.p6 | 33 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 challenge-094/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-094/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-094/luca-ferrari/raku/ch-1.p6 b/challenge-094/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..eab71e9777 --- /dev/null +++ b/challenge-094/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,41 @@ +#!raku + +sub get-anagrams( @S where { @S.elems > 1 } ) { + my @results; + + # cycle thru the words using ordering + # so that the same anagram is not + # backward referenced + for 0 ..^ @S -> $outer-index { + + # use a temporary array to store these anagrams + # and initialize it with the current word + my @anagrams = @S[ $outer-index ]; + for @anagrams[ 0 ].comb.permutations { + my $current = .join; + # push this word if it is not the same as the current one, + # it is within @S and is not already into @results + @anagrams.push: $current if ( @S[ $outer-index .. *-1 ].grep( $current ) + && ! @anagrams.grep( $current ) + && ! @results.List.flat.grep( $current ) ); + } + + # if there is more than one word (the first one is the current) + # then there is at least an anagram, so push to the final results + @results.push: [ @anagrams ] if ( @anagrams.elems > 1 ); + } + + return @results; +} + + +multi sub MAIN( *@S ) { + my @results = @S.elems > 1 ?? get-anagrams( @S ) !! @S; + say "Valid anagrams: { @results.join(' - ') }"; +} + +multi sub MAIN(){ + my Str @S = ; + my @results = get-anagrams( @S ); + say "Valid anagrams: { @results.join(' - ') }"; +} diff --git a/challenge-094/luca-ferrari/raku/ch-2.p6 b/challenge-094/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..a2c97345fd --- /dev/null +++ b/challenge-094/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,33 @@ +#!raku + + +class Node { + has Int $.value is rw; + has Node $.left is rw; + has Node $.right is rw; + + + method me-recursive() { + my $str = self.me; + $str ~= ' -> ' ~ $!left.me-recursive if $!left; + $str ~= ' -> ' ~ $!right.me-recursive if $!right; + $str; + } + + method me() { $!value; } + +} + + +sub MAIN() { + my $root = Node.new( value => 1 ); + $root.left = Node.new( value => 2 ); + $root.right = Node.new( value => 3 ); + $root.left.left = Node.new( value => 4 ); + $root.left.right = Node.new( value => 5 ); + $root.left.right.left = Node.new( value => 6 ); + $root.left.right.right = Node.new( value => 7 ); + + $root.me-recursive.say; + +} -- cgit