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 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 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