aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2023-11-28 11:27:29 +0100
committerMariano Spadaccini <spadacciniweb@gmail.com>2023-11-28 11:27:29 +0100
commitf5a9f5585542ee68199517ca9bdf474223c4b1dc (patch)
treec0518651f694d581f107f0d982a1e27a6851db46
parenta91d49494a545d745c5c622afa3a9646bf1ac774 (diff)
downloadperlweeklychallenge-club-f5a9f5585542ee68199517ca9bdf474223c4b1dc.tar.gz
perlweeklychallenge-club-f5a9f5585542ee68199517ca9bdf474223c4b1dc.tar.bz2
perlweeklychallenge-club-f5a9f5585542ee68199517ca9bdf474223c4b1dc.zip
PWC-245: Perl ch-1
-rw-r--r--challenge-245/spadacciniweb/perl/ch-1.pl51
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-245/spadacciniweb/perl/ch-1.pl b/challenge-245/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..358cd2004a
--- /dev/null
+++ b/challenge-245/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+# Task 1: Sort Language
+# Submitted by: Mohammad S Anwar
+#
+# You are given two array of languages and its popularity.
+# Write a script to sort the language based on popularity.
+#
+# Example 1
+# Input: @lang = ('perl', 'c', 'python')
+# @popularity = (2, 1, 3)
+# Output: ('c', 'perl', 'python')
+#
+# Example 2
+# Input: @lang = ('c++', 'haskell', 'java')
+# @popularity = (1, 3, 2)
+# Output: ('c++', 'java', 'haskell')
+
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+
+my @lang = ('perl', 'c', 'python');
+my @popularity = (2, 1, 3);
+order_lang(\@lang, \@popularity);
+
+@lang = ('c++', 'haskell', 'java');
+@popularity = (1, 3, 2);
+order_lang(\@lang, \@popularity);
+
+@lang = ('c++', 'haskell', 'java', 'perl', 'c', 'python');
+@popularity = (1, 3, 2, 2, 1, 3);
+order_lang(\@lang, \@popularity);
+
+exit 0;
+
+sub order_lang {
+ my $lang = shift;
+ my $popularity = shift;
+
+ my %lang;
+ foreach my $i (0..$#lang) {
+ push @{ $lang{ $popularity[$i] } }, $lang[$i];
+ }
+
+ printf "language based on popularity: (%s)\n",
+ join ', ', map { join ', ', @{ $lang{$_} } }
+ sort { $a <=> $b } keys %lang;
+}