diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2020-08-17 13:58:20 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2020-08-17 13:58:20 +0200 |
| commit | 837cc22c0835cb7788e06c05bb8667e15ffc01db (patch) | |
| tree | a9cf5304852b31b18f761a2e7571d3a6d37ee5c0 | |
| parent | 7a0333597b5f54b5601489b8436a8b1194811a5b (diff) | |
| download | perlweeklychallenge-club-837cc22c0835cb7788e06c05bb8667e15ffc01db.tar.gz perlweeklychallenge-club-837cc22c0835cb7788e06c05bb8667e15ffc01db.tar.bz2 perlweeklychallenge-club-837cc22c0835cb7788e06c05bb8667e15ffc01db.zip | |
Task 2 done
| -rw-r--r-- | challenge-074/luca-ferrari/raku/ch-2.p6 | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/challenge-074/luca-ferrari/raku/ch-2.p6 b/challenge-074/luca-ferrari/raku/ch-2.p6 index 78559cd9b0..2c0da8f0ea 100644 --- a/challenge-074/luca-ferrari/raku/ch-2.p6 +++ b/challenge-074/luca-ferrari/raku/ch-2.p6 @@ -2,33 +2,32 @@ sub MAIN( Str $S where { $S.chars > 2 } ) { - my $fnr = Nil; my @result; my %counting; my @chars = $S.comb( '', :skip-empty ); - for 0 ..^ @chars.elems { - my $current-char = @chars[ $_ ]; - - if ! $fnr { - $fnr = $current-char; - } - elsif $current-char ~~ $fnr { - my %counting; - %counting{ $_ }++ for @chars[ 0 .. $_ ]; - $fnr = %counting.pairs.grep( { .value == 1 && .key !~~ $current-char } ).first.key // '#'; - "selezionato $fnr".say; - } - elsif $_ !~~ $fnr { - say "$fnr $current-char"; - $fnr = $current-char; - } - else { - $fnr = '#'; - } + for 0 ..^ @chars.elems -> $index { + my $current-char = @chars[ $index ]; - @result.push: $fnr; + + # 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 ) + .reverse + .grep( { %counting{ $_ }:exists && %counting{ $_ } == 1 } ) + .first // '#'; + + @result.push: $fnr; } @result.join.say; |
