forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request manwar#1686 from E7-87-83/master
Cheok Yin's Submission for PWC#059
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
http://blogs.perl.org/users/c_y_fung/2020/05/cys-take-on-perl-weekly-challenge-059.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
|
||
|