diff options
Diffstat (limited to 'challenge-059')
| -rw-r--r-- | challenge-059/yet-ebreo/perl/ch-1.pl | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/challenge-059/yet-ebreo/perl/ch-1.pl b/challenge-059/yet-ebreo/perl/ch-1.pl new file mode 100644 index 0000000000..37a061d69f --- /dev/null +++ b/challenge-059/yet-ebreo/perl/ch-1.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl +use strict; +use warnings; +use feature 'say'; + +my @arr = ( 1, 4, 3, 2, 5, 2 ); +my $k = 3; +my $link = new LinkedList; + +#Add items in linked list +for my $i (@arr) { + $link->add($i); +} + +#Declare two linked list for partitions +my $uplink = new LinkedList; +my $dnlink = new LinkedList; + +#Print original list +say "Original List:"; +while ($link->iter) { + my $i = $link->value; + if ($i >= $k) { + $uplink->add($i) + } else { + $dnlink->add($i); + } + print $i; + print " -> " if $link->next +} + +#Join dnlink and uplink +$dnlink->join($uplink); + + +#Print final re-ordered list +say "\n\nPartitioned List:"; +while ($dnlink->iter) { + my $i = $dnlink->value; + print $i; + print " -> " if $dnlink->next +} + + +package LinkedList; +my $iter_item; +sub new { + my $class = shift; + my $self = { + value => shift, + next => undef, + + }; + + return bless $self, $class; +} +sub add { + my ($self,$val) = @_; + my $curr = $self->last; + + $curr->{value} = $val; + $curr->{next} = undef; +} +sub last { + my $self = shift; + my $curr = $self; + while (defined $curr->{value}) { + $curr = \%{$curr->{next}}; + } + return $curr; +} +sub value { + my $self = shift; + $iter_item = $self if (!$iter_item); + return $iter_item->{value}; +} +sub next { + return $iter_item->{next}; +} +sub iter { + my ($self, $join_mode) = @_; + + if (!$iter_item) { + $iter_item = $self; + } else { + if ($iter_item->{next}) { + $iter_item = \%{$iter_item->{next}}; + } else { + $iter_item = undef; + } + } +} +sub join { + my ($self,$val) = @_; + while ($val->iter) { + my $i = $val->value; + $self->add($i) + } +} +1;
\ No newline at end of file |
