From 982554f03910669b50c7090262aed38c14152e67 Mon Sep 17 00:00:00 2001 From: Myoungjin JEON Date: Thu, 20 Aug 2020 22:00:58 +1000 Subject: [ch-074/jeongoon] Perl and Raku solution --- challenge-074/jeongoon/common-lisp/ch-2.lsp | 4 +- challenge-074/jeongoon/haskell/ch-1.hs | 2 +- challenge-074/jeongoon/haskell/ch-2.hs | 2 +- challenge-074/jeongoon/perl/ch-1.pl | 65 +++++++++++++++++++++++++++++ challenge-074/jeongoon/perl/ch-2.pl | 41 ++++++++++++++++++ challenge-074/jeongoon/raku/ch-1.raku | 38 +++++++++++++++++ challenge-074/jeongoon/raku/ch-2.raku | 38 +++++++++++++++++ 7 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 challenge-074/jeongoon/perl/ch-1.pl create mode 100644 challenge-074/jeongoon/perl/ch-2.pl create mode 100644 challenge-074/jeongoon/raku/ch-1.raku create mode 100644 challenge-074/jeongoon/raku/ch-2.raku diff --git a/challenge-074/jeongoon/common-lisp/ch-2.lsp b/challenge-074/jeongoon/common-lisp/ch-2.lsp index 0122b94794..77a936db66 100644 --- a/challenge-074/jeongoon/common-lisp/ch-2.lsp +++ b/challenge-074/jeongoon/common-lisp/ch-2.lsp @@ -1,8 +1,8 @@ ;; 1. sort and delete-duplicates the copy of sequence ;; 2. while traversing sublist of original data from the beginning ; a. reverse the sublist -;; b. find each chacter from the original datat twice (a) -;; c. if the chacter found only once (NR :Not Repeating) +;; b. find each character from the original data twice (a) +;; c. if the character found only once (NR :Not Repeating) ;; make the pair of (character, length of right hand side result) ;; d. if the length is less than previous one, replace orginal data, ;; else leave it diff --git a/challenge-074/jeongoon/haskell/ch-1.hs b/challenge-074/jeongoon/haskell/ch-1.hs index 0dea499be8..b5b37059bd 100644 --- a/challenge-074/jeongoon/haskell/ch-1.hs +++ b/challenge-074/jeongoon/haskell/ch-1.hs @@ -70,7 +70,7 @@ main = do solution = getMajor (snd ex) in do -- XXX: even though I didn't intend to make majority number - -- radom number list could have majority number + -- randon list could contain a majority number. putStr $ "Input: " ++ (if majorInfoExist then "Major = " ++ show majorNum else "*Maybe* No Major" ) ++ ": " diff --git a/challenge-074/jeongoon/haskell/ch-2.hs b/challenge-074/jeongoon/haskell/ch-2.hs index 9eb3593795..e888379f2c 100644 --- a/challenge-074/jeongoon/haskell/ch-2.hs +++ b/challenge-074/jeongoon/haskell/ch-2.hs @@ -26,7 +26,7 @@ main = do arg <- getRecord "Challenge #074 - Task #2" putStrLn ( "Input: " ++ arg :: [Char] ) putStr "Output: " - print $ findLNR arg + putStrLn $ findLNR arg {- diff --git a/challenge-074/jeongoon/perl/ch-1.pl b/challenge-074/jeongoon/perl/ch-1.pl new file mode 100644 index 0000000000..9353563837 --- /dev/null +++ b/challenge-074/jeongoon/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*- +# -*- coding: utf-8 -*- + +use strict; use warnings; + +# solution +sub get_major (@) { + @_ == 0 and return -1; + @_ == 1 and return $_[0]; + + my @sorted = sort @_; + my $halflen = int( .5 * @sorted ); # (halflen)gth + my $pnum = shift @sorted; # (p)revious (num)ber + my $pcnt = 1; # (p)revious (c)ou(nt) + + my $result = -1; + for my $cnum ( @sorted ) { + if ( $pnum == $cnum ) { + ++$pcnt > $halflen and ( $result = $cnum, last ); + } + else { + ( $pnum, $pcnt ) = ( $cnum, 1 ); + } + } + $result +} + +# testing +sub unsort ( @ ) { sort { (-1,1)[(rand 1)+0.46] } @_ } +sub ssprintf ($$) { sprintf "%#$_[0]s", $_[1] } +sub map_ssprintf { map { sprintf "%#$_[0]s", $_ } @_[1..$#_] } + +sub print_count (@) { + my $num = shift; + my $len = shift; + my $cnt = 0; + my @count = map { $_ == $num ? ++$cnt : ' ' } @_; + local $" = ' '; + print "Count (num: @{[ssprintf $len,$num]}): @{[map_ssprintf $len,@count]}\n"; +} + + +package main; + +my $S = shift || 10; +my $ensure_major = int .5 + rand 1; +my $major_number = int rand $S; +my $half_length = int .5 * $S; +my $L = length $S; + +my @sample = unsort ( ( $major_number ) x ( $half_length + 1), + (map{ int rand $S } 0.. $half_length - 1) ); + +$" = ','; +print "Sample (size: @{[ssprintf $L,$S]}): @{[map_ssprintf $L,@sample]}\n"; + + +my $maybe_major = get_major @sample; +print "Output: $maybe_major"; +$maybe_major != -1 + and ( print( " where\n" ), print_count $maybe_major, $L, @sample ) + or print$/; +( $ensure_major and $maybe_major == 1) + and ( print( " however\n" ), print_count $major_number, $L, @sample ); diff --git a/challenge-074/jeongoon/perl/ch-2.pl b/challenge-074/jeongoon/perl/ch-2.pl new file mode 100644 index 0000000000..c9f619e7e2 --- /dev/null +++ b/challenge-074/jeongoon/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl +# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*- +# -*- coding: utf-8 -*- + +use strict; use warnings; + +sub uniq_sorted { + my %mem = (); + map { exists $mem{$_} ? () : ($mem{$_} = $_) } sort @_ +} + +sub printLNR ($) { + my $str = shift; + my @candi = uniq_sorted( split '', $str ); + + for my $last_idx ( 1 .. length $str ) { + my $sub_chars = substr $str, 0, $last_idx; + + my $nr_pos = -1; + my $nr_chr = '#'; + for my $c ( @candi ) { + my @chunks = split $c, "$sub_chars\0"; + next unless @chunks == 2; + + my $new_pos = length $chunks[0]; + $nr_pos < $new_pos and ( $nr_pos = $new_pos, + $nr_chr = $c ); + } + print "$nr_chr"; + } + print $/; +} + +*printFNR = \&printLNR; + +package main; + +my $sample = $ARGV[0] ||'xyzzyx'; +print "Input: $sample\n"; +print "Output: "; +printFNR( $sample ); diff --git a/challenge-074/jeongoon/raku/ch-1.raku b/challenge-074/jeongoon/raku/ch-1.raku new file mode 100644 index 0000000000..a306b1b2fb --- /dev/null +++ b/challenge-074/jeongoon/raku/ch-1.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku +# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*- +# vim: set et ts=4 sw=4: + +use v6.d; + +# solution +sub get-major ( Int @list ) { + given @list.elems { + when 0 { -1.return } + when 1 { @list.first.return } + } + + my @sorted = @list.sort; + my $half-len = @sorted.elems * 0.5 .floor; + my ( $pnum, $pcnt, $res ) = @sorted.shift, 1, -1; + + for @sorted -> $cnum { + if $pnum == $cnum { if ++$pcnt > $half-len { $res = $cnum, last } } + else { ( $pnum, $pcnt ) = ( $cnum, 1 ); } + } + $res +} + +# testing +sub MAIN ( \S where * > 0 = 10 #= sample size + ) { + my $ensure-major = Bool.pick; + my $major-number = S.rand .floor; + my $half-length = .5 * S .floor; + + my @sample = $ensure-major ?? ( flat $major-number xx ( $half-length+1 ) + , | (0..S).pick( $half-length-1 ) ) + !! (0..S).pick(S); + my $major = get-major( Array[Int].new( @sample ) ); + say "Sample: {@sample.raku}"; + say "Major : $major"; +} diff --git a/challenge-074/jeongoon/raku/ch-2.raku b/challenge-074/jeongoon/raku/ch-2.raku new file mode 100644 index 0000000000..df90bbe567 --- /dev/null +++ b/challenge-074/jeongoon/raku/ch-2.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku +# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*- +# vim: set et ts=4 sw=4: + +use v6.d; + +# solution +role fnr { + method sayLNR ( Str:D $str = self.Str ) { + my @chars = $str.comb; + my @candi = @chars.unique.sort; + + for 1 .. @chars.elems -> $last-index { + my $sub-chars = $str.substr( 0, $last-index ); + my $nr-pos = -1; + my $nr-char = '#'; + for @candi -> $c { + given $sub-chars.indices( $c ) { + .elems == 1 or next; + $nr-pos < .tail and ( $nr-char = $c, $nr-pos = .tail ); + } + } + print $nr-char; + } + say ""; + } +} + +sub MAIN ( + Str:D $sample where *.chars > 0 = 'xyzzyx' #= string to find FNR +) { + my $fnr-string = $sample does fnr; + + "Input: ".print; + $fnr-string.say; + "Output: ".print; + $fnr-string.sayLNR; +} -- cgit From 3b660bd6b0556f43ee01a2f62c2aeb5611292c66 Mon Sep 17 00:00:00 2001 From: Myoungjin JEON Date: Thu, 20 Aug 2020 22:04:20 +1000 Subject: [ch-074/jeongoon] Perl and Raku solution --- challenge-074/jeongoon/perl/ch-2.pl | 2 +- challenge-074/jeongoon/raku/ch-2.raku | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-074/jeongoon/perl/ch-2.pl b/challenge-074/jeongoon/perl/ch-2.pl index c9f619e7e2..251885f8d9 100644 --- a/challenge-074/jeongoon/perl/ch-2.pl +++ b/challenge-074/jeongoon/perl/ch-2.pl @@ -6,7 +6,7 @@ use strict; use warnings; sub uniq_sorted { my %mem = (); - map { exists $mem{$_} ? () : ($mem{$_} = $_) } sort @_ + map { exists $mem{$_} ? () : ($mem{$_} = $_) } @_ } sub printLNR ($) { diff --git a/challenge-074/jeongoon/raku/ch-2.raku b/challenge-074/jeongoon/raku/ch-2.raku index df90bbe567..fba9030909 100644 --- a/challenge-074/jeongoon/raku/ch-2.raku +++ b/challenge-074/jeongoon/raku/ch-2.raku @@ -8,7 +8,7 @@ use v6.d; role fnr { method sayLNR ( Str:D $str = self.Str ) { my @chars = $str.comb; - my @candi = @chars.unique.sort; + my @candi = @chars.unique; for 1 .. @chars.elems -> $last-index { my $sub-chars = $str.substr( 0, $last-index ); @@ -17,7 +17,7 @@ role fnr { for @candi -> $c { given $sub-chars.indices( $c ) { .elems == 1 or next; - $nr-pos < .tail and ( $nr-char = $c, $nr-pos = .tail ); + $nr-pos < .tail and ( $nr-char = $c, $nr-pos = .tail ); } } print $nr-char; -- cgit From b5bc98033bc5881a638f62faba0484ef3cc61fb3 Mon Sep 17 00:00:00 2001 From: Myoungjin JEON Date: Thu, 20 Aug 2020 22:06:29 +1000 Subject: [ch-074/jeongoon] Perl and Raku solution --- challenge-074/jeongoon/haskell/ch-1.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-074/jeongoon/haskell/ch-1.hs b/challenge-074/jeongoon/haskell/ch-1.hs index b5b37059bd..34b45fd2d4 100644 --- a/challenge-074/jeongoon/haskell/ch-1.hs +++ b/challenge-074/jeongoon/haskell/ch-1.hs @@ -70,7 +70,7 @@ main = do solution = getMajor (snd ex) in do -- XXX: even though I didn't intend to make majority number - -- randon list could contain a majority number. + -- random list still could contain a majority number putStr $ "Input: " ++ (if majorInfoExist then "Major = " ++ show majorNum else "*Maybe* No Major" ) ++ ": " -- cgit