diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-07-12 23:20:05 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-07-12 23:20:05 +1000 |
| commit | 61fa134fc4580e5e0e2ca009852283e250da7814 (patch) | |
| tree | 67261d69e5888ce329797b3787881c704424f9c2 | |
| parent | 29cb8af4fcd8e3de5009ec0c770ee69198d4b69e (diff) | |
| download | perlweeklychallenge-club-61fa134fc4580e5e0e2ca009852283e250da7814.tar.gz perlweeklychallenge-club-61fa134fc4580e5e0e2ca009852283e250da7814.tar.bz2 perlweeklychallenge-club-61fa134fc4580e5e0e2ca009852283e250da7814.zip | |
add raku solution task1 task2
| -rw-r--r-- | challenge-068/jeongoon/raku/ch-1.raku | 93 | ||||
| -rw-r--r-- | challenge-068/jeongoon/raku/ch-2.raku | 160 |
2 files changed, 253 insertions, 0 deletions
diff --git a/challenge-068/jeongoon/raku/ch-1.raku b/challenge-068/jeongoon/raku/ch-1.raku new file mode 100644 index 0000000000..6d79227d62 --- /dev/null +++ b/challenge-068/jeongoon/raku/ch-1.raku @@ -0,0 +1,93 @@ +#!/usr/bin/env raku +# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*- +# vim: set et ts=4 sw=4: + +class SomeZeroMatrix { + has Array @!rows; + has Bool @!row-all-zero-flags; + has Bool @!col-one-flags; + has Int $!col-count; + + submethod BUILD ( Int :$rows where { $rows > 2 } = 3, + Int :$columns where { $columns > 2 and $rows.defined } = 3 ) { + $!col-count = $columns; + self!purge-col-one-flags; + self.push-random-row( :times( $rows ) ); + } + + method !purge-col-one-flags () { + @!col-one-flags = ( True xx $!col-count ); + } + method !max-nrows-to-zero { return (@!rows.elems * 0.5 ).ceiling } + + method push-random-row( Int :$times where { $times > 0 } ) { + for ^$times { + my @values = + @!row-all-zero-flags.sum <= self!max-nrows-to-zero + ?? (( 0 xx 1, 1 xx 9 ).flat.pick xx $!col-count ) + !! ( 1 xx $!col-count ); + + self!push-row( @values ); + } + return self; + } + + method !push-row( @values ) { + push @!rows, @values; + + if $!col-count != @values.elems { + warn "column count is not the same as count of new values"; + $!col-count max= @values.elems; + } + + push @!row-all-zero-flags, ( @values.any == 0 ).Bool; + if $!col-count != @!col-one-flags.elems { + # unmatched length of data between previous and current rows + # increase them + @!col-one-flags.splice: *, *, ( True xx ($!col-count - @!col-one-flags.elems) ).flat; + } + @!col-one-flags = @!col-one-flags Z&& @values.map( *.Bool ); + } + + method print-rows-orig () { + $*ERR.say: "Input: "; + for @!rows -> $r { $*ERR.say: ~ $r.Array.raku; } + return self; + } + + method print-rows () { + $*ERR.say: "Output: "; + for @!row-all-zero-flags -> $all-zero { + if $all-zero { + say ( 0 xx $!col-count ).Array.raku; + } + else { + say @!col-one-flags.map( *.Int ).Array.raku; + } + } + $*ERR.say; + return self; + } + + method empty-rows () { + @!rows = (); + @!row-all-zero-flags = (); + self!purge-col-one-flags(); + return self; + } +} + +sub MAIN ( + Int :$R where { $R > 2 } = 3, #= number of rows + Int :$C where { $C > 2 } = 3, #= number of columns + Int :$round is copy where { $round > 0 } = 1 # number of rounds to run +) { + my SomeZeroMatrix $matrix .= new( :rows($R) :columns($C) ); + while ( $round-- > 0 ) { + $matrix. + print-rows-orig(). + print-rows(). + empty-rows(). + push-random-row( :times( $R ) ); + } +} diff --git a/challenge-068/jeongoon/raku/ch-2.raku b/challenge-068/jeongoon/raku/ch-2.raku new file mode 100644 index 0000000000..328cb1c4c3 --- /dev/null +++ b/challenge-068/jeongoon/raku/ch-2.raku @@ -0,0 +1,160 @@ +#!/usr/bin/env perl6 +# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*- +# vim: set et ts=4 sw=4: + +our $SeparatorStr = ''; +sub find-seprator-from-str ( Str $line ) { + # finding separator + my @words = $line.split( /<space>/ ); + my %words-score; + + my $max-checking = 20; + for @words -> $w { + if $w (elem) qw"-> => →" { + %words-score{$w} += 5; + } + else { + ++%words-score{$w}; + } + } + return $SeparatorStr = reduce { %words-score{$^a} > %words-score{$^b} + ?? $^a !! $^b }, %words-score.keys; +} + +class LinkedList { ... } +class Iter { + trusts LinkedList; + has $.value; + has Iter $!next; + + method clone () { + my $cloned = Iter.new( :$.value ); + $cloned!set-next( $!next ); + return $cloned; + } + + submethod BUILD ( Any:D :$value ) { + $!value = $value; + } + + method next () { $!next } + + method is-last () { $!next.defined.not } + method set-value ( Any:D $value ) { + $.value = $value; + } + + method !set-next ( Iter $another ) { $!next = $another } + method !reset-next () { $!next = Nil } + method !replace-next ( Iter:D $another ) { + my $prev-one = $!next; + $!next = $another; + return $prev-one; + } +} + +class LinkedList { + has Iter $!first; + + method front () { return $!first } + method count () { + my $count = 0; + loop ( my $itr = $!first; defined $itr; $itr = $itr.next ) { + ++$count + } + return $count; + } + + # push back after an iterator and return another iterator moved forward + multi method push-back ( + Iter:D :$after, + Iter:D :$node + ) returns Iter { + $node!Iter::set-next( $after!Iter::replace-next( $node ) ); + return $node; + } + multi method push-back ( + Iter:D :$after where { $after.next.defined.not }, + Any:D :$value + ) returns Iter { + my $new = Iter.new( :$value ); + $new!Iter::set-next( $after!Iter::replace-next( $new ) ); + return $new; + } + + multi method push-back ( Iter:U :$after, Any:D :$value + ) returns Iter { return $!first = Iter.new( :$value ) } + multi method pop-back ( + Iter:U :$after ) returns Iter { return Nil } + multi method pop-back ( + Iter:D :$after ) returns Iter { + my $go = $after.next; + if $go.defined { + $after!Iter::set-next( $go.next ); + $go!Iter::reset-next(); + } + return $go; + } + + method print-values ( Str :$prompt ) { + if $prompt.defined { + $*ERR.print( $prompt ); + } + my $itr = $!first; + $itr.value.print; + loop ( $itr = $itr.next ; ; $itr = $itr.next ) { + print( " {$SeparatorStr} " ~ $itr.value ); + last if $itr.is-last; + } + say ""; + } + + method move-as-mentioned ( Int :$count-if-known ) { + my $left-pos = 0; + my Iter $li = self.front; # left iterator + + my $count = $count-if-known // self.count; + loop ( my $round = ( $count * 0.5 ).floor; $round > 0; --$round ) { + my $ri2 = $li; # ri2: 2nd last iterator from right + my $n-repeat = $count -1 - $left-pos; + while ( --$n-repeat > 0 ) { $ri2 = $ri2.next; } + my Iter $ri = self.pop-back( :after( $ri2 ) ); + self.push-back( :after($li), :node($ri) ); + $li = $li.next.next; + + $left-pos += 2; + } + } +}; + + +sub MAIN ( + Bool :$stdin = False #= read linked list from stdin +) { + my $example-str = "1 → 2 → 3 → 4 → 5 → 6 → 7"; + my $input = ""; + + if $stdin { + $*ERR.print: + "Default: $example-str\n" + ~ "please input a linked list like above" + ~ " or press <enter> to use default values..\n" + ~ "Input: "; + $input = prompt(); + } + $input //= $example-str; + + my $sep = find-seprator-from-str( $input ); + + my @given-list = $input.split( / <space>* $sep <space>* / ); + my $List = LinkedList.new; + + my $itr = $List.front; + for @given-list -> $value { + $itr = $List.push-back( :after($itr), :$value ); + } + + $List.print-values( :prompt("Input:\n") ); + $List.move-as-mentioned(); + $List.print-values( :prompt("Output:\n" ) ); +} |
