aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-10 08:04:15 +0100
committerGitHub <noreply@github.com>2020-05-10 08:04:15 +0100
commit176c004dd0b040eadbe91e8e257bd089333eb123 (patch)
tree0211eb6352e18a295a4a4dfa3ff00ddc6975a721 /challenge-059
parentdda476fb7a7f28baf26abcf255a9aeb61e1e0769 (diff)
parent9bf01355ebb5004e741147b3917ab271aaf7a9bf (diff)
downloadperlweeklychallenge-club-176c004dd0b040eadbe91e8e257bd089333eb123.tar.gz
perlweeklychallenge-club-176c004dd0b040eadbe91e8e257bd089333eb123.tar.bz2
perlweeklychallenge-club-176c004dd0b040eadbe91e8e257bd089333eb123.zip
Merge pull request #1692 from sangeetkar/master
Solution updated.
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/sangeet-kar/perl/Listy.pm47
-rw-r--r--challenge-059/sangeet-kar/perl/ch-1.pl74
-rw-r--r--challenge-059/sangeet-kar/perl/ch-2.pl2
3 files changed, 57 insertions, 66 deletions
diff --git a/challenge-059/sangeet-kar/perl/Listy.pm b/challenge-059/sangeet-kar/perl/Listy.pm
deleted file mode 100644
index eeb08c4d61..0000000000
--- a/challenge-059/sangeet-kar/perl/Listy.pm
+++ /dev/null
@@ -1,47 +0,0 @@
-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
index 31586ff9ec..2083625860 100644
--- a/challenge-059/sangeet-kar/perl/ch-1.pl
+++ b/challenge-059/sangeet-kar/perl/ch-1.pl
@@ -1,47 +1,85 @@
use strict;
use warnings;
-use File::Basename;
-use lib dirname (__FILE__);
use experimental qw(signatures);
-use Listy;
+#List node class
+package Node;
-sub find_parent ($lst, $node, $start_node=undef) {
- $start_node //= $lst->{head};
+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};
+ }
+ }
+}
+
+sub parent_of_node ($self, $node, $start_node=undef) {
+ $start_node //= $self->{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;
+sub shift_right_rotate ($self, $i, $j) {
+ my $parent_i = $self->parent_of_node($i);
+ my $parent_j = $self->parent_of_node($j, $i);
$parent_j->{next_node} = $j->{next_node};
- $lst->{last1} = $parent_j if $j == $lst->{last1};
+ $self->{last1} = $parent_j if $j == $self->{last1};
$j->{next_node} = $i;
if ($parent_i == $i) {
- $lst->{head} = $j;
+ $self->{head} = $j;
}
else {
$parent_i->{next_node} = $j;
}
}
-sub partition_list {
- my ($lst, $k) = @_;
- my $i = $lst->{head};
+sub partition ($self, $k) {
+ my $i = $self->{head};
$i = $i->{next_node} if defined $i && $i->{val} < $k;
- return $lst unless defined $i;
+ return 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);
+ return unless defined $j;
+ $self->shift_right_rotate($i, $j);
}
}
-my $lst = Listy->new ([1, 4, 3, 2, 5, 2]);
-partition_list ($lst, 3) -> print_list;
+my $list = Listy->new ([1, 4, 3, 2, 5, 2]);
+$list->partition(3);
+$list->print_list; \ No newline at end of file
diff --git a/challenge-059/sangeet-kar/perl/ch-2.pl b/challenge-059/sangeet-kar/perl/ch-2.pl
index 44af4aa8aa..9c91581d41 100644
--- a/challenge-059/sangeet-kar/perl/ch-2.pl
+++ b/challenge-059/sangeet-kar/perl/ch-2.pl
@@ -6,7 +6,7 @@ use Algorithm::Combinatorics qw(combinations);
sub helper {
my ( $a, $b ) = @_;
- sum( split //, sprintf( "%b", $a ) ^ sprintf( "%b", $b ) );
+ sum( split //, sprintf ("%b", $a ^ $b ));
}
sub bitsum {