diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2021-01-04 17:27:02 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2021-01-04 18:07:20 +0100 |
| commit | 761b98f87b5980a347074650487d0351b97eeca1 (patch) | |
| tree | 40fc876463775dc2449ad7145f1b5082a176c674 | |
| parent | 22a3899e5f0b7e3409378993bcd7da1e1513c0ee (diff) | |
| download | perlweeklychallenge-club-761b98f87b5980a347074650487d0351b97eeca1.tar.gz perlweeklychallenge-club-761b98f87b5980a347074650487d0351b97eeca1.tar.bz2 perlweeklychallenge-club-761b98f87b5980a347074650487d0351b97eeca1.zip | |
Tasks 1 and 2 done.
| -rw-r--r-- | challenge-094/luca-ferrari/raku/ch-1.p6 | 41 | ||||
| -rw-r--r-- | challenge-094/luca-ferrari/raku/ch-2.p6 | 33 |
2 files changed, 74 insertions, 0 deletions
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 = <opt bat saw tab pot top was>; + 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; + +} |
