diff options
| author | lakpatashi <lakpatashi@gmail.com> | 2021-05-10 12:56:31 +0530 |
|---|---|---|
| committer | lakpatashi <lakpatashi@gmail.com> | 2021-05-10 12:56:31 +0530 |
| commit | 2d54689b0c64905cfe62c534d22a9b6b84141fbf (patch) | |
| tree | c3f337883b88ca4439f4633faa32413aa5effaf2 | |
| parent | 4f996b5dd45569e1fafb33cec89534287f1654cc (diff) | |
| download | perlweeklychallenge-club-2d54689b0c64905cfe62c534d22a9b6b84141fbf.tar.gz perlweeklychallenge-club-2d54689b0c64905cfe62c534d22a9b6b84141fbf.tar.bz2 perlweeklychallenge-club-2d54689b0c64905cfe62c534d22a9b6b84141fbf.zip | |
Finished challenge-018 ch-2 only with perl
| -rw-r--r-- | challenge-018/lakpatashi/README | 1 | ||||
| -rwxr-xr-x | challenge-018/lakpatashi/perl/ch-1.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-1.pl b/challenge-018/lakpatashi/perl/ch-1.pl new file mode 100755 index 0000000000..de0fa545e0 --- /dev/null +++ b/challenge-018/lakpatashi/perl/ch-1.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; |
