From fed310b12f1ff9a0b03090cb8ae922e940221d68 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 10 May 2020 11:28:00 -0400 Subject: solution for challenge 059 --- challenge-059/adam-russell/blog.txt | 1 + challenge-059/adam-russell/perl/LinkedList.pm | 75 +++++++++++++++++++++++++++ challenge-059/adam-russell/perl/ch-1.pl | 22 ++++++++ challenge-059/adam-russell/perl/ch-2.pl | 38 ++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 challenge-059/adam-russell/blog.txt create mode 100644 challenge-059/adam-russell/perl/LinkedList.pm create mode 100644 challenge-059/adam-russell/perl/ch-1.pl create mode 100644 challenge-059/adam-russell/perl/ch-2.pl (limited to 'challenge-059') diff --git a/challenge-059/adam-russell/blog.txt b/challenge-059/adam-russell/blog.txt new file mode 100644 index 0000000000..4f5e408d78 --- /dev/null +++ b/challenge-059/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/16202.html diff --git a/challenge-059/adam-russell/perl/LinkedList.pm b/challenge-059/adam-russell/perl/LinkedList.pm new file mode 100644 index 0000000000..a340f898e9 --- /dev/null +++ b/challenge-059/adam-russell/perl/LinkedList.pm @@ -0,0 +1,75 @@ +use strict; +use warnings; +package LinkedList{ + use boolean; + use Tie::RefHash; + use Class::Struct; + package Node{ + use Class::Struct; + + struct( + data => q/$/, + next => q/Node/ + ); + } + + struct( + head => q/Node/ + ); + + sub stringify{ + my($self) = @_; + my $s = ""; + my $next = $self->head()->next(); + while($next && $next->next()){ + $s .= " -> " if $s; + $s = $s . $next->data(); + $next = $next->next(); + } + $s = $s . " -> " . $next->data() if $next->data(); + $s .= "\n"; + return $s; + } + + sub insert{ + my($self, $data, $previous) = @_; + if(!$previous){ + $previous=new Node(data => undef, next => undef); + $self->head($previous); + } + my $next=new Node(data => $data, next => undef); + $previous->next($next); + return $next; + } + + sub partition{ + my($self, $k) = @_; + my $previous = $self->head(); + my $next = $self->head()->next(); + tie my %node_value, "Tie::RefHash"; + while($next){ + if($next->data() < $k){ + $node_value{$next} = $next->data(); + if($next->next()){ + $previous->next($next->next()); + } + else{ + $previous->next(new Node()); + $next = undef; + next; + } + } + $previous = $next; + $next = $next->next(); + } + my @sorted_nodes = sort {$node_value{$b} <=> $node_value{$a}} keys %node_value; + $previous = $self->head(); + my $old_first = $previous->next(); + while(@sorted_nodes){ + my $node = pop @sorted_nodes; + $previous = insert($self,$node->data(), $previous); + } + $previous->next($old_first); + } + true; +} diff --git a/challenge-059/adam-russell/perl/ch-1.pl b/challenge-059/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..960302b6a3 --- /dev/null +++ b/challenge-059/adam-russell/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +## +# Write a script to partition a linked list such that +# all nodes less than k come before nodes greater than or equal to k. +## +use LinkedList; + +MAIN:{ + my $ll = new LinkedList(); + my $next = $ll->insert(1, undef); + $next = $ll->insert(4, $next); + $next = $ll->insert(3, $next); + $next = $ll->insert(2, $next); + $next = $ll->insert(5, $next); + $next = $ll->insert(2, $next); + print "Original: "; + print $ll->stringify(); + $ll->partition(3); + print "Partitioned: "; + print $ll->stringify(); +} diff --git a/challenge-059/adam-russell/perl/ch-2.pl b/challenge-059/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..ea09ad3c25 --- /dev/null +++ b/challenge-059/adam-russell/perl/ch-2.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; +## +# Write a function which returns the count of +# different bits of the binary representation of +# positive numbers a and b. The script should +# accept n positive numbers and sum the result +# for every pair of numbers given. +## +sub count_different_bits{ + my($a, $b) = @_; + my $bits = 0; + while($a || $b){ + $bits++ if ($a & 1) && !($b & 1); + $bits++ if !($a & 1) && ($b & 1); + $a = $a >> 1; + $b = $b >> 1; + } + return $bits; +} + +MAIN:{ + my $sum = 0; + my $line; + while($line = ){ + chomp($line); + my($a, $b) = split(/,/, $line); + my $different_bits = count_different_bits($a, $b); + print "($a, $b) = $different_bits\n"; + $sum += $different_bits; + } + print "sum of all bit differences: $sum\n"; +} + +__DATA__ +2,3 +2,4 +3,4 -- cgit