diff options
| -rw-r--r-- | challenge-074/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-074/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-074/luca-ferrari/raku/ch-1.p6 | 20 | ||||
| -rw-r--r-- | challenge-074/luca-ferrari/raku/ch-2.p6 | 33 |
4 files changed, 55 insertions, 0 deletions
diff --git a/challenge-074/luca-ferrari/blog-1.txt b/challenge-074/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..babe89e945 --- /dev/null +++ b/challenge-074/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/08/17/PerlWeeklyChallenge74.html#task1 diff --git a/challenge-074/luca-ferrari/blog-2.txt b/challenge-074/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..d1a50f274b --- /dev/null +++ b/challenge-074/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/08/17/PerlWeeklyChallenge74.html#task2 diff --git a/challenge-074/luca-ferrari/raku/ch-1.p6 b/challenge-074/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..64bf58939d --- /dev/null +++ b/challenge-074/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#!raku + +# % raku ch-1.p6 1 2 2 3 2 4 2 +# 2 +# +# % raku ch-1.p6 1 3 1 2 4 5 +# -1 + +sub MAIN( *@array where { @array.grep( * ~~ Int ).elems == @array.elems } ) { + my $N = @array.elems; + my $majority = floor( $N / 2 ); + + my %counting; + %counting{ $_ }++ for @array; + + given %counting.pairs.map( { .value >= $majority ?? $_ !! Nil } ).grep( * ~~ Pair ).unique.head { + when .so { .key.say; } + default { '-1'.say; } + } +} diff --git a/challenge-074/luca-ferrari/raku/ch-2.p6 b/challenge-074/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..e8ea2d376f --- /dev/null +++ b/challenge-074/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,33 @@ +#!raku + +sub MAIN( Str $S where { $S.chars > 2 } ) { + + my @result; + my %counting; + my @chars = $S.comb( '', :skip-empty ); + + for 0 ..^ @chars.elems -> $index { + my $current-char = @chars[ $index ]; + + + + # if the result array is empty + # or the char is not in the array, it is + # ok to push + @result.push( $current-char ) && next if ! @result || ! @result.grep( * ~~ $current-char ); + + + # if here I need to search for the first rightmost + # not repeating char so far + %counting = %(); + %counting{ $_ }++ for $S.substr( 0 .. $index ).comb( '', :skip-empty ); + my $fnr = $S.substr( 0 .. $index ) + .comb( '', :skip-empty ) + .first( { %counting{ $_ }:exists && %counting{ $_ } == 1 }, :end ) + // '#'; + + @result.push: $fnr; + } + + @result.join.say; +} |
