aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlakpatashi <lakpatashi@gmail.com>2021-05-10 12:56:31 +0530
committerlakpatashi <lakpatashi@gmail.com>2021-05-10 12:56:31 +0530
commit2d54689b0c64905cfe62c534d22a9b6b84141fbf (patch)
treec3f337883b88ca4439f4633faa32413aa5effaf2
parent4f996b5dd45569e1fafb33cec89534287f1654cc (diff)
downloadperlweeklychallenge-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/README1
-rwxr-xr-xchallenge-018/lakpatashi/perl/ch-1.pl45
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;