diff options
| -rw-r--r-- | challenge-074/jeongoon/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-074/jeongoon/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-074/jeongoon/raku/ch-2.raku | 8 |
3 files changed, 29 insertions, 11 deletions
diff --git a/challenge-074/jeongoon/perl/ch-1.pl b/challenge-074/jeongoon/perl/ch-1.pl index 9353563837..04bec4e2b9 100644 --- a/challenge-074/jeongoon/perl/ch-1.pl +++ b/challenge-074/jeongoon/perl/ch-1.pl @@ -10,7 +10,7 @@ sub get_major (@) { @_ == 1 and return $_[0]; my @sorted = sort @_; - my $halflen = int( .5 * @sorted ); # (halflen)gth + my $halflen = int .5 * @sorted; # (halflen)gth my $pnum = shift @sorted; # (p)revious (num)ber my $pcnt = 1; # (p)revious (c)ou(nt) diff --git a/challenge-074/jeongoon/perl/ch-2.pl b/challenge-074/jeongoon/perl/ch-2.pl index 1758963be6..794ec9ee82 100644 --- a/challenge-074/jeongoon/perl/ch-2.pl +++ b/challenge-074/jeongoon/perl/ch-2.pl @@ -4,17 +4,37 @@ use strict; use warnings; -sub uniq_sorted { - my %mem = (); - map { exists $mem{$_} ? () : ($mem{$_} = $_) } @_ +sub unique { + scalar @_ or return (); + $_[0], map { $_[0] ne $_ ? $_: () } @_[1..$#_]; } -sub printLNR ($) { +# adapted from my own solution in common-lisp which is a bit lengthy +sub printLNR { + my $str = shift; + for my $last_idx ( 1 .. length $str ) { + my $sub_chars = substr $str, 0, $last_idx; + my @candi = unique( split '', $sub_chars ); + + my ( $nr_pos, $nr_chr ) = ( -1, '#' ); + + for my $c ( @candi ) { + next if ( my $pos1 = index $sub_chars, $c ) == -1; + next if ( my $pos2 = index $sub_chars, $c, $pos1 + 1 ) != -1; + $nr_pos < $pos1 and ( $nr_pos = $pos1, $nr_chr = $c ); + } + print "$nr_chr"; + } + print $/; +} + +# posted on 20th of Aug +sub printLNR_by_split ($) { my $str = shift; for my $last_idx ( 1 .. length $str ) { my $sub_chars = substr $str, 0, $last_idx; - my @candi = uniq_sorted( split '', $sub_chars ); + my @candi = unique( split '', $sub_chars ); my $nr_pos = -1; my $nr_chr = '#'; diff --git a/challenge-074/jeongoon/raku/ch-2.raku b/challenge-074/jeongoon/raku/ch-2.raku index ab03aa5e41..c3d9eab63e 100644 --- a/challenge-074/jeongoon/raku/ch-2.raku +++ b/challenge-074/jeongoon/raku/ch-2.raku @@ -7,13 +7,11 @@ use v6.d; # solution role fnr { method sayLNR ( Str:D $str = self.Str ) { - my @chars = $str.comb; - - for 1 .. @chars.elems -> $last-index { + for 1 .. $str.chars -> $last-index { my $sub-chars = $str.substr( 0, $last-index ); my @candi = @sub-chars.unique; - my $nr-pos = -1; - my $nr-char = '#'; + my ( $nr-pos, $nr-char ) = ( -1, '#' ); + for @candi -> $c { given $sub-chars.indices( $c ) { .elems == 1 or next; |
