diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-07-27 19:43:06 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-07-27 19:43:06 -0600 |
| commit | e34df1af5b74562babaf15e1eb6c93faa8fef1f3 (patch) | |
| tree | d083c41acc67d58bb11c432e1342c1682e1957bf /challenge-018 | |
| parent | fb872e922475cb55c557b5b1a9c56674418264c1 (diff) | |
| download | perlweeklychallenge-club-e34df1af5b74562babaf15e1eb6c93faa8fef1f3.tar.gz perlweeklychallenge-club-e34df1af5b74562babaf15e1eb6c93faa8fef1f3.tar.bz2 perlweeklychallenge-club-e34df1af5b74562babaf15e1eb6c93faa8fef1f3.zip | |
Solution to 18.2 in P6 and P5
Diffstat (limited to 'challenge-018')
| -rwxr-xr-x | challenge-018/joelle-maslak/perl5/ch-2.pl | 57 | ||||
| -rwxr-xr-x | challenge-018/joelle-maslak/perl5/lib/PriorityQueue.pm | 50 | ||||
| -rwxr-xr-x | challenge-018/joelle-maslak/perl6/ch-2.p6 | 77 |
3 files changed, 184 insertions, 0 deletions
diff --git a/challenge-018/joelle-maslak/perl5/ch-2.pl b/challenge-018/joelle-maslak/perl5/ch-2.pl new file mode 100755 index 0000000000..e017c62f40 --- /dev/null +++ b/challenge-018/joelle-maslak/perl5/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +use v5.22; +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; + +# See lib/PriorityQueue.pm +use PriorityQueue; + +use Test2::V0; + +my $pk = PriorityQueue->new(); + +subtest 'Start empty' => sub { + ok $pk->is_empty(); +}; + +subtest 'One element insert' => sub { + $pk->insert_with_priority( 'A', 50 ); + ok !$pk->is_empty; + is $pk->pull_highest_priority_element, 'A'; + ok $pk->is_empty; +}; + +subtest 'With Multiple Priorities' => sub { + $pk->insert_with_priority( 'A', 50 ); + $pk->insert_with_priority( 'B', 50 ); + $pk->insert_with_priority( 'C', 75 ); + $pk->insert_with_priority( 'D', 50 ); + $pk->insert_with_priority( 'E', 10 ); + $pk->insert_with_priority( 'F', 10 ); + + ok !$pk->is_empty; + is $pk->pull_highest_priority_element(), 'C'; + + $pk->insert_with_priority( 'G', 75 ); + is $pk->pull_highest_priority_element(), 'G'; + + is $pk->pull_highest_priority_element(), 'A'; + is $pk->pull_highest_priority_element(), 'B'; + is $pk->pull_highest_priority_element(), 'D'; + is $pk->pull_highest_priority_element(), 'E'; + is $pk->pull_highest_priority_element(), 'F'; + + ok $pk->is_empty; +}; + +done_testing; + diff --git a/challenge-018/joelle-maslak/perl5/lib/PriorityQueue.pm b/challenge-018/joelle-maslak/perl5/lib/PriorityQueue.pm new file mode 100755 index 0000000000..e2e447999e --- /dev/null +++ b/challenge-018/joelle-maslak/perl5/lib/PriorityQueue.pm @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +use v5.22; +use strict; +use warnings; + +package PriorityQueue; + +use Moose; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; +use List::Util qw(max); + +has 'priorities' => ( + is => 'rw', + isa => 'HashRef', + default => sub { {} }, +); + +has 'maxprio' => ( is => 'rw', ); + +sub is_empty($self) { return !keys $self->priorities->%* } + +sub insert_with_priority ( $self, $elem, $priority ) { + $self->priorities->{ +$priority } //= []; + push $self->priorities->{ +$priority }->@*, $elem; + + if ( ( !defined( $self->maxprio ) ) or ( $priority > $self->maxprio ) ) { + $self->maxprio( max keys $self->priorities->%* ); + } + + return; +} + +sub pull_highest_priority_element($self) { + my $elem = shift $self->priorities->{ $self->maxprio }->@*; + + if ( $self->priorities->{ $self->maxprio }->@* == 0 ) { + delete $self->priorities->{ $self->maxprio }; + $self->maxprio( max keys $self->priorities->%* ); + } + + return $elem; +} + +1; + diff --git a/challenge-018/joelle-maslak/perl6/ch-2.p6 b/challenge-018/joelle-maslak/perl6/ch-2.p6 new file mode 100755 index 0000000000..9378f7a9e6 --- /dev/null +++ b/challenge-018/joelle-maslak/perl6/ch-2.p6 @@ -0,0 +1,77 @@ +#!/usr/bin/env perl6 +use v6; + +# +# Copyright © 2019 Joelle Maslak +# All Rights Reserved - See License +# + +class Priority-Queue { + has %!priorities; + has $!max; + + method is_empty() { return ! %!priorities.keys.elems } + + method insert_with_priority($element, $priority) { + %!priorities{$priority} //= []; + %!priorities{$priority}.push: $element; + + if (! $!max.defined) or ($priority > $!max) { + $!max = $priority; + } + } + + method pull_highest_priority_element() { + my $elem = %!priorities{$!max}.shift; + + if %!priorities{$!max}.elems == 0 { + %!priorities{$!max}:delete; + $!max = %!priorities.keys.max; + } + + return $elem; + } +} + +sub MAIN() { + use Test; + + my $pk = Priority-Queue.new; + + subtest 'Start empty', { + is $pk.is_empty, True; + } + + subtest 'One element insert', { + $pk.insert_with_priority('A', 50); + is $pk.is_empty, False; + is $pk.pull_highest_priority_element, 'A'; + is $pk.is_empty, True; + } + + subtest 'With Multiple Priorities', { + $pk.insert_with_priority('A', 50); + $pk.insert_with_priority('B', 50); + $pk.insert_with_priority('C', 75); + $pk.insert_with_priority('D', 50); + $pk.insert_with_priority('E', 10); + $pk.insert_with_priority('F', 10); + + is $pk.is_empty, False; + is $pk.pull_highest_priority_element, 'C'; + + $pk.insert_with_priority('G', 75); + is $pk.pull_highest_priority_element, 'G'; + + is $pk.pull_highest_priority_element, 'A'; + is $pk.pull_highest_priority_element, 'B'; + is $pk.pull_highest_priority_element, 'D'; + is $pk.pull_highest_priority_element, 'E'; + is $pk.pull_highest_priority_element, 'F'; + + is $pk.is_empty, True; + } + + done-testing; +} + |
