diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2020-12-04 13:15:56 -0500 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2020-12-04 13:15:56 -0500 |
| commit | 67a4705dbff021860fb0db52aa1202184385693c (patch) | |
| tree | d43f457e3f9e139d037def5c935fac1f0628f500 /challenge-068/stuart-little | |
| parent | d19b0f983bbefca06f6139624711c079ac18eb6e (diff) | |
| download | perlweeklychallenge-club-67a4705dbff021860fb0db52aa1202184385693c.tar.gz perlweeklychallenge-club-67a4705dbff021860fb0db52aa1202184385693c.tar.bz2 perlweeklychallenge-club-67a4705dbff021860fb0db52aa1202184385693c.zip | |
1st commit on 068
Diffstat (limited to 'challenge-068/stuart-little')
| -rw-r--r-- | challenge-068/stuart-little/README | 1 | ||||
| -rwxr-xr-x | challenge-068/stuart-little/raku/ch-1.p6 | 18 | ||||
| -rwxr-xr-x | challenge-068/stuart-little/raku/ch-2.p6 | 104 |
3 files changed, 123 insertions, 0 deletions
diff --git a/challenge-068/stuart-little/README b/challenge-068/stuart-little/README new file mode 100644 index 0000000000..78439907de --- /dev/null +++ b/challenge-068/stuart-little/README @@ -0,0 +1 @@ +Solutions by Stuart Little diff --git a/challenge-068/stuart-little/raku/ch-1.p6 b/challenge-068/stuart-little/raku/ch-1.p6 new file mode 100755 index 0000000000..d1080b972e --- /dev/null +++ b/challenge-068/stuart-little/raku/ch-1.p6 @@ -0,0 +1,18 @@ +#!/usr/bin/env perl6 +use v6; + +sub transp(@a) { @a[0].keys.map({ @a[0..*-1;$_] }) } +sub zero_rows(@a) { @a.map({ ([*] @($_)) xx $_ }).map(*.Array) } + +my @a=@*ARGS.map(*.comb.Array); +for (zip @a.&zero_rows, @a.&transp.&zero_rows.&transp, :with({ @($^a) Z* @($^b) })) {.say} + +=finish + +Run as <script> <space-separated binary strings>, each binary string representing a row, as in + +<script> 101 111 111 to input the matrix + +[1, 0, 1] +[1, 1, 1] +[1, 1, 1] diff --git a/challenge-068/stuart-little/raku/ch-2.p6 b/challenge-068/stuart-little/raku/ch-2.p6 new file mode 100755 index 0000000000..cba704d38b --- /dev/null +++ b/challenge-068/stuart-little/raku/ch-2.p6 @@ -0,0 +1,104 @@ +#!/usr/bin/env perl6 +use v6; + +# run as <script> <space-separated items of the initial list> + +class Node is export { + has Real $.data; + has Node $.next is rw; +} + +class Sll is export { + has Node $.head is rw; + + method show() { + my $node = self.head; + (! $node) && do {say ""; return}; + while ($node.next) { + print $node.data, " -> "; + $node=$node.next; + } + say $node.data; + } + + method len() { + my $node = self.head; + my $len=0; + while ($node) { + $len++; + $node=$node.next; + } + return $len; + } + + multi method insert(Real $data) { + my $node=self.head; + (! $node) && do { + self.head=Node.new(data => $data); + return; + } + while ($node.next) { + $node=$node.next; + } + $node.next=Node.new(data => $data); + } + + multi method insert(0, Real $data) { + my $node=Node.new(data => $data, next => self.head); + self.head=$node; + } + + multi method insert(Int $idx where * > 0, Real $data) { + given self.len { + when 0 { self.insert(0,$data) } + when 1..$idx { self.insert($data) } + default { + my $cur=self.head; + my $node=Node.new(data => $data); + for (0..^($idx-1)) { + $cur=$cur.next; + } + $node.next=$cur.next; + $cur.next=$node; + } + } + } + + method del(Int $idx where * >= 0) { + given $idx { + when self.len..* { warn "Index out of range" } + when 0 { self.head=self.head.next } + default { + my $cur=self.head; + for (^($idx-1)) { + $cur=$cur.next; + } + $cur.next=$cur.next.next; + } + } + } +} + +sub foldback(Sll $ll) { + my $len=$ll.len; + + my $curr=$ll.head; + for (1..($len div 2)) { $curr = $curr.next } + for (1..(($len-1) div 2)) -> $offset { + my $data=$curr.next.data; + $ll.del(($len div 2)+$offset); + $ll.insert((($len-1) div 2)-($offset-1),$data); + } + return $ll; +} + +my $ll=Sll.new(); +for (@*ARGS.map(*.Real)) { + $ll.insert($_); +} + +print "Initial list: "; +$ll.show(); + +print "Folded list: "; +foldback($ll).show(); |
