diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-10 11:18:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-10 11:18:38 +0100 |
| commit | f2e1fd959ee3995f5f5c3e022922015af962de21 (patch) | |
| tree | 7e1fc996f6d337d7a8c10ad42dade9376d53ad1c | |
| parent | 857539d45416ab9be590ce0c34965f45887c47d2 (diff) | |
| parent | f9daa08e2104599a6fedc78b2b703e8ca81ca66d (diff) | |
| download | perlweeklychallenge-club-f2e1fd959ee3995f5f5c3e022922015af962de21.tar.gz perlweeklychallenge-club-f2e1fd959ee3995f5f5c3e022922015af962de21.tar.bz2 perlweeklychallenge-club-f2e1fd959ee3995f5f5c3e022922015af962de21.zip | |
Merge pull request #4048 from lakpatashi/challenge-018
Finished challenge-018 ch-2 only with perl
| -rw-r--r-- | challenge-018/lakpatashi/README | 1 | ||||
| -rwxr-xr-x | challenge-018/lakpatashi/perl/ch-2.pl | 45 |
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-018/lakpatashi/README b/challenge-018/lakpatashi/README new file mode 100644 index 0000000000..bc153bd576 --- /dev/null +++ b/challenge-018/lakpatashi/README @@ -0,0 +1 @@ +Solution by lakpatashi diff --git a/challenge-018/lakpatashi/perl/ch-2.pl b/challenge-018/lakpatashi/perl/ch-2.pl new file mode 100755 index 0000000000..de0fa545e0 --- /dev/null +++ b/challenge-018/lakpatashi/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use Data::Dumper; +use List::Util qw(sum min); +use feature qw(switch); + +#part 2 +my %priorityQ = (1 => ['apple','ball']); +insert_with_priority(\%priorityQ,'cat',2); +print pull_highest_priority_element(\%priorityQ),"\n"; +print pull_highest_priority_element(\%priorityQ),"\n"; +if( is_empty(\%priorityQ) ){ + print "empty\n"; +}else{ + print "not empty\n"; +} + +sub insert_with_priority{ + #(pQ,ele,priority) + my ($pQ,$val,$key) = @_; + if( $pQ->{$key} ){ #if key exist + push( $pQ->{$key},$val ); + }else{ + $pQ->{$key} = [$val] ; #key doesn't exist + } +} +sub pull_highest_priority_element{ + #(pQ) + my ($pQ) = @_; + my $key = min keys $pQ; + my $val = shift( $pQ->{$key} ); + delete $pQ->{$key} unless @{$pQ->{$key}}; + return $val; +} + +sub is_empty{ + #(pQ) + my ($pQ) = @_; + return not scalar %{$pQ}; +} + +print '='x10," P Queue ",'='x10,"\n"; +print Dumper \%priorityQ; |
