From 414a5163292c0c26d80850124dac7ed89cafcbf9 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Mon, 4 May 2020 18:44:04 +0800 Subject: Rename ch-1.cl to challenge-059/cheok-yin-fung/lisp/ch-1.cl --- challenge-059/cheok-yin-fung/lisp/ch-1.cl | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-059/cheok-yin-fung/lisp/ch-1.cl (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/lisp/ch-1.cl b/challenge-059/cheok-yin-fung/lisp/ch-1.cl new file mode 100644 index 0000000000..e78c8e53c1 --- /dev/null +++ b/challenge-059/cheok-yin-fung/lisp/ch-1.cl @@ -0,0 +1,29 @@ +;(setf *line* '(1 4 3 2 5 2)) +;(setf *K* 3) +(setf *line* '(5 6 3 2 7 9)) +(setf *K* 6) + +(setf *small* nil) +(setf *large* nil) + +(defun newas (R) + (append *small* (cons R nil)) +) + +(defun newal (R) + (append *large* (cons R nil)) +) + +(defun biway (R) + (if (> *K* R) + (setf *small* (newas R )) + (setf *large* (newal R )) + ) +) + +(loop for R in *line* + do (biway R) +) + +(print (append *small* *large*)) + -- cgit From ba861d6f8c5c0920f8bfc948c87047d2ad7bd0e9 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Mon, 4 May 2020 18:45:07 +0800 Subject: Rename ch-1.pl to challenge-059/cheok-yin-fung/perl/ch-1.pl --- challenge-059/cheok-yin-fung/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-059/cheok-yin-fung/perl/ch-1.pl (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/perl/ch-1.pl b/challenge-059/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..3ade87fe43 --- /dev/null +++ b/challenge-059/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use strict; +use Test::Simple tests => 3; + + +sub newlist { + my @small = (); + my @large = (); + my ($K, @line) = @_; + foreach(@line) { + if ($_ < $K) { + push @small, $_; + } + else { + push @large, $_; + } + } + + return (@small, @large); +} + +print join "," , newlist @ARGV; +print "\n"; + +ok (newlist(3, 1, 4, 3, 2, 5, 2))==(1, 2, 2, 4, 3, 5) , "bad condition"; +ok (newlist(6,5,6,3,2,7,9)) == (5,3,2,6,7,9), "bad"; +ok (newlist(2 , 3, 2, 1)) == (1, 3, 2), "too bad"; + -- cgit From fe5f04e11db88e9cc57d20d5827396252efc0577 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Tue, 5 May 2020 14:47:23 +0800 Subject: Rename ch-2.pl to challenge-059/cheok-yin-fung/perl/ch-2.pl --- challenge-059/cheok-yin-fung/perl/ch-2.pl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 challenge-059/cheok-yin-fung/perl/ch-2.pl (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/perl/ch-2.pl b/challenge-059/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..420c20d172 --- /dev/null +++ b/challenge-059/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use strict; +use Test::Simple tests => 2; +use List::Util qw/max sum/; + +#reference: https://www.tutorialspoint.com/perl/bitwise_operators_example.htm + + +sub f { # compare two bit-strings by xor + my $hint = ($_[0] ^ $_[1]) ; + my $ans = sum (split //, sprintf("%b", $hint)); + return $ans; +} + +# first parameter: number of terms; remaining: terms in the list +sub conclude { + my ($N, @val) = @_; + my $sum = 0; + for my $i (0..$N-2) { + for my $j ($i+1..$N-1) { + $sum += f($val[$i], $val[$j]); + } + } + return $sum; +} + + +ok (conclude(3,2,3,4) == 6), "bad"; +ok (conclude(2,1,3)==1), "too bad"; + + -- cgit From 6cc990ac20b40b91b48dc9ba1383415de5413c5b Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 5 May 2020 10:38:34 +0100 Subject: - Updated solutions by Javier Luque. --- challenge-059/javier-luque/perl/ch-1.pl | 84 +++++++++++++++----------------- challenge-059/javier-luque/raku/ch-1.p6 | 86 ++++++++++++++++----------------- 2 files changed, 82 insertions(+), 88 deletions(-) (limited to 'challenge-059') diff --git a/challenge-059/javier-luque/perl/ch-1.pl b/challenge-059/javier-luque/perl/ch-1.pl index 0672fdac2a..3bc8fb057d 100644 --- a/challenge-059/javier-luque/perl/ch-1.pl +++ b/challenge-059/javier-luque/perl/ch-1.pl @@ -57,57 +57,49 @@ sub create_list { sub partition_list { my ($self, $k) = @_; - # Temp variables to store node locations - my $k_node_current; - my $k_node_first; - my $before_node_current; - my $before_node_first; - my $after_node_current; - my $after_node_first; - # Loop through the nodes my $node = $self->first; - while ($node) { - if ($node->value == $k) { - if ($k_node_current) { - $k_node_current->next($node); - $k_node_current = $node; - } else { - $k_node_first = $node; - $k_node_current = $node; - } - } + my $passed_k = 0; + my $prev_node; + my $k_node; - # Process the nodex lower than k - if ($node->value < $k) { - if ($before_node_current) { - $before_node_current->next($node); - $before_node_current = $node; - } else { - $before_node_first = $node; - $before_node_current = $node; - } - } + while ($node) { + my $next_node = $node->next; + my $moved_node = 0; - # Process the nodex higher than k - if ($node->value > $k) { - if ($after_node_current) { - $after_node_current->next($node); - $after_node_current = $node; - } else { - $after_node_first = $node; - $after_node_current = $node; + if ($node->value < $k && $passed_k) { + my $traverse_node = $self->first; + while ($traverse_node->next->value < $node->value) { + $traverse_node = $traverse_node->next; } + $prev_node->next($node->next); + $node->next($traverse_node->next); + $traverse_node->next($node); + $moved_node = 1; } - $node = $node->next; + # Other k's + if ($node->value == $k && $passed_k) { + my $temp = $k_node->next; + $prev_node->next($node->next); + $k_node->next($node); + $node->next($temp); + $moved_node = 1; + }; + + # First k encountered + if ($node->value == $k && !$passed_k) { + $passed_k = 1; + $k_node = $node; + }; + + # The prev node pointer only changes if we + # didn't move the node + $prev_node = $node unless ($moved_node); + + # Next node + $node = $next_node; } - - # link the chains - $self->first($before_node_first); - $before_node_current->next($k_node_first); - $k_node_current->next($after_node_first); - $after_node_current->next(undef); } sub display_list { @@ -138,4 +130,8 @@ say 'Before: ' . $ll->display_list; $ll->partition_list(3); say 'After: ' . $ll->display_list; - +say "\nDuplicate k's"; +$ll->create_list(1,4,3,2,5,2,3); +say 'Before: ' . $ll->display_list; +$ll->partition_list(3); +say 'After: ' . $ll->display_list; diff --git a/challenge-059/javier-luque/raku/ch-1.p6 b/challenge-059/javier-luque/raku/ch-1.p6 index ca4849b3a3..1a4843f812 100644 --- a/challenge-059/javier-luque/raku/ch-1.p6 +++ b/challenge-059/javier-luque/raku/ch-1.p6 @@ -27,58 +27,50 @@ class LinkedList { } } - method partition_list(Int $k) { - # Temp variables to store node locations - my $k_node_current; - my $k_node_first; - my $before_node_current; - my $before_node_first; - my $after_node_current; - my $after_node_first; - + method partition-list(Int $k) { # Loop through the nodes my $node = self.first; + my $passed_k = False; + my $prev_node; + my $k_node; + while ($node) { - if ($node.value == $k) { - if ($k_node_current) { - $k_node_current.next = $node; - $k_node_current = $node; - } else { - $k_node_first = $node; - $k_node_current = $node; - } - } + my $next_node = $node.next; + my $moved_node = False; - # Process the nodex lower than k - if ($node.value < $k) { - if ($before_node_current) { - $before_node_current.next = $node; - $before_node_current = $node; - } else { - $before_node_first = $node; - $before_node_current = $node; + if ($node.value < $k && $passed_k) { + my $traverse_node = self.first; + while ($traverse_node.next.value < $node.value) { + $traverse_node = $traverse_node.next; } + $prev_node.next = $node.next; + $node.next = $traverse_node.next; + $traverse_node.next = $node; + $moved_node = True; } - # Process the nodex higher than k - if ($node.value > $k) { - if ($after_node_current) { - $after_node_current.next = $node; - $after_node_current = $node; - } else { - $after_node_first = $node; - $after_node_current = $node; - } - } + # Other k's + if ($node.value == $k && $passed_k) { + my $temp = $k_node.next; + $prev_node.next = $node.next; + $k_node.next = $node; + $node.next = $temp; + $moved_node = True; + }; + + # First k encountered + if ($node.value == $k && !$passed_k) { + $passed_k = 1; + $k_node = $node; + }; + + # The prev node pointer only changes if we + # didn't move the node + $prev_node = $node unless ($moved_node); - $node = $node.next; + # Next node + $node = $next_node; } - - # link the chains - self.first = $before_node_first; - $before_node_current.next = $k_node_first; - $k_node_current.next = $after_node_first; - $after_node_current.next = Nil; } method display-list { @@ -99,6 +91,12 @@ sub MAIN() { my $ll = LinkedList.new(); $ll.create-list(1,4,3,2,5,2); say 'Before: ' ~ $ll.display-list; - $ll.partition_list(3); + $ll.partition-list(3); + say 'After: ' ~ $ll.display-list; + + say "\nDuplicate k's"; + $ll.create-list(1,4,3,2,5,2,3); + say 'Before: ' ~ $ll.display-list; + $ll.partition-list(3); say 'After: ' ~ $ll.display-list; } -- cgit From 6588912d49aa9fa99705a4c8de4f23fff2b41af7 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 5 May 2020 20:46:38 +0200 Subject: Include faster implementation of Bit Sum Don't count in pairs, count all at once. --- challenge-059/e-choroba/perl/ch-2.pl | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'challenge-059') diff --git a/challenge-059/e-choroba/perl/ch-2.pl b/challenge-059/e-choroba/perl/ch-2.pl index 6eee2084c1..f9cde1626c 100755 --- a/challenge-059/e-choroba/perl/ch-2.pl +++ b/challenge-059/e-choroba/perl/ch-2.pl @@ -19,6 +19,29 @@ sub chain_diff_bits { return $s } +sub chain_diff_bits2 { + my (@list) = @_; + my @binary = map { unpack 'b*', pack N => $_ } @list; + my $s = 0; + for my $pos (0 .. length($binary[0]) - 1) { + my $ones = grep $_, map { substr $_, $pos, 1 } @binary; + $s += $ones * (@list - $ones); + } + return $s +} + +sub chain_diff_bits3 { + my (@list) = @_; + my $s = 0; + my $mask = 1; + for (1 .. 8 * length $list[0]) { + my $ones = grep $mask & $_, @list; + $mask <<= 1; + $s += $ones * (@list - $ones); + } + return $s +} + use Test::More; is diff_bits(0, 0), 0, 'd 0 0'; @@ -32,6 +55,12 @@ is diff_bits(113, 68), 4, 'd 113 68'; is chain_diff_bits(2, 3, 4), 6, 'chain 2 3 4'; is chain_diff_bits(89, 106, 116), 12, 'chain 89, 106, 116'; +is chain_diff_bits2(2, 3, 4), 6, 'chain 2 3 4'; +is chain_diff_bits2(89, 106, 116), 12, 'chain 89, 106, 116'; + +is chain_diff_bits3(2, 3, 4), 6, 'chain 2 3 4'; +is chain_diff_bits3(89, 106, 116), 12, 'chain 89, 106, 116'; + =heading1 Last test explained 89 | 1 1 1 0 1 0 0 @@ -42,4 +71,18 @@ is chain_diff_bits(89, 106, 116), 12, 'chain 89, 106, 116'; =cut +my @l = map int rand 100, 1 .. 1000; + +is chain_diff_bits(@l), + chain_diff_bits2(@l), 'same 1 2'; +is chain_diff_bits2(@l), + chain_diff_bits3(@l), 'same 2 3'; + done_testing(); + +use Benchmark qw{ cmpthese }; +cmpthese(-2, { + new => sub { chain_diff_bits2(@l) }, + old => sub { chain_diff_bits(@l) }, + newest => sub { chain_diff_bits3(@l) } +}); -- cgit From 9d8c0bfc090ae806308f5b964d7876feed8a4eda Mon Sep 17 00:00:00 2001 From: Shawak Date: Wed, 6 May 2020 12:18:22 +0200 Subject: 059-02 by shawak --- challenge-059/shawak/README.md | 6 ++++++ challenge-059/shawak/ruby/ch-02.rb | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 challenge-059/shawak/README.md create mode 100644 challenge-059/shawak/ruby/ch-02.rb (limited to 'challenge-059') diff --git a/challenge-059/shawak/README.md b/challenge-059/shawak/README.md new file mode 100644 index 0000000000..e511d1c9b6 --- /dev/null +++ b/challenge-059/shawak/README.md @@ -0,0 +1,6 @@ +Solution by Shawak + +```bash +echo "2,3,4" | ruby ch-02.rb +``` + diff --git a/challenge-059/shawak/ruby/ch-02.rb b/challenge-059/shawak/ruby/ch-02.rb new file mode 100644 index 0000000000..c0eb71a461 --- /dev/null +++ b/challenge-059/shawak/ruby/ch-02.rb @@ -0,0 +1,7 @@ +a=gets.split(',').map &:to_i +a=a.combination(2).map{|a,b| [a.to_s(2), b.to_s(2)]} +puts a.sum{|a,b| + ([a.size, b.size].max-1).downto(0).count {|i| + a[i] == b[i] || !a[i] || !b[i] + } +} -- cgit From 0d01f67611f47df33dced823eb50a8a5e231dc8f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 6 May 2020 12:23:58 +0100 Subject: - Added Ruby solution by Shawak. --- challenge-059/shawak/ruby/ch-02.rb | 7 ------- challenge-059/shawak/ruby/ch-2.rb | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 challenge-059/shawak/ruby/ch-02.rb create mode 100644 challenge-059/shawak/ruby/ch-2.rb (limited to 'challenge-059') diff --git a/challenge-059/shawak/ruby/ch-02.rb b/challenge-059/shawak/ruby/ch-02.rb deleted file mode 100644 index c0eb71a461..0000000000 --- a/challenge-059/shawak/ruby/ch-02.rb +++ /dev/null @@ -1,7 +0,0 @@ -a=gets.split(',').map &:to_i -a=a.combination(2).map{|a,b| [a.to_s(2), b.to_s(2)]} -puts a.sum{|a,b| - ([a.size, b.size].max-1).downto(0).count {|i| - a[i] == b[i] || !a[i] || !b[i] - } -} diff --git a/challenge-059/shawak/ruby/ch-2.rb b/challenge-059/shawak/ruby/ch-2.rb new file mode 100644 index 0000000000..c0eb71a461 --- /dev/null +++ b/challenge-059/shawak/ruby/ch-2.rb @@ -0,0 +1,7 @@ +a=gets.split(',').map &:to_i +a=a.combination(2).map{|a,b| [a.to_s(2), b.to_s(2)]} +puts a.sum{|a,b| + ([a.size, b.size].max-1).downto(0).count {|i| + a[i] == b[i] || !a[i] || !b[i] + } +} -- cgit From 1f2dcb66b72603a6c730235fc9813105fb80d3e6 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 6 May 2020 14:51:19 +0100 Subject: - Added Perl solutions to the "Bit Sum" task. --- challenge-059/mohammad-anwar/perl/ch-2.pl | 38 ++++++++++++++++++++++++ challenge-059/mohammad-anwar/perl/ch-2a.pl | 46 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 challenge-059/mohammad-anwar/perl/ch-2.pl create mode 100644 challenge-059/mohammad-anwar/perl/ch-2a.pl (limited to 'challenge-059') diff --git a/challenge-059/mohammad-anwar/perl/ch-2.pl b/challenge-059/mohammad-anwar/perl/ch-2.pl new file mode 100644 index 0000000000..534646d355 --- /dev/null +++ b/challenge-059/mohammad-anwar/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Algorithm::Combinatorics qw(combinations); + +print &s([2, 3, 4]); + +sub s { + my ($A) = @_; + + my $sum = 0; + foreach my $pair (combinations($A, 2)) { + $sum += f(@$pair); + } + + return $sum; +} + +sub f { + my ($a, $b) = @_; + + $a = sprintf("%b", $a); + $b = sprintf("%b", $b); + + my $m = length($a) > length($b) ? length($a) : length($b); + my $f = '%0'.$m.'d'; + my @a = split //, sprintf $f, $a; + my @b = split //, sprintf $f, $b; + + my $bits = 0; + foreach (0..$m-1) { + $bits += 1 if ($a[$_] != $b[$_]); + } + + return $bits; +} diff --git a/challenge-059/mohammad-anwar/perl/ch-2a.pl b/challenge-059/mohammad-anwar/perl/ch-2a.pl new file mode 100644 index 0000000000..bed6b32f44 --- /dev/null +++ b/challenge-059/mohammad-anwar/perl/ch-2a.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +use Test::More; + +use Algorithm::Combinatorics qw(combinations); + +my $unit_tests = [ + { in => [1, 2, 3], out => 4 }, + { in => [2, 3, 4], out => 6 }, +]; + +foreach my $unit_test (@$unit_tests) { + is (&s($unit_test->{in}), $unit_test->{out}); +} + +done_testing; + +sub s { + my ($A) = @_; + + my $sum = 0; + foreach my $pair (combinations($A, 2)) { + $sum += f(@$pair); + } + + return $sum; +} + +sub f { + my ($a, $b) = @_; + + $a = sprintf("%b", $a); + $b = sprintf("%b", $b); + + my $m = length($a) > length($b) ? length($a) : length($b); + my $f = '%0'.$m.'d'; + my @a = split //, sprintf $f, $a; + my @b = split //, sprintf $f, $b; + + my $bits = 0; + foreach (0..$m-1) { + $bits += 1 if ($a[$_] != $b[$_]); + } + + return $bits; +} -- cgit From 6a73dd615847cb893ecadd3945454448d21423f0 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Wed, 6 May 2020 16:44:28 +0200 Subject: Initial --- challenge-059/markus-holzer/raku/ch-1.p6 | 38 +++++++ challenge-059/markus-holzer/raku/ch-2.p6 | 7 ++ .../markus-holzer/raku/lib/LinkedList/Simple.pm6 | 110 +++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 challenge-059/markus-holzer/raku/ch-1.p6 create mode 100644 challenge-059/markus-holzer/raku/ch-2.p6 create mode 100644 challenge-059/markus-holzer/raku/lib/LinkedList/Simple.pm6 (limited to 'challenge-059') diff --git a/challenge-059/markus-holzer/raku/ch-1.p6 b/challenge-059/markus-holzer/raku/ch-1.p6 new file mode 100644 index 0000000000..c84d1ab897 --- /dev/null +++ b/challenge-059/markus-holzer/raku/ch-1.p6 @@ -0,0 +1,38 @@ +use LinkedList::Simple; + +multi sub MAIN( Bool :$t ) +{ + test(); +} + +multi sub MAIN( Int:D :$k, *@n where { .elems > 1 && .all ~~ Int } ) +{ + say LinkedList::Simple + .from( |@n ) + .roughsort( * < $k ) + .flat + .join( '→' ); +} + +sub test +{ + use Test; + + my %tests = + '2-5-6-7-13-14-18' => [ 10, ( 2, 5, 13, 14, 6, 7, 18 ) ], + '2-5-6-7-13-14-18' => [ 10, ( 13, 2, 5, 14, 6, 7, 18 ) ], + '1-2-3-4-11-12-13-14' => [ 10, ( 1, 11, 2, 12, 3, 13, 4, 14 ) ], + '1-1-1-1-2-2-2-2' => [ 2, ( 1, 2, 1, 2, 1, 2, 1, 2 ) ], + '1-2-2-4-3-5' => [ 3, ( 1, 4, 3, 2, 5, 2 ) ] + ; + + my &test = { + ok LinkedList::Simple + .from( $^test.value[1].flat ) + .roughsort( * < $^test.value[0] ) + .flat + .join('-') eq $^test.key, $^test.key } + ; + + .&test for %tests; +} diff --git a/challenge-059/markus-holzer/raku/ch-2.p6 b/challenge-059/markus-holzer/raku/ch-2.p6 new file mode 100644 index 0000000000..35c3074e93 --- /dev/null +++ b/challenge-059/markus-holzer/raku/ch-2.p6 @@ -0,0 +1,7 @@ +sub MAIN( *@n where *.elems > 0 ) +{ + say [+] @n.combinations( 2 ).map: -> ( Int $a, Int $b ) + { + ( $a +^ $b ).base( 2 ).indices( 1 ) + } +} \ No newline at end of file diff --git a/challenge-059/markus-holzer/raku/lib/LinkedList/Simple.pm6 b/challenge-059/markus-holzer/raku/lib/LinkedList/Simple.pm6 new file mode 100644 index 0000000000..d6aeef33b7 --- /dev/null +++ b/challenge-059/markus-holzer/raku/lib/LinkedList/Simple.pm6 @@ -0,0 +1,110 @@ +role LinkedList::Simple::Node +{ + has $.next is rw; + + method LinkedList + { + LinkedList::Simple.new( :head( self ) ); + } + + multi method append( $new-value ) + { + self.append( $new-value but LinkedList::Simple::Node ) + } + + multi method append( LinkedList::Simple::Node $new-node ) + { + self.next = $new-node + } + + multi method perl() + { + nextsame + } +} + +role LinkedList::Simple does Iterable +{ + has LinkedList::Simple::Node $.head is rw; + + method from( *@nodes ) + { + self.bless( :@nodes ); + } + + multi submethod BUILD( LinkedList::Simple::Node :$head, :@nodes ) + { + $!head = my $node = $head // ( @nodes.shift but LinkedList::Simple::Node ); + $node = $node.append( $_ ) for @nodes; + } + + method sequence() + { + $!head, *.next ... ! *.next.defined + } + + method iterator() + { + self.sequence.iterator; + } + + multi method perl() + { + "LinkedList::Simple.from({ self.sequence.map( *.perl ).join(', ') });" + } + + method roughsort( Code $categorize ) + { + sub dismantle( &categorize ) + { + my $end; + my $start; + my $head-category = &categorize( self.head ); + + gather + { + for self.flat -> $node + { + if ( $head-category == $node.&categorize ) + { + take { :$start, :$end } with $end; + + $head-category .= not; + $start = $node; + } + + $end = $node; + } + + take { :$start, :$end }; + } + } + + sub assemble( $pieces ) + { + my $first-node; + my $last-frag; + + for $pieces.rotor( 2 => -1 ) -> ( $left, $right ) + { + FIRST $first-node = $left; + LAST $last-frag = $right; + + $left.next = $right; + } + + with $first-node + { + self.head = $first-node; + $last-frag.next = Any; + } + + self; + } + + assemble( + dismantle( *.$categorize ) + .sort( not *.$categorize ) ); + + } +} -- cgit From 9696e7432e589629b97079e21fc177d02e525c0d Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 6 May 2020 16:08:35 +0100 Subject: - Added Raku solutions to the "Bit Sum" task. --- challenge-059/mohammad-anwar/raku/ch-2.p6 | 38 +++++++++++++++++++++++++ challenge-059/mohammad-anwar/raku/ch-2a.p6 | 45 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-059/mohammad-anwar/raku/ch-2.p6 create mode 100644 challenge-059/mohammad-anwar/raku/ch-2a.p6 (limited to 'challenge-059') diff --git a/challenge-059/mohammad-anwar/raku/ch-2.p6 b/challenge-059/mohammad-anwar/raku/ch-2.p6 new file mode 100644 index 0000000000..2c50a0bdd7 --- /dev/null +++ b/challenge-059/mohammad-anwar/raku/ch-2.p6 @@ -0,0 +1,38 @@ +#!/usr/bin/env perl6 + +use v6.d; + +sub MAIN($A = [2, 3, 4]) { + say s($A); +} + +sub s($A) { + + my $sum = 0; + for $A.combinations(2) -> $pair { + $sum += f($pair[0], $pair[1]); + } + + return $sum; +} + +sub f(Int $a, Int $b) { + my Str $a_binary = $a.base(2); + my Str $b_binary = $b.base(2); + + my $m = max($a_binary.chars, $b_binary.chars); + my $f = '%0' ~ $m ~ 'd'; + $a_binary = sprintf($f, $a_binary); + $b_binary = sprintf($f, $b_binary); + my @a = $a_binary.comb; + my @b = $b_binary.comb; + + my $bits = 0; + for 0..$m-1 -> $i { + if @a[$i] !== @b[$i] { + $bits += 1; + } + } + + return $bits; +} diff --git a/challenge-059/mohammad-anwar/raku/ch-2a.p6 b/challenge-059/mohammad-anwar/raku/ch-2a.p6 new file mode 100644 index 0000000000..d055181825 --- /dev/null +++ b/challenge-059/mohammad-anwar/raku/ch-2a.p6 @@ -0,0 +1,45 @@ +#!/usr/bin/env perl6 + +use Test; + +my $unit_tests = [ + { in => [1, 2, 3], out => 4 }, + { in => [2, 3, 4], out => 6 } +]; + +for |$unit_tests -> $unit_test { + is s($unit_test.{"in"}), $unit_test.{"out"}; +} + +done-testing; + +sub s($A) { + + my $sum = 0; + for $A.combinations(2) -> $pair { + $sum += f($pair[0], $pair[1]); + } + + return $sum; +} + +sub f(Int $a, Int $b) { + my Str $a_binary = $a.base(2); + my Str $b_binary = $b.base(2); + + my $m = max($a_binary.chars, $b_binary.chars); + my $f = '%0' ~ $m ~ 'd'; + $a_binary = sprintf($f, $a_binary); + $b_binary = sprintf($f, $b_binary); + my @a = $a_binary.comb; + my @b = $b_binary.comb; + + my $bits = 0; + for 0..$m-1 -> $i { + if @a[$i] !== @b[$i] { + $bits += 1; + } + } + + return $bits; +} -- cgit From a84080ce47abd3416c3abfea30f07b5f59c03838 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 6 May 2020 23:04:58 +0100 Subject: - Added member Sangeet Kar. --- challenge-059/sangeet-kar/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-059/sangeet-kar/README (limited to 'challenge-059') diff --git a/challenge-059/sangeet-kar/README b/challenge-059/sangeet-kar/README new file mode 100644 index 0000000000..268cc9355d --- /dev/null +++ b/challenge-059/sangeet-kar/README @@ -0,0 +1 @@ +Solutions by Sangeet Kar. -- cgit From e3a7c5823a720c9324c931402e18dafd7f354a6f Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 6 May 2020 23:00:56 -0400 Subject: BLOGGING! --- challenge-059/dave-jacoby/blog.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 challenge-059/dave-jacoby/blog.txt (limited to 'challenge-059') diff --git a/challenge-059/dave-jacoby/blog.txt b/challenge-059/dave-jacoby/blog.txt new file mode 100644 index 0000000000..5238d6a896 --- /dev/null +++ b/challenge-059/dave-jacoby/blog.txt @@ -0,0 +1,2 @@ +https://jacoby.github.io/2020/05/04/challenge-59-lists-and-binary-xor.html +https://jacoby.github.io/2020/05/06/more-on-linked-lists.html -- cgit From 9859892106de52182108cf03728343d9f5a00994 Mon Sep 17 00:00:00 2001 From: saiftynet Date: Thu, 7 May 2020 12:53:38 +0100 Subject: Challenge-059 solutions by saiftynet --- challenge-059/saiftynet/perl/ch-1.pl | 31 +++++++++++++++++++++++++++++++ challenge-059/saiftynet/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-059/saiftynet/perl/ch-1.pl create mode 100644 challenge-059/saiftynet/perl/ch-2.pl (limited to 'challenge-059') diff --git a/challenge-059/saiftynet/perl/ch-1.pl b/challenge-059/saiftynet/perl/ch-1.pl new file mode 100644 index 0000000000..31e750d415 --- /dev/null +++ b/challenge-059/saiftynet/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/env/perl +# Task 1 Challenge 059 Solution by saiftynet + +# Linked List: PWC 059 +# k = 3# Expected Output: 1 → 2 → 2 → 4 → 3 → 5. +# given a "linked list" partition about a number, but retain +# relative order of other numbers + +my @l=qw/1 4 3 2 5 2/; + +print join "->",partition(3); + +sub partition{ + +# get the pivot and list + my ($k,@list)=@_; + +# prepare anonymnous lists containing equal, after and before numbers + my @seq=([],[],[]); + +# <=> return -1 if less (before), 0 if equal (pivot) and 1 of greater (after); +# use this result as an index to push the numbers into one of these lists + foreach my $t (@l){ + push @{$seq[$t<=>$k]},$t + }; + +# return partitioned data + return @{$seq[-1]},@{$seq[0]},@{$seq[1]}; + +} + diff --git a/challenge-059/saiftynet/perl/ch-2.pl b/challenge-059/saiftynet/perl/ch-2.pl new file mode 100644 index 0000000000..b8831a1e26 --- /dev/null +++ b/challenge-059/saiftynet/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/env/perl +# Task 2 Challenge 059 Solution by saiftynet +# -1 + +# Bitsum- sum of bits that are different. This is effectively and xor +# operation, followed by count of the bits that are 1. One can convert +# the yiled of the xor operation into a binar string (e.g. using +# sprintf "%b"), and counting the ones. Getting the list context yield +# of a match operation, the reading that in scalar context gives us what +# we want. A for loop that gets all possible pairs gives us a bit sum +# of a list of numbers + +print bitsum(2,3,4); +sub bitsum{ + my @list=@_; + my $sum=0; # accumulator + foreach my $m (0..@list-2){ # usual 2 fors to get + foreach my $n ($m+1..$#list){ # all combinations + $sum += # get scalar + ()= # cast intoa array + (sprintf "%b", $list[$m]^$list[$n])# covert to binary + =~m/1/g; # get matches + } + } + return $sum; +} -- cgit From 89c415f93d8c78b751ef86623b4ccf124c36ec98 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 7 May 2020 19:17:46 +0100 Subject: - Added blogs by Dave Jacoby. --- challenge-059/dave-jacoby/blog.txt | 1 - challenge-059/dave-jacoby/blog1.txt | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 challenge-059/dave-jacoby/blog1.txt (limited to 'challenge-059') diff --git a/challenge-059/dave-jacoby/blog.txt b/challenge-059/dave-jacoby/blog.txt index 5238d6a896..03c4a9d8fe 100644 --- a/challenge-059/dave-jacoby/blog.txt +++ b/challenge-059/dave-jacoby/blog.txt @@ -1,2 +1 @@ https://jacoby.github.io/2020/05/04/challenge-59-lists-and-binary-xor.html -https://jacoby.github.io/2020/05/06/more-on-linked-lists.html diff --git a/challenge-059/dave-jacoby/blog1.txt b/challenge-059/dave-jacoby/blog1.txt new file mode 100644 index 0000000000..76bb8f0583 --- /dev/null +++ b/challenge-059/dave-jacoby/blog1.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/05/06/more-on-linked-lists.html -- cgit From c8154136a370ca6bc9e7fbfebbbed52bde37606c Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Thu, 7 May 2020 21:50:04 +0200 Subject: solutions for challenge #059 --- challenge-059/jo-37/perl/ch-1.pl | 72 ++++++++++++++++++++++++++++++++++++++++ challenge-059/jo-37/perl/ch-2.pl | 15 +++++++++ 2 files changed, 87 insertions(+) create mode 100755 challenge-059/jo-37/perl/ch-1.pl create mode 100755 challenge-059/jo-37/perl/ch-2.pl (limited to 'challenge-059') diff --git a/challenge-059/jo-37/perl/ch-1.pl b/challenge-059/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..f9bbf97102 --- /dev/null +++ b/challenge-059/jo-37/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +# expects split limit in $ARGV[0] +# expects values to build linked list from in @ARGV[1..-1] + +use strict; +use warnings; + +# create linked list from given array ref +sub make_list { + my $values = shift; + my $head = {}; + my $prev = $head; + foreach my $val (@$values) { + my $this; + $this->{val} = $val; + $prev->{next} = $this; + $prev = $this; + } + return $head->{next}; +} + +# print linked list from given title and head ref +sub print_list { + my ($title, $head) = @_; + print $title, ":\t"; + for (my $this = $head; $this; $this = $this->{next}) { + print $this->{val}; + print '->' if $this->{next}; + } + print "\n"; +} + +# switch list +# limit given as 1st arg, +# linked list given by head ref in 2nd arg +# returns: ref to head of switched list +sub switch_list { + my ($lim, $head) = @_; + my $upper = {}; + my $uhead = $upper; + my $lower = {}; + my $lhead = $lower; + for (my $this = $head; $this; $this = $this->{next}) { + if ($this->{val} < $lim) { + $lower->{next} = $this; + delete $upper->{next}; + $lower = $this; + } else { + $upper->{next} = $this; + delete $lower->{next}; + $upper = $this; + } + } + # invalidate old head ref + undef $_[1]; + + if ($lhead->{next}) { + $lower->{next} = $uhead->{next}; + return $lhead->{next}; + } else { + return $uhead->{next}; + } +} + +# main +my $splitval = shift; +my $head = make_list \@ARGV; +print_list 'original', $head; + +my $switched = switch_list $splitval, $head; +print_list 'switched', $switched; diff --git a/challenge-059/jo-37/perl/ch-2.pl b/challenge-059/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..6bd296e10a --- /dev/null +++ b/challenge-059/jo-37/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +# expects input in @ARGV +# prints result to STDOUT +# numbers must be in the range 0 .. 2**64 - 1 + +use strict; +use warnings; + +my $sum; +while (defined (my $x = shift)) { + my $bits = pack 'Q', $x; + $sum += unpack '%64b*', $bits ^ $_ foreach map {pack 'Q', $_} @ARGV; +} +print $sum, "\n"; -- cgit From a0be6e85851b6da6425765237d1096704c2b55df Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Fri, 8 May 2020 00:33:12 +0200 Subject: Add link to E. Choroba's blog post about 059: Linked List + Bit Sum Simplify the code according to the blog post, too. --- challenge-059/e-choroba/blog.txt | 1 + challenge-059/e-choroba/perl/ch-2.pl | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 challenge-059/e-choroba/blog.txt (limited to 'challenge-059') diff --git a/challenge-059/e-choroba/blog.txt b/challenge-059/e-choroba/blog.txt new file mode 100644 index 0000000000..7692d6072b --- /dev/null +++ b/challenge-059/e-choroba/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/e_choroba/2020/05/perl-weekly-challenge-059-linked-list-and-bit-sum.html diff --git a/challenge-059/e-choroba/perl/ch-2.pl b/challenge-059/e-choroba/perl/ch-2.pl index f9cde1626c..5a8e182b01 100755 --- a/challenge-059/e-choroba/perl/ch-2.pl +++ b/challenge-059/e-choroba/perl/ch-2.pl @@ -4,8 +4,7 @@ use strict; sub diff_bits { my ($x, $y) = @_; - my ($bx, $by) = map { pack 'N', $_ } $x, $y; - return unpack '%32b*', $bx ^ $by + return unpack '%32b*', pack 'N', $x ^ $y } sub chain_diff_bits { -- cgit From f08bd246f0e8653d4eeb9a085ee1c9381e688b89 Mon Sep 17 00:00:00 2001 From: Sangeet Kar Date: Fri, 8 May 2020 12:56:53 +0200 Subject: Added solution to the weekly challenge 059 --- challenge-059/sangeet-kar/perl/Listy.pm | 47 +++++++++++++++++++++++++++++++++ challenge-059/sangeet-kar/perl/ch-1.pl | 47 +++++++++++++++++++++++++++++++++ challenge-059/sangeet-kar/perl/ch-2.pl | 16 +++++++++++ 3 files changed, 110 insertions(+) create mode 100644 challenge-059/sangeet-kar/perl/Listy.pm create mode 100644 challenge-059/sangeet-kar/perl/ch-1.pl create mode 100644 challenge-059/sangeet-kar/perl/ch-2.pl (limited to 'challenge-059') diff --git a/challenge-059/sangeet-kar/perl/Listy.pm b/challenge-059/sangeet-kar/perl/Listy.pm new file mode 100644 index 0000000000..eeb08c4d61 --- /dev/null +++ b/challenge-059/sangeet-kar/perl/Listy.pm @@ -0,0 +1,47 @@ +use strict; +use warnings; +use experimental qw(signatures); + +#List node class +package Node; + +sub new ($class, $val, $next_node=undef) { + bless {val => $val, next_node => $next_node}, $class; +} + +#LinkedList class +package Listy; + +sub new ($class, $list) { + my $head = my $last = undef; + + for (@$list) { + my $node = Node->new ($_); + if (defined $last) { + $last->{next_node} = $node; + $last = $node; + } + else { + $head = $last = $node; + } + } + bless {head => $head, last1 => $last}, $class +} + +sub is_empty ($self) { + not defined $self->{head}; +} + +sub print_list ($self) { + if ($self->is_empty) { + print "empty"; + } + else { + for (my $i = $self->{head}; defined $i; $i = $i->{next_node}) { + print $i->{val}; + print "->" unless $i == $self->{last1}; + } + } +} + +1; \ No newline at end of file diff --git a/challenge-059/sangeet-kar/perl/ch-1.pl b/challenge-059/sangeet-kar/perl/ch-1.pl new file mode 100644 index 0000000000..31586ff9ec --- /dev/null +++ b/challenge-059/sangeet-kar/perl/ch-1.pl @@ -0,0 +1,47 @@ +use strict; +use warnings; +use File::Basename; +use lib dirname (__FILE__); +use experimental qw(signatures); + +use Listy; + +sub find_parent ($lst, $node, $start_node=undef) { + $start_node //= $lst->{head}; + return $start_node if $start_node == $node; + + $start_node = $start_node->{next_node} while $start_node->{next_node} != $node; + return $start_node; +} + +sub shift_list ($lst, $i, $j) { + my $parent_i = find_parent $lst, $i; + my $parent_j = find_parent $lst, $j, $i; + + $parent_j->{next_node} = $j->{next_node}; + $lst->{last1} = $parent_j if $j == $lst->{last1}; + $j->{next_node} = $i; + if ($parent_i == $i) { + $lst->{head} = $j; + } + else { + $parent_i->{next_node} = $j; + } +} + +sub partition_list { + my ($lst, $k) = @_; + my $i = $lst->{head}; + $i = $i->{next_node} if defined $i && $i->{val} < $k; + return $lst unless defined $i; + + while (1) { + my $j = $i->{next_node}; + $j = $j->{next_node} while defined $j && $j->{val} >= $k; + return $lst unless defined $j; + shift_list($lst, $i, $j); + } +} + +my $lst = Listy->new ([1, 4, 3, 2, 5, 2]); +partition_list ($lst, 3) -> print_list; diff --git a/challenge-059/sangeet-kar/perl/ch-2.pl b/challenge-059/sangeet-kar/perl/ch-2.pl new file mode 100644 index 0000000000..44af4aa8aa --- /dev/null +++ b/challenge-059/sangeet-kar/perl/ch-2.pl @@ -0,0 +1,16 @@ +use 5.30.0; +use warnings; + +use List::Util qw(sum); +use Algorithm::Combinatorics qw(combinations); + +sub helper { + my ( $a, $b ) = @_; + sum( split //, sprintf( "%b", $a ) ^ sprintf( "%b", $b ) ); +} + +sub bitsum { + sum map {helper @$_} combinations \@ARGV, 2 +} + +print bitsum; -- cgit From 056dc1dcf18f3c417cce9d6cdf7cbfede9f2da26 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Fri, 8 May 2020 20:20:19 +0800 Subject: Delete ch-1.cl --- challenge-059/cheok-yin-fung/lisp/ch-1.cl | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 challenge-059/cheok-yin-fung/lisp/ch-1.cl (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/lisp/ch-1.cl b/challenge-059/cheok-yin-fung/lisp/ch-1.cl deleted file mode 100644 index e78c8e53c1..0000000000 --- a/challenge-059/cheok-yin-fung/lisp/ch-1.cl +++ /dev/null @@ -1,29 +0,0 @@ -;(setf *line* '(1 4 3 2 5 2)) -;(setf *K* 3) -(setf *line* '(5 6 3 2 7 9)) -(setf *K* 6) - -(setf *small* nil) -(setf *large* nil) - -(defun newas (R) - (append *small* (cons R nil)) -) - -(defun newal (R) - (append *large* (cons R nil)) -) - -(defun biway (R) - (if (> *K* R) - (setf *small* (newas R )) - (setf *large* (newal R )) - ) -) - -(loop for R in *line* - do (biway R) -) - -(print (append *small* *large*)) - -- cgit From 188b91e6f01090c705be44e0e3f50f406e0b4b77 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Fri, 8 May 2020 20:20:34 +0800 Subject: Delete ch-1.pl --- challenge-059/cheok-yin-fung/perl/ch-1.pl | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 challenge-059/cheok-yin-fung/perl/ch-1.pl (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/perl/ch-1.pl b/challenge-059/cheok-yin-fung/perl/ch-1.pl deleted file mode 100644 index 3ade87fe43..0000000000 --- a/challenge-059/cheok-yin-fung/perl/ch-1.pl +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/perl -use strict; -use Test::Simple tests => 3; - - -sub newlist { - my @small = (); - my @large = (); - my ($K, @line) = @_; - foreach(@line) { - if ($_ < $K) { - push @small, $_; - } - else { - push @large, $_; - } - } - - return (@small, @large); -} - -print join "," , newlist @ARGV; -print "\n"; - -ok (newlist(3, 1, 4, 3, 2, 5, 2))==(1, 2, 2, 4, 3, 5) , "bad condition"; -ok (newlist(6,5,6,3,2,7,9)) == (5,3,2,6,7,9), "bad"; -ok (newlist(2 , 3, 2, 1)) == (1, 3, 2), "too bad"; - -- cgit From d57751d69fca329eda59f26c2d7c12d486e16cfa Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Fri, 8 May 2020 20:20:53 +0800 Subject: Add files via upload --- challenge-059/cheok-yin-fung/perl/ch-1.pl | 121 ++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 challenge-059/cheok-yin-fung/perl/ch-1.pl (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/perl/ch-1.pl b/challenge-059/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..64e34adbb2 --- /dev/null +++ b/challenge-059/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,121 @@ +#!/usr/bin/perl +use strict; + +{ package item; + + sub new + { + my ($class) = @_; + bless { + _value => $_[1], + _next => $_[2], + }, $class; + } + + sub value { $_[0]->{_value}} + + sub inext { + my ($self, $newnext) = @_; + $self->{_next} = $newnext if defined($newnext); + return $self->{_next}; + } + + sub unlink { + my ($self) = @_; + $self->{_next} = undef; + return $self->{_next}; + } +} + +#my @input = (2,1, 2,3); +#my $K = 2; + +my @input = (5,6,3,2,7,9); +my $K = 6; + +#my @input = (1 , 4, 3, 2, 5, 2 ); +#my $K = 3; + +# my @input = (2 , 4, 3, 1, 5, 1 ); + # my $K = 3; + +my ($head, $smallpivot, $inusepivot); +my $ll; + + +sub make_linked_list { + my @temp_list = @_; + $ll->[$#temp_list] = item->new( $temp_list[$#temp_list] , \(undef) ); + for (reverse 0..$#temp_list-1) { + $ll->[$_] = item->new($temp_list[$_] , \( $ll->[$_+1] ) ); + } + $head = \($ll->[0]); +} + + + +my $HELPER = -1; +make_linked_list($HELPER, @input); + +$smallpivot = $head; +$inusepivot = ${$head}->inext; + +sub print_linked_listi { #for testing use + my $i = 0; + my $t = ${$head}; + while( $t = ${$t->inext} and $i<10) { + print $t->value, "->"; + $i++; + } + print "nil\n"; +} + +sub print_linked_list { + my $t = ${$head}; + while( $t = ${$t->inext} ) { + print $t->value, "->"; + } + print "nil\n"; +} + + +print "\nbefore: "; +print_linked_list; +print "\n"; + +my $j = 0; + +my $previous = $head; +while( 1) { #j is for testing help + + # print $$inusepivot->value, " " ,$$smallpivot->value, " \n"; + # print_linked_list;print "\n"; + + if ($$inusepivot->value < $K) { + $$previous->unlink; + $$previous->inext( $$inusepivot->inext ); + + $$inusepivot->unlink; + $$inusepivot->inext($$smallpivot->inext); + + $$smallpivot->unlink; + $$smallpivot->inext($inusepivot); + $smallpivot = $inusepivot; + + } + + + if (($$previous->inext != \undef) and ($$inusepivot->inext != \undef)) { + $previous = $inusepivot; + $inusepivot = $$inusepivot->inext; + } + else { + print "\nafter : "; + print_linked_list; + exit; + } + +} + + + -- cgit From 70e020d318fceb009ad1ed48e944e2fdeefa5f40 Mon Sep 17 00:00:00 2001 From: Leo Manfredi Date: Fri, 8 May 2020 21:10:59 +0200 Subject: Added solution Task #2 --- challenge-059/manfredi/perl/ch-2.pl | 38 +++++++++++++++++++++++++++++++++++ challenge-059/manfredi/python/ch-2.py | 24 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 challenge-059/manfredi/perl/ch-2.pl create mode 100755 challenge-059/manfredi/python/ch-2.py (limited to 'challenge-059') diff --git a/challenge-059/manfredi/perl/ch-2.pl b/challenge-059/manfredi/perl/ch-2.pl new file mode 100755 index 0000000000..f4fc17a63d --- /dev/null +++ b/challenge-059/manfredi/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my @numbers = ( 2, 4, 3 ); + +print("numbers: @numbers\n"); + +my @n = sort @numbers; + +my @pair = (); + +for my $i (0 .. $#n) { + for my $j ($i .. $#n) { + push @pair, [ $n[$i] , $n[$j] ] unless $n[$i] eq $n[$j]; + } +} + +my $bit_sum = 0; + +for my $pair (@pair) { + my $binA = sprintf("%032b", $pair->[0]); + my $binB = sprintf("%032b", $pair->[1]); + + my @binA = split '', $binA; + my @binB = split '', $binB; + my $diff = 0; + + for my $idx (0.. $#binA) { + $diff++ if $binA[$idx] != $binB[$idx]; + } + + printf "%s = %s\n%s = %s\n", $binA, $pair->[0], $binB,$pair->[1]; + printf "f(%s, %s) = %s\n\n", $pair->[0], $pair->[1], $diff; + $bit_sum += $diff; +} + +print "bit sum = $bit_sum\n"; diff --git a/challenge-059/manfredi/python/ch-2.py b/challenge-059/manfredi/python/ch-2.py new file mode 100755 index 0000000000..d758d9889c --- /dev/null +++ b/challenge-059/manfredi/python/ch-2.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +numbers = [ 2, 4, 3 ] + +print("numbers", numbers) + +n = sorted(numbers) + +pair = [ (n[i], n[j]) for i in range(len(n)) + for j in range(i, len(n)) if n[i] is not n[j] ] + +bit_sum = 0 + +for i, j in pair: + bin_i = "{0:032b}".format(i) + bin_j = "{0:032b}".format(j) + diff = 0 + for k in range(len(bin_i)): + if bin_i[k] is not bin_j[k]: diff += 1 + + bit_sum += diff + print("{1} = {0}\n{3} = {2}\nf({0}, {2}) = {4}\n".format(i, bin_i, j, bin_j, diff)) + +print("bit sum = {}".format(bit_sum)); -- cgit From 42497f56acb5263f25c9baf5d3e521242333d6e7 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 8 May 2020 21:30:56 +0100 Subject: - Added solutions by Arne Sommer. --- challenge-059/arne-sommer/blog.txt | 1 + challenge-059/arne-sommer/raku/bit-diff | 23 ++++++++++ challenge-059/arne-sommer/raku/bit-diff2 | 18 ++++++++ challenge-059/arne-sommer/raku/bit-sum | 20 ++++++++ challenge-059/arne-sommer/raku/bit-sum-musing | 46 +++++++++++++++++++ challenge-059/arne-sommer/raku/bit-sum-musing2 | 56 +++++++++++++++++++++++ challenge-059/arne-sommer/raku/ch-1.p6 | 31 +++++++++++++ challenge-059/arne-sommer/raku/ch-2.p6 | 20 ++++++++ challenge-059/arne-sommer/raku/linked-list | 31 +++++++++++++ challenge-059/arne-sommer/raku/linked-list-linked | 48 +++++++++++++++++++ 10 files changed, 294 insertions(+) create mode 100644 challenge-059/arne-sommer/blog.txt create mode 100755 challenge-059/arne-sommer/raku/bit-diff create mode 100755 challenge-059/arne-sommer/raku/bit-diff2 create mode 100755 challenge-059/arne-sommer/raku/bit-sum create mode 100755 challenge-059/arne-sommer/raku/bit-sum-musing create mode 100755 challenge-059/arne-sommer/raku/bit-sum-musing2 create mode 100755 challenge-059/arne-sommer/raku/ch-1.p6 create mode 100755 challenge-059/arne-sommer/raku/ch-2.p6 create mode 100755 challenge-059/arne-sommer/raku/linked-list create mode 100755 challenge-059/arne-sommer/raku/linked-list-linked (limited to 'challenge-059') diff --git a/challenge-059/arne-sommer/blog.txt b/challenge-059/arne-sommer/blog.txt new file mode 100644 index 0000000000..3217ab84fe --- /dev/null +++ b/challenge-059/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/linked-sum.html diff --git a/challenge-059/arne-sommer/raku/bit-diff b/challenge-059/arne-sommer/raku/bit-diff new file mode 100755 index 0000000000..fc27e3b060 --- /dev/null +++ b/challenge-059/arne-sommer/raku/bit-diff @@ -0,0 +1,23 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $a is copy, $b is copy, :$verbose); + +($a, $b) = ($b, $a) if $b > $a; + +my $a2 = $a.base(2); + +my $length = $a2.chars; + +my $b2 = $b.fmt('%0' ~ $length ~ 'b'); + +my $c2 = ($a +^ $b).fmt('%0' ~ $length ~ 'b'); + +if $verbose +{ + say ": $a2 ($a)"; + say ": $b2 ($b)"; + say ": $c2 -> ", $c2.comb.sum; +} + +say $c2.comb.sum; + diff --git a/challenge-059/arne-sommer/raku/bit-diff2 b/challenge-059/arne-sommer/raku/bit-diff2 new file mode 100755 index 0000000000..0cb783fa56 --- /dev/null +++ b/challenge-059/arne-sommer/raku/bit-diff2 @@ -0,0 +1,18 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $a, $b, :$verbose); + +my $c = ($a +^ $b); + +my $sum = $c.comb.sum; + +if $verbose +{ + my $length = (max($a, $b)).base(2).chars; + + say ": { $a.fmt('%0' ~ $length ~ 'b') } ($a)"; + say ": { $b.fmt('%0' ~ $length ~ 'b') } ($b)"; + say ": { $c.fmt('%0' ~ $length ~ 'b') } -> $sum"; +} + +say $sum; diff --git a/challenge-059/arne-sommer/raku/bit-sum b/challenge-059/arne-sommer/raku/bit-sum new file mode 100755 index 0000000000..89962b4271 --- /dev/null +++ b/challenge-059/arne-sommer/raku/bit-sum @@ -0,0 +1,20 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int, + :$verbose); + +my $grand-total; + +for @numbers.combinations(2) -> $list +{ + my $sum = bit-diff(|$list); + say ": $list -> $sum" if $verbose; + $grand-total += $sum; +} + +say $grand-total; + +sub bit-diff (Int $a, Int $b) +{ + return ($a +^ $b).base(2).comb.sum; +} diff --git a/challenge-059/arne-sommer/raku/bit-sum-musing b/challenge-059/arne-sommer/raku/bit-sum-musing new file mode 100755 index 0000000000..e4d1166e93 --- /dev/null +++ b/challenge-059/arne-sommer/raku/bit-sum-musing @@ -0,0 +1,46 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $limit where $limit > 1 = 100, :$verbose); + +my $prev-sum = 0; +my $prev-inc = 0; +my $prev-inx = 0; + +my @result; + +for 2 .. $limit -> $number +{ + my @list = 1 .. $number; + + my $sum = bit-sum(@list); + my $inc = $sum - $prev-sum; + my $inx = $inc - $prev-inc; + + say ": bit-sum 1..{ $number.fmt("%3d") } -> { $sum.fmt("%3d") } -> { $inc.fmt("%3d") } -> { $inx.fmt("%3d") }" if $verbose; + + @result.push: $inx; + + $prev-sum = $sum; + $prev-inc = $inc; + $prev-inx = $inx; +} + +put @result; + +sub bit-sum (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int) +{ + my $grand-total; + + for @numbers.combinations(2) -> $list + { + my $sum = bit-diff(|$list); + $grand-total += $sum; + } + + return $grand-total; + + sub bit-diff (Int $a, Int $b) + { + return ($a +^ $b).base(2).comb.sum; + } +} diff --git a/challenge-059/arne-sommer/raku/bit-sum-musing2 b/challenge-059/arne-sommer/raku/bit-sum-musing2 new file mode 100755 index 0000000000..9562349691 --- /dev/null +++ b/challenge-059/arne-sommer/raku/bit-sum-musing2 @@ -0,0 +1,56 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $limit where $limit > 1 = 100, :$verbose, :$type = "int"); + +my $prev-sum = 0; +my $prev-inc = 0; +my $prev-inx = 0; + +my @result; + +for 2 .. $limit -> $number +{ + my @list; + + given $type + { + when "int" { @list = (1 .. Inf)[^$number] } + when "even" { @list = (2, 4 ... Inf)[^$number] } + when "odd" { @list = (1, 3 ... Inf)[^$number] } + when "prime" { @list = ((1 .. Inf).grep: *.is-prime)[^$number] } + when "fib" { @list = (1, 1, * + * ... Inf)[^$number] } + default { die "Unknown type $_" } + } + + my $sum = bit-sum(@list); + my $inc = $sum - $prev-sum; + my $inx = $inc - $prev-inc; + + say ": bit-sum @list[] -> { $sum.fmt("%3d") } -> { $inc.fmt("%3d") } -> { $inx.fmt("%3d") }" if $verbose; + + @result.push: $inx; + + $prev-sum = $sum; + $prev-inc = $inc; + $prev-inx = $inx; +} + +put @result; + +sub bit-sum (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int) +{ + my $grand-total; + + for @numbers.combinations(2) -> $list + { + my $sum = bit-diff(|$list); + $grand-total += $sum; + } + + return $grand-total; + + sub bit-diff (Int $a, Int $b) + { + return ($a +^ $b).base(2).comb.sum; + } +} diff --git a/challenge-059/arne-sommer/raku/ch-1.p6 b/challenge-059/arne-sommer/raku/ch-1.p6 new file mode 100755 index 0000000000..27b549dfd5 --- /dev/null +++ b/challenge-059/arne-sommer/raku/ch-1.p6 @@ -0,0 +1,31 @@ +#! /usr/bin/env raku + +multi MAIN (Int $k = 3, Str $list = "1 4 3 2 5 2", :$verbose) +{ + MAIN($k, $list.words, :$verbose); +} + +multi MAIN (Int $k = 3, *@list, :$verbose) +{ + my @lower; + my @higher; + + for @list -> $elem + { + $elem >= $k + ?? @higher.push: $elem + !! @lower.push: $elem; + } + + my @result = (@lower, @higher).flat; + + if $verbose + { + say ": == : $k"; + say ": < : @lower[]"; + say ": >= : @higher[]"; + } + + say @result.join(" → "); +} + \ No newline at end of file diff --git a/challenge-059/arne-sommer/raku/ch-2.p6 b/challenge-059/arne-sommer/raku/ch-2.p6 new file mode 100755 index 0000000000..89962b4271 --- /dev/null +++ b/challenge-059/arne-sommer/raku/ch-2.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int, + :$verbose); + +my $grand-total; + +for @numbers.combinations(2) -> $list +{ + my $sum = bit-diff(|$list); + say ": $list -> $sum" if $verbose; + $grand-total += $sum; +} + +say $grand-total; + +sub bit-diff (Int $a, Int $b) +{ + return ($a +^ $b).base(2).comb.sum; +} diff --git a/challenge-059/arne-sommer/raku/linked-list b/challenge-059/arne-sommer/raku/linked-list new file mode 100755 index 0000000000..27b549dfd5 --- /dev/null +++ b/challenge-059/arne-sommer/raku/linked-list @@ -0,0 +1,31 @@ +#! /usr/bin/env raku + +multi MAIN (Int $k = 3, Str $list = "1 4 3 2 5 2", :$verbose) +{ + MAIN($k, $list.words, :$verbose); +} + +multi MAIN (Int $k = 3, *@list, :$verbose) +{ + my @lower; + my @higher; + + for @list -> $elem + { + $elem >= $k + ?? @higher.push: $elem + !! @lower.push: $elem; + } + + my @result = (@lower, @higher).flat; + + if $verbose + { + say ": == : $k"; + say ": < : @lower[]"; + say ": >= : @higher[]"; + } + + say @result.join(" → "); +} + \ No newline at end of file diff --git a/challenge-059/arne-sommer/raku/linked-list-linked b/challenge-059/arne-sommer/raku/linked-list-linked new file mode 100755 index 0000000000..00796d8676 --- /dev/null +++ b/challenge-059/arne-sommer/raku/linked-list-linked @@ -0,0 +1,48 @@ +#! /usr/bin/env raku + +multi MAIN (Int $k = 3, Str $list = "1 4 3 2 5 2", :$verbose) +{ + MAIN($k, $list.words, :$verbose); +} + +multi MAIN (Int $k = 3, *@list, :$verbose) +{ + my @lower; + my @higher; + + for @list -> $elem + { + $elem >= $k + ?? @higher.push: $elem + !! @lower.push: $elem; + } + + my @result = (@lower, @higher).flat; + + if $verbose + { + say ": == : $k"; + say ": < : @lower[]"; + say ": >= : @higher[]"; + } + + say @result.join(" → ") if $verbose; + + class ListElem + { + has $.value; + has $.next is rw; + + method display + { + print $.value; + if $.next { print " → "; $.next.display; } else { say ""; } + } + } + + my $head; + + $head = ListElem.new(value => $_, next => $head) for @result.reverse; + + $head.display; +} -- cgit From 08ab8860fea3a1df2bc3fee73e45564f5cd810d9 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Sat, 9 May 2020 08:24:56 +0800 Subject: Create BLOG.txt --- challenge-059/cheok-yin-fung/BLOG.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-059/cheok-yin-fung/BLOG.txt (limited to 'challenge-059') diff --git a/challenge-059/cheok-yin-fung/BLOG.txt b/challenge-059/cheok-yin-fung/BLOG.txt new file mode 100644 index 0000000000..0db37b7d91 --- /dev/null +++ b/challenge-059/cheok-yin-fung/BLOG.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/c_y_fung/2020/05/cys-take-on-perl-weekly-challenge-059.html -- cgit From a2292a9824c3545abc160c1b909dec60102fb251 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 9 May 2020 03:26:41 +0100 Subject: - Added Perl and Raku solutions to Linked List task. --- challenge-059/mohammad-anwar/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-059/mohammad-anwar/perl/ch-1a.pl | 25 +++++++++++++++++++++++++ challenge-059/mohammad-anwar/raku/ch-1.p6 | 26 ++++++++++++++++++++++++++ challenge-059/mohammad-anwar/raku/ch-1a.p6 | 23 +++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 challenge-059/mohammad-anwar/perl/ch-1.pl create mode 100644 challenge-059/mohammad-anwar/perl/ch-1a.pl create mode 100644 challenge-059/mohammad-anwar/raku/ch-1.p6 create mode 100644 challenge-059/mohammad-anwar/raku/ch-1a.p6 (limited to 'challenge-059') diff --git a/challenge-059/mohammad-anwar/perl/ch-1.pl b/challenge-059/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..a3cfeed4c4 --- /dev/null +++ b/challenge-059/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $L = [ 1, 4, 3, 2, 5, 2 ]; +my $K = 3; +my $O = split_list($L, $K); + +print sprintf("Input: [ %s ]\n", join (" -> ", @$L)); +print sprintf("Output: [ %s ]\n", join (" -> ", @$O)); + +sub split_list { + my ($L, $K) = @_; + + my $before = []; + my $after = []; + foreach my $i (@$L) { + if ($i < $K) { + push @$before, $i; + } + else { + push @$after, $i; + } + } + + return [ @$before, @$after ]; +} diff --git a/challenge-059/mohammad-anwar/perl/ch-1a.pl b/challenge-059/mohammad-anwar/perl/ch-1a.pl new file mode 100644 index 0000000000..7be98d1233 --- /dev/null +++ b/challenge-059/mohammad-anwar/perl/ch-1a.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use Test::More; +use Test::Deep; + +is_deeply( split_list([ 1, 4, 3, 2, 5, 2 ], 3), [ 1, 2, 2, 4, 3, 5 ]); + +done_testing; + +sub split_list { + my ($L, $K) = @_; + + my $before = []; + my $after = []; + foreach my $i (@$L) { + if ($i < $K) { + push @$before, $i; + } + else { + push @$after, $i; + } + } + + return [ @$before, @$after ]; +} diff --git a/challenge-059/mohammad-anwar/raku/ch-1.p6 b/challenge-059/mohammad-anwar/raku/ch-1.p6 new file mode 100644 index 0000000000..77b6ade26e --- /dev/null +++ b/challenge-059/mohammad-anwar/raku/ch-1.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env perl6 + +use v6.d; + +sub MAIN($L = [ 1, 4, 3, 2, 5, 2 ], $K = 3) { + + my $O = split-list($L, $K); + say sprintf("Input: [ %s ]", $L.join(" -> ")); + say sprintf("Output: [ %s ]", $O.join(" -> ")); +} + +sub split-list($L, $K) { + + my $before = []; + my $after = []; + for |$L -> $i { + if $i < $K { + $before.push: $i; + } + else { + $after.push: $i; + } + } + + return [ |$before, |$after ]; +} diff --git a/challenge-059/mohammad-anwar/raku/ch-1a.p6 b/challenge-059/mohammad-anwar/raku/ch-1a.p6 new file mode 100644 index 0000000000..ae19e9e834 --- /dev/null +++ b/challenge-059/mohammad-anwar/raku/ch-1a.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env perl6 + +use Test; + +is-deeply split-list([ 1, 4, 3, 2, 5, 2 ], 3), [ 1, 2, 2, 4, 3, 5 ]; + +done-testing; + +sub split-list($L, $K) { + + my $before = []; + my $after = []; + for |$L -> $i { + if $i < $K { + $before.push: $i; + } + else { + $after.push: $i; + } + } + + return [ |$before, |$after ]; +} -- cgit From de0ca0b00cad07ea6ec6a30d3b12207efd1e55e7 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 9 May 2020 09:04:03 +0100 Subject: - Added solutions by Colin Crain. --- challenge-059/colin-crain/blog.txt | 1 + challenge-059/colin-crain/perl/ch-1.pl | 88 ++++++++++++++++++++++++++++++++++ challenge-059/colin-crain/perl/ch-2.pl | 35 ++++++++++++++ challenge-059/colin-crain/raku/ch-1.p6 | 65 +++++++++++++++++++++++++ challenge-059/colin-crain/raku/ch-2.p6 | 11 +++++ 5 files changed, 200 insertions(+) create mode 100644 challenge-059/colin-crain/blog.txt create mode 100644 challenge-059/colin-crain/perl/ch-1.pl create mode 100644 challenge-059/colin-crain/perl/ch-2.pl create mode 100644 challenge-059/colin-crain/raku/ch-1.p6 create mode 100644 challenge-059/colin-crain/raku/ch-2.p6 (limited to 'challenge-059') diff --git a/challenge-059/colin-crain/blog.txt b/challenge-059/colin-crain/blog.txt new file mode 100644 index 0000000000..196a8bde29 --- /dev/null +++ b/challenge-059/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.wordpress.com/2020/05/08/sorting-a-linked-list-and-summing-binary-bits/ diff --git a/challenge-059/colin-crain/perl/ch-1.pl b/challenge-059/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..ee71fb31ae --- /dev/null +++ b/challenge-059/colin-crain/perl/ch-1.pl @@ -0,0 +1,88 @@ +use warnings; +use strict; +use feature ":5.26"; + +## ## ## ## ## MAIN: + +## acquire the locus value and the array representation +## of the linked list +my ($locus, @input) = @ARGV; +my $next = undef; +my $node; + +## 1. convert the input commandline array into a linked list +while (scalar @input > 0) { + my $value = pop @input; + $node = new Node($value, $next); + $next = $node +} +## $node currently points to beginning of the list + +my $prelist_first; +my $prelist_last; +my $postlist_first; +my $postlist_last; + +while (defined $node) { + ## 2a. if it is less than the given value, add it to + ## the end of the pre list + if ($node->value < $locus) { + defined $prelist_last ? $prelist_last->next($node) + : ($prelist_first = $node); + $prelist_last = $node; + } + ## 2b. if it is more than or equal to the given value + ## add it to the end of the post list + else { + defined $postlist_last ? $postlist_last->next($node) + : ($postlist_first = $node); + $postlist_last = $node; + } + + $node = $node->next; +} + +## 3. link the pre list to the post list: + +## 3a. point the last element of the pre list to +## the first element of the post +$prelist_last->next($postlist_first) if (defined $prelist_last); + +## 3b. point the last element of the post list to null +$postlist_last->{'next'} = undef; + +## ## ## output + +## if prelist never got made, start with the postlist +$node = $prelist_first || $postlist_first; +my @output; +while (defined $node) { + push @output, $node->value; + $node = $node->next; +} +say join ' → ', @output; + + +## ## ## ## ## NODE PACKAGE + +package Node; + +sub new { + my ($class, $value, $next) = @_; + my $self = { "value" => $value, + "next" => $next }; + bless $self, $class; + return $self; +} + +sub value { + my ($self, $value ) = @_; + $self->{value} = $value if defined $value; + return $self->{value} +} + +sub next { + my ($self, $next ) = @_; + $self->{next} = $next if defined $next; + return $self->{next} +} diff --git a/challenge-059/colin-crain/perl/ch-2.pl b/challenge-059/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..7f9768a6f2 --- /dev/null +++ b/challenge-059/colin-crain/perl/ch-2.pl @@ -0,0 +1,35 @@ +use warnings; +use strict; +use feature ":5.26"; + +use List::Util qw(sum); + +## ## ## ## ## MAIN: + +my @array = @ARGV; +my @sets = choose_2_sets( @array ); + +my $sum; +for my $set ( @sets ) { + $sum += bit_difference($set->[0], $set->[1]); +} + +say $sum; + + +## ## ## ## ## SUBS: + +sub bit_difference { + return sum( split //, sprintf "%b", 0+$_[0] ^ 0+$_[1] ); +} + +sub choose_2_sets { + my @array = @_; + my @out; + for my $i (0..(scalar @array - 1)) { + for my $j ($i+1..(scalar @array - 1)) { + push @out, [ $array[$i], $array[$j] ]; + } + } + return @out; +} diff --git a/challenge-059/colin-crain/raku/ch-1.p6 b/challenge-059/colin-crain/raku/ch-1.p6 new file mode 100644 index 0000000000..b6c5975aee --- /dev/null +++ b/challenge-059/colin-crain/raku/ch-1.p6 @@ -0,0 +1,65 @@ +class Node { + has Int $.value is rw; + has Node $.next is rw; +} + +class LinkedList { + has Node $.first is rw; + has Node $.last is rw; + + method populate_from_array ( @array ) { + my $node; + my $next; + while @array.elems > 0 { + $node = Node.new(value => @array.pop.Int); + $node.next = $next if $next.defined; + $next = $node; + } + $.first = $node; + } + + method arrow_print () { + my @output; + my $node = $.first; + while (defined $node) { + push @output, $node.value; + $node = $node.next; + } + @output.join(' → ').say; + } +} + + +sub MAIN (Int:D $locus, *@input) { + + ## 1. convert the input commandline array into a linked list + my $list = LinkedList.new(); + $list.populate_from_array( @input ); + + my $before = LinkedList.new(); + my $after = LinkedList.new(); + my $node = $list.first; + + ## 2a. if it is less than the given value, add it to + ## the end of the before list + ## 2b. if it is more than or equal to the given value + ## add it to the end of the after list + ## if a sublist isn't started, start it with the node + while $node.defined { + my $sublist = $node.value < $locus ?? $before !! $after; + $sublist.last.defined ?? $sublist.last.next + !! $sublist.first = $node; + $sublist.last = $node; + $node = $node.next; + } + + ## 3. link the pre list to the post list: + ## 3a. point the last element of the pre list to + ## the first element of the post + ## 3b. point the last element of the post list to null + $before.last.next = $after.first if defined $before.last; + $after.last.next = Nil if defined $after.la