aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorSangeet Kar <ssauravk@areas.com>2020-05-09 16:33:08 +0200
committerSangeet Kar <ssauravk@areas.com>2020-05-09 16:33:08 +0200
commit30488068c99b7dfffd98eb9f57419a8a73d6ac15 (patch)
treefcbcf803b64346112c48a91749f7b2d480f0b616 /challenge-059
parent2323c1492e963ad594656dbddbe0547926157e0b (diff)
downloadperlweeklychallenge-club-30488068c99b7dfffd98eb9f57419a8a73d6ac15.tar.gz
perlweeklychallenge-club-30488068c99b7dfffd98eb9f57419a8a73d6ac15.tar.bz2
perlweeklychallenge-club-30488068c99b7dfffd98eb9f57419a8a73d6ac15.zip
merging Listy module and ch-1.pl
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/sangeet-kar/perl/ch-1.pl76
1 files changed, 58 insertions, 18 deletions
diff --git a/challenge-059/sangeet-kar/perl/ch-1.pl b/challenge-059/sangeet-kar/perl/ch-1.pl
index 31586ff9ec..f6154d9d92 100644
--- a/challenge-059/sangeet-kar/perl/ch-1.pl
+++ b/challenge-059/sangeet-kar/perl/ch-1.pl
@@ -1,47 +1,87 @@
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 rotate_right_to_left ($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->rotate_right_to_left($i, $j);
}
}
-my $lst = Listy->new ([1, 4, 3, 2, 5, 2]);
-partition_list ($lst, 3) -> print_list;
+__END__
+
+my $list = Listy->new ([1, 4, 3, 2, 5, 2]);
+$list->partition(3);
+$list->print_list; \ No newline at end of file