aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-284/mgo1977/perl/ch-1.pl66
-rw-r--r--challenge-284/mgo1977/perl/ch-2.pl104
2 files changed, 170 insertions, 0 deletions
diff --git a/challenge-284/mgo1977/perl/ch-1.pl b/challenge-284/mgo1977/perl/ch-1.pl
new file mode 100644
index 0000000000..358663a555
--- /dev/null
+++ b/challenge-284/mgo1977/perl/ch-1.pl
@@ -0,0 +1,66 @@
+#!/bin/perl -w
+
+
+use Data::Dump qw(dump);
+
+
+# Task 1: Lucky Integer
+# Submitted by: Mohammad Sajid Anwar
+# You are given an array of integers, @ints.
+
+# Write a script to find the lucky integer if found otherwise return -1. If there are more than one then return the largest.
+
+# A lucky integer is an integer that has a frequency in the array equal to its value.
+
+# Example 1
+# Input: @ints = (2, 2, 3, 4)
+# Output: 2
+# Example 2
+# Input: @ints = (1, 2, 2, 3, 3, 3)
+# Output: 3
+# Example 3
+# Input: @ints = (1, 1, 1, 3)
+# Output: -1
+
+
+
+testMe(\&process, 'Example1', [2, 2, 3, 4], 2);
+testMe(\&process, 'Example2', [1, 2, 2, 3, 3, 3], 3);
+testMe(\&process, 'Example3', [1, 1, 1, 3], -1);
+
+sub testMe {
+ my $processor = shift;
+ my $name = shift;
+ my $input1 = shift;
+ my $expectedValue = shift;
+
+ my $got = &$processor($input1);
+
+ if ( $got == $expectedValue ) {
+ print "[OK ] $name\n";
+ }
+ else {
+ print "[ERR] $name :: got='$got', expectedValue='$expectedValue'\n";
+ }
+
+}
+
+
+sub process {
+ my $input1 = shift;
+
+ my $map = {};
+
+ foreach my $v ( @$input1 ) {
+ $map->{$v}++;
+ }
+
+ foreach my $v ( reverse sort keys %$map ) {
+ if ( $map->{$v}==$v ) {
+ return $v;
+ }
+ }
+
+ return -1;
+
+}
diff --git a/challenge-284/mgo1977/perl/ch-2.pl b/challenge-284/mgo1977/perl/ch-2.pl
new file mode 100644
index 0000000000..2fce93dcf6
--- /dev/null
+++ b/challenge-284/mgo1977/perl/ch-2.pl
@@ -0,0 +1,104 @@
+#!/bin/perl -w
+
+
+# Task 2: Relative Sort
+# Submitted by: Mohammad Sajid Anwar
+# You are given two list of integers, @list1 and @list2. The elements in the @list2 are distinct and also in the @list1.
+
+# Write a script to sort the elements in the @list1 such that the relative order of items in @list1 is same as in the @list2. Elements that is missing in @list2 should be placed at the end of @list1 in ascending order.
+
+# Example 1
+# Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5)
+# @list2 = (2, 1, 4, 3, 5, 6)
+# Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9)
+
+# Example 2
+# Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3)
+# @list2 = (1, 3, 2)
+# Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6)
+
+# Example 3
+# Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1)
+# @list2 = (1, 0, 3, 2)
+# Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5)
+
+
+
+testMe(\&process, 'Example1', [2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5], [2, 1, 4, 3, 5, 6], [2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9]);
+testMe(\&process, 'Example2', [3, 3, 4, 6, 2, 4, 2, 1, 3], [1, 3, 2], [1, 3, 3, 3, 2, 2, 4, 4, 6]);
+testMe(\&process, 'Example3', [3, 0, 5, 0, 2, 1, 4, 1, 1], [1, 0, 3, 2], [1, 1, 1, 0, 0, 3, 2, 4, 5]);
+
+
+
+sub testMe {
+ my $processor = shift;
+ my $name = shift;
+ my $input1 = shift;
+ my $input2 = shift;
+ my $expectedValue = shift;
+
+ my $got = &$processor($input1, $input2);
+
+ if ( areEquals($got, $expectedValue) ) {
+ print "[OK ] $name\n";
+ }
+ else {
+ print "[ERR] $name :: got=@$got, expectedValue=@$expectedValue\n";
+ }
+
+}
+
+sub areEquals {
+ my $a1 = shift;
+ my $a2 = shift;
+
+ if ( @$a1!=@$a2 ) {
+ return 0;
+ }
+
+ for(my $i=0; $i<@$a1; ++$i) {
+
+ if ( $a1->[$i] != $a2->[$i] ) {
+ return 0;
+ }
+
+ }
+
+
+ return 1;
+}
+
+sub process {
+ my $input1 = shift;
+ my $input2 = shift;
+
+ my $positionMap = {};
+
+ for(my $i=0; $i <@$input2; ++$i) {
+ $positionMap->{ $input2->[$i] } = $i;
+ }
+
+ my @found;
+ my @missed;
+
+ foreach my $el ( @$input1 ) {
+
+ if ( not exists $positionMap->{$el} ) {
+ push(@missed, $el);
+ }
+ else {
+ push(@found, $el);
+ }
+
+
+ }
+
+ my @result = (
+ ( sort { $positionMap->{$a} <=> $positionMap->{$b} } @found ),
+ ( sort { $a <=> $b } @missed)
+ );
+
+ return \@result;
+
+}
+