diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-20 12:19:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-20 12:19:51 +0100 |
| commit | 66139b10b7bf1d6a1e405cb79feb06f5b7c63883 (patch) | |
| tree | c88f84db0b8183394c96f74867ef8d6c48574bbf | |
| parent | a2c1aa1f1b160195bd812e981012bada1487a04b (diff) | |
| parent | b5bc98033bc5881a638f62faba0484ef3cc61fb3 (diff) | |
| download | perlweeklychallenge-club-66139b10b7bf1d6a1e405cb79feb06f5b7c63883.tar.gz perlweeklychallenge-club-66139b10b7bf1d6a1e405cb79feb06f5b7c63883.tar.bz2 perlweeklychallenge-club-66139b10b7bf1d6a1e405cb79feb06f5b7c63883.zip | |
Merge pull request #2111 from jeongoon/ch-074
[ch-074/jeongoon] Perl and Raku solution
| -rw-r--r-- | challenge-074/jeongoon/common-lisp/ch-2.lsp | 4 | ||||
| -rw-r--r-- | challenge-074/jeongoon/haskell/ch-1.hs | 2 | ||||
| -rw-r--r-- | challenge-074/jeongoon/haskell/ch-2.hs | 2 | ||||
| -rw-r--r-- | challenge-074/jeongoon/perl/ch-1.pl | 65 | ||||
| -rw-r--r-- | challenge-074/jeongoon/perl/ch-2.pl | 41 | ||||
| -rw-r--r-- | challenge-074/jeongoon/raku/ch-1.raku | 38 | ||||
| -rw-r--r-- | challenge-074/jeongoon/raku/ch-2.raku | 38 |
7 files changed, 186 insertions, 4 deletions
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..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 - -- radom number list could have majority number + -- random list still 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..251885f8d9 --- /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{$_} = $_) } @_ +} + +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..fba9030909 --- /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; + + 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; +} |
