aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-278/mgo1977/perl/ch-1.pl87
-rw-r--r--challenge-278/mgo1977/perl/ch-2.pl68
2 files changed, 155 insertions, 0 deletions
diff --git a/challenge-278/mgo1977/perl/ch-1.pl b/challenge-278/mgo1977/perl/ch-1.pl
new file mode 100644
index 0000000000..38497c0eb4
--- /dev/null
+++ b/challenge-278/mgo1977/perl/ch-1.pl
@@ -0,0 +1,87 @@
+#!/bin/perl -w
+
+
+use Data::Dump qw(dump);
+
+
+# Task 1: Sort String
+# Submitted by: Mohammad Sajid Anwar
+# You are given a shuffle string, $str.
+
+# Write a script to return the sorted string.
+
+# A string is shuffled by appending word position to each word.
+
+# Example 1
+# Input: $str = "and2 Raku3 cousins5 Perl1 are4"
+# Output: "Perl and Raku are cousins"
+# Example 2
+# Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7"
+# Output: "Python is the most popular guest language"
+# Example 3
+# Input: $str = "Challenge3 The1 Weekly2"
+# Output: "The Weekly Challenge"
+
+
+testMe(\&process, 'Example1', "and2 Raku3 cousins5 Perl1 are4", "Perl and Raku are cousins");
+testMe(\&process, 'Example2', "guest6 Python1 most4 the3 popular5 is2 language7", "Python is the most popular guest language");
+testMe(\&process, 'Example3', "Challenge3 The1 Weekly2", "The Weekly Challenge");
+
+sub testMe {
+ my $processor = shift;
+ my $name = shift;
+ my $input1 = shift;
+ my $expectedValue = shift;
+
+ my $got = &$processor($input1);
+
+ if ( $got eq $expectedValue ) {
+ print "[OK ] $name\n";
+ }
+ else {
+ print "[ERR] $name :: got='$got', expectedValue='$expectedValue'\n";
+ }
+
+}
+
+
+sub process {
+ my $input1 = shift;
+
+ my @words = split(/\s+/, $input1);
+
+ my @sorted = sort {
+ my $i1 = getIndex($a);
+ my $i2 = getIndex($b);
+ $i1 <=> $i2;
+ } @words;
+
+ my $str = join(' ', map { stripIndex($_) } @sorted);
+
+ # print "words=" . dump(\@words) . "\n";
+ # print "sorted words=" . dump(\@sorted) . "\n";
+ # print "str=" . $str . "\n";
+
+ return $str;
+
+}
+
+
+sub getIndex {
+ my $word = shift;
+
+ if ( $word =~ /(\d+)$/ ) {
+ return int($1);
+ }
+
+ return -1;
+}
+
+
+sub stripIndex {
+ my $word = shift;
+
+ $word =~ s/\d+$//;
+
+ return $word;
+}
diff --git a/challenge-278/mgo1977/perl/ch-2.pl b/challenge-278/mgo1977/perl/ch-2.pl
new file mode 100644
index 0000000000..a4332359c7
--- /dev/null
+++ b/challenge-278/mgo1977/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/bin/perl -w
+
+
+# Task 2: Reverse Word
+# Submitted by: Mohammad Sajid Anwar
+# You are given a word, $word and a character, $char.
+
+# Write a script to replace the substring up to and including $char with its characters sorted alphabetically. If the $char doesn’t exist then DON'T do anything.
+
+# Example 1
+# Input: $str = "challenge", $char = "e"
+# Ouput: "acehllnge"
+
+# Example 2
+# Input: $str = "programming", $char = "a"
+# Ouput: "agoprrmming"
+
+# Example 3
+# Input: $str = "champion", $char = "b"
+# Ouput: "champion"
+
+testMe(\&process, 'Example1', "challenge", "e", "acehllnge");
+testMe(\&process, 'Example2', "programming", "a", "agoprrmming");
+testMe(\&process, 'Example3', "champion", "b", "champion");
+testMe(\&process, 'Example4', "champion", "n", "achimnop");
+testMe(\&process, 'Example5', "", "n", "");
+testMe(\&process, 'Example6', "purePerl", "P", "Pepruerl");
+
+sub testMe {
+ my $processor = shift;
+ my $name = shift;
+ my $input1 = shift;
+ my $input2 = shift;
+ my $expectedValue = shift;
+
+ my $got = &$processor($input1, $input2);
+
+ if ( $got eq $expectedValue ) {
+ print "[OK ] $name\n";
+ }
+ else {
+ print "[ERR] $name :: got=$got, expectedValue=$expectedValue\n";
+ }
+
+}
+
+sub process {
+ my $word = shift;
+ my $char = shift;
+
+ my $index = index($word, $char);
+
+ if ( $index==-1 ) {
+ return $word;
+ }
+
+ my $p1 = substr($word, 0, $index+1);
+ my $p2 = substr($word, $index+1);
+
+ my $sortedP1 = join('', sort { $a cmp $b } split(//, $p1));
+
+ # print "word='$word' char='$char' (index=$index) p1='$p1' sortedP1='$sortedP1' p2='$p2'\n";
+
+ return $sortedP1 . $p2;
+}
+
+
+