aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-05-10 17:39:31 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-05-10 17:39:31 +0100
commit11a61e8ae7e5220d67dbcbf24d4b508789d463d4 (patch)
treea9955c881eb8afd41ff2a82ae622d19bc19bf9b1 /challenge-059
parenteb1db8b394ec3f1e49b744d7f3d669e84961d78d (diff)
parent90274063a58c3387170ae98b4a49f4c6feadb4c3 (diff)
downloadperlweeklychallenge-club-11a61e8ae7e5220d67dbcbf24d4b508789d463d4.tar.gz
perlweeklychallenge-club-11a61e8ae7e5220d67dbcbf24d4b508789d463d4.tar.bz2
perlweeklychallenge-club-11a61e8ae7e5220d67dbcbf24d4b508789d463d4.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/adam-russell/blog.txt1
-rw-r--r--challenge-059/adam-russell/perl/LinkedList.pm75
-rw-r--r--challenge-059/adam-russell/perl/ch-1.pl22
-rw-r--r--challenge-059/adam-russell/perl/ch-2.pl38
4 files changed, 136 insertions, 0 deletions
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 = <DATA>){
+ 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