aboutsummaryrefslogtreecommitdiff
path: root/challenge-018
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-07-28 23:41:07 +0100
committerGitHub <noreply@github.com>2019-07-28 23:41:07 +0100
commitabdd94876eabf211deaf2db36992764f52800fa1 (patch)
tree9843b54b62be1e1eb2ab7009d0c19eefac1e3647 /challenge-018
parent9796f06e5a38574aae00ce2243b96b816bf53747 (diff)
parent79394bac206b6479105c33cf72a42499e39b831f (diff)
downloadperlweeklychallenge-club-abdd94876eabf211deaf2db36992764f52800fa1.tar.gz
perlweeklychallenge-club-abdd94876eabf211deaf2db36992764f52800fa1.tar.bz2
perlweeklychallenge-club-abdd94876eabf211deaf2db36992764f52800fa1.zip
Merge pull request #441 from yzhernand/ch-018-yozen
Added solutions by Yozen Hernandez for challenge 2 for week 18
Diffstat (limited to 'challenge-018')
-rw-r--r--challenge-018/yozen-hernandez/blog.txt1
-rw-r--r--challenge-018/yozen-hernandez/blog1.txt1
-rwxr-xr-xchallenge-018/yozen-hernandez/perl5/ch-2.pl107
3 files changed, 109 insertions, 0 deletions
diff --git a/challenge-018/yozen-hernandez/blog.txt b/challenge-018/yozen-hernandez/blog.txt
new file mode 100644
index 0000000000..92c50c9736
--- /dev/null
+++ b/challenge-018/yozen-hernandez/blog.txt
@@ -0,0 +1 @@
+https://yzhernand.github.io/posts/perl-weekly-challenge-18-1/
diff --git a/challenge-018/yozen-hernandez/blog1.txt b/challenge-018/yozen-hernandez/blog1.txt
new file mode 100644
index 0000000000..0119f09de2
--- /dev/null
+++ b/challenge-018/yozen-hernandez/blog1.txt
@@ -0,0 +1 @@
+https://yzhernand.github.io/posts/perl-weekly-challenge-18-2/ \ No newline at end of file
diff --git a/challenge-018/yozen-hernandez/perl5/ch-2.pl b/challenge-018/yozen-hernandez/perl5/ch-2.pl
new file mode 100755
index 0000000000..bd2ac95bd5
--- /dev/null
+++ b/challenge-018/yozen-hernandez/perl5/ch-2.pl
@@ -0,0 +1,107 @@
+#!/usr/bin/env perl
+
+=for comment
+
+Write a script to implement Priority Queue. It is like regular queue except each
+element has a priority associated with it. In a priority queue, an element with
+high priority is served before an element with low priority. Please check this
+wiki page for more informations. It should serve the following operations:
+
+1) is_empty: check whether the queue has no elements.
+
+2) insert_with_priority: add an element to the queue with an associated
+priority.
+
+3) pull_highest_priority_element: remove the element from the queue that has the
+highest priority, and return it. If two elements have the same priority, then
+return element added first.
+
+=cut
+
+package Queue::Priority;
+use v5.24;
+use strict;
+use warnings;
+use feature qw(signatures);
+no warnings "experimental::signatures";
+use Scalar::Util qw(looks_like_number);
+use List::Util qw(max);
+use Carp;
+
+sub new {
+ my $class = shift;
+ $class = ref $class if ref $class;
+ my $self = bless { queue => {} }, $class;
+ $self;
+}
+
+sub is_empty {
+ return !( shift->{queue}->%* );
+}
+
+sub insert_with_priority ( $self, $elem, $p = -1 ) {
+ croak "Error: second argument (priority) must be a number. Given: $p"
+ unless looks_like_number($p);
+
+ push $self->{queue}->{$p}->@*, $elem;
+}
+
+sub pull_highest_priority_element {
+ my $self = shift;
+ my $high = max keys $self->{queue}->%*;
+ return undef unless $high;
+
+ my $pulled = shift $self->{queue}->{$high}->@*;
+ delete $self->{queue}->{$high} if $self->{queue}->{$high}->@* == 0;
+ return $pulled;
+}
+
+sub peek {
+ my $self = shift;
+ my $high = max keys $self->{queue}->%*;
+ return undef unless $high;
+
+ return $self->{queue}->{$high}->[0];
+}
+
+sub print {
+ my $self = shift;
+ say "Queue is empty" if $self->is_empty;
+ for my $k ( sort { $b <=> $a } keys $self->{queue}->%* ) {
+ printf "%5d => %s\n", $k, join( ", ", $self->{queue}->{$k}->@* );
+ }
+}
+
+1;
+
+package main;
+
+use v5.24;
+use strict;
+use warnings;
+use Test::More tests => 9;
+
+my $queue = Queue::Priority->new;
+ok( $queue->is_empty, "Queue is empty" );
+
+$queue->insert_with_priority( 2, 8 );
+$queue->insert_with_priority(5);
+$queue->insert_with_priority( 10, 3 );
+$queue->insert_with_priority( 2.78, 3 );
+$queue->insert_with_priority( "Element", 10 );
+$queue->print;
+
+ok( !$queue->is_empty, "Queue is not empty" );
+my $highest = $queue->pull_highest_priority_element;
+is( $highest, "Element", "Next highest element is 'Element'" );
+$highest = $queue->pull_highest_priority_element;
+is( $highest, 2, "Next highest element is '2'" );
+$highest = $queue->pull_highest_priority_element;
+is( $highest, 10, "Next highest element is '10'" );
+is( $queue->peek(), 2.78, "Peek shows next highest element is '2.78'" );
+$highest = $queue->pull_highest_priority_element;
+is( $highest, 2.78, "Next highest element is '2.78'" );
+$highest = $queue->pull_highest_priority_element;
+is( $highest, 5, "Next highest element is '5'" );
+$highest = $queue->pull_highest_priority_element;
+ok( !$highest, "No more elements in queue" );