aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-278/paulo-custodio/Makefile2
-rw-r--r--challenge-278/paulo-custodio/perl/ch-1.pl35
-rw-r--r--challenge-278/paulo-custodio/perl/ch-2.pl34
-rw-r--r--challenge-278/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-278/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-279/paulo-custodio/Makefile2
-rw-r--r--challenge-279/paulo-custodio/perl/ch-1.pl37
-rw-r--r--challenge-279/paulo-custodio/perl/ch-2.pl37
-rw-r--r--challenge-279/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-279/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-281/paulo-custodio/Makefile2
-rw-r--r--challenge-281/paulo-custodio/perl/ch-1.pl36
-rw-r--r--challenge-281/paulo-custodio/perl/ch-2.pl103
-rw-r--r--challenge-281/paulo-custodio/t/test-1.yaml55
-rw-r--r--challenge-281/paulo-custodio/t/test-2.yaml10
15 files changed, 413 insertions, 0 deletions
diff --git a/challenge-278/paulo-custodio/Makefile b/challenge-278/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-278/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-278/paulo-custodio/perl/ch-1.pl b/challenge-278/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..726d509125
--- /dev/null
+++ b/challenge-278/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+# Challenge 278
+#
+# 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"
+
+use Modern::Perl;
+
+say join(' ',
+ map { $_->[0] }
+ sort { $a->[1] <=> $b->[1] }
+ map { /(.*)(\d+)$/; [$1, $2] }
+ @ARGV);
diff --git a/challenge-278/paulo-custodio/perl/ch-2.pl b/challenge-278/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..43636b2692
--- /dev/null
+++ b/challenge-278/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+# Challenge 278
+#
+# 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"
+
+use Modern::Perl;
+
+@ARGV==2 or die "Usage: $0 word letter\n";
+my($word, $letter) = @ARGV;
+
+say $word =~ s{.*?$letter}{ join '', sort split //, $& }er;
diff --git a/challenge-278/paulo-custodio/t/test-1.yaml b/challenge-278/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..4ac0fc3d76
--- /dev/null
+++ b/challenge-278/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: and2 Raku3 cousins5 Perl1 are4
+ input:
+ output: Perl and Raku are cousins
+- setup:
+ cleanup:
+ args: guest6 Python1 most4 the3 popular5 is2 language7
+ input:
+ output: Python is the most popular guest language
+- setup:
+ cleanup:
+ args: Challenge3 The1 Weekly2
+ input:
+ output: The Weekly Challenge
diff --git a/challenge-278/paulo-custodio/t/test-2.yaml b/challenge-278/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..0d132f73ec
--- /dev/null
+++ b/challenge-278/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: challenge e
+ input:
+ output: acehllnge
+- setup:
+ cleanup:
+ args: programming a
+ input:
+ output: agoprrmming
+- setup:
+ cleanup:
+ args: champion b
+ input:
+ output: champion
diff --git a/challenge-279/paulo-custodio/Makefile b/challenge-279/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-279/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-279/paulo-custodio/perl/ch-1.pl b/challenge-279/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..8bd06d89d1
--- /dev/null
+++ b/challenge-279/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+# Challenge 279
+#
+# Task 1: Sort Letters
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given two arrays, @letters and @weights.
+#
+# Write a script to sort the given array @letters based on the @weights.
+# Example 1
+#
+# Input: @letters = ('R', 'E', 'P', 'L')
+# @weights = (3, 2, 1, 4)
+# Output: PERL
+#
+# Example 2
+#
+# Input: @letters = ('A', 'U', 'R', 'K')
+# @weights = (2, 4, 1, 3)
+# Output: RAKU
+#
+# Example 3
+#
+# Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
+# @weights = (5, 4, 2, 6, 1, 3)
+# Output: PYTHON
+
+use Modern::Perl;
+
+@ARGV > 2 or die "Usage: $0 LETTTERS WEIGHTS...\n";
+my($letters, @weights) = @ARGV;
+my @letters = split //, $letters;
+say join('',
+ map {$_->[0]}
+ sort {$a->[1] <=> $b->[1]}
+ map {[$letters[$_], $weights[$_]]} 0..$#letters);
diff --git a/challenge-279/paulo-custodio/perl/ch-2.pl b/challenge-279/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3c3b9bf3e5
--- /dev/null
+++ b/challenge-279/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+# Challenge 279
+#
+# Task 2: Split String
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str.
+#
+# Write a script to split the given string into two containing exactly same
+# number of vowels and return true if you can otherwise false.
+#
+# Example 1
+#
+# Input: $str = "perl"
+# Ouput: false
+#
+# Example 2
+#
+# Input: $str = "book"
+# Ouput: true
+#
+# Two possible strings "bo" and "ok" containing exactly one vowel each.
+#
+# Example 3
+#
+# Input: $str = "good morning"
+# Ouput: true
+#
+# Two possible strings "good " and "morning" containing two vowels each or
+# "good m" and "orning" containing two vowels each.
+
+use Modern::Perl;
+
+my $str = "@ARGV";
+my $vowels = $str =~ tr/aeiouAEIOU/aeiouAEIOU/;
+say $vowels % 2 == 0 ? "true" : "false"; # actual splitting is not necessary
diff --git a/challenge-279/paulo-custodio/t/test-1.yaml b/challenge-279/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..4a196af484
--- /dev/null
+++ b/challenge-279/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: REPL 3 2 1 4
+ input:
+ output: PERL
+- setup:
+ cleanup:
+ args: AURK 2 4 1 3
+ input:
+ output: RAKU
+- setup:
+ cleanup:
+ args: OHYNPT 5 4 2 6 1 3
+ input:
+ output: PYTHON
diff --git a/challenge-279/paulo-custodio/t/test-2.yaml b/challenge-279/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..ce5b158c3d
--- /dev/null
+++ b/challenge-279/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: perl
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: book
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: good morning
+ input:
+ output: true
diff --git a/challenge-281/paulo-custodio/Makefile b/challenge-281/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-281/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-281/paulo-custodio/perl/ch-1.pl b/challenge-281/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..0ba89a2b98
--- /dev/null
+++ b/challenge-281/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 281
+#
+# Task 1: Check Color
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given coordinates, a string that represents the coordinates of a
+# square of the chessboard as shown below:
+#
+# Write a script to return true if the square is light, and false if the square
+# is dark.
+#
+# Example 1
+#
+# Input: $coordinates = "d3"
+# Output: true
+#
+# Example 2
+#
+# Input: $coordinates = "g5"
+# Output: false
+#
+# Example 3
+#
+# Input: $coordinates = "e6"
+# Output: true
+
+use Modern::Perl;
+
+@ARGV==1 or die "Usage: $0 coordinates\n";
+(my $coords = shift) =~ /^[a-h][1-8]$/ or die "Usage: $0 coordinates\n";
+
+my @coords = split //, $coords;
+my $light = (((ord($coords[0])-ord('a'))) & 1) ^ (($coords[1]-1) & 1);
+say $light ? "true" : "false";
diff --git a/challenge-281/paulo-custodio/perl/ch-2.pl b/challenge-281/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..4489f672c3
--- /dev/null
+++ b/challenge-281/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,103 @@
+#!/usr/bin/env perl
+
+# Challenge 281
+#
+# Task 2: Knight’s Move
+# Submitted by: Peter Campbell Smith
+#
+# A Knight in chess can move from its current position to any square two rows
+# or columns plus one column or row away. So in the diagram below, if it starts
+# a S, it can move to any of the squares marked E.
+#
+# Write a script which takes a starting position and an ending position and
+# calculates the least number of moves required.
+#
+# Example 1
+#
+# Input: $start = 'g2', $end = 'a8'
+# Ouput: 4
+#
+# g2 -> e3 -> d5 -> c7 -> a8
+#
+# Example 2
+#
+# Input: $start = 'g2', $end = 'h2'
+# Ouput: 3
+#
+# g2 -> e3 -> f1 -> h2
+
+use Modern::Perl;
+
+@ARGV==2 or die "Usage: $0 start end\n";
+my($start, $end) = @ARGV;
+for ($start, $end) {
+ /^[a-h][1-8]$/ or die "Usage: $0 start end\n";
+}
+say find_path_size($start, $end);
+
+sub find_path_size {
+ my($start, $end) = @_;
+
+ my $min_path;
+ my @paths = ([[$start], {$start=>1}]);
+ while (@paths) {
+ my @top = @{shift(@paths)};
+ my @path = @{$top[0]};
+ my %seen = %{$top[1]};
+
+ # check if we found a solution
+ if ($path[-1] eq $end) { # found one solution
+ if (!defined($min_path) || $min_path > scalar(@path)) {
+ $min_path = scalar(@path); # found shorter solution
+ }
+ }
+
+ # prune the tree
+ if (defined($min_path) && scalar(@path) > $min_path) {
+ next;
+ }
+
+ # find 8 next positions and push them to @paths
+ my @dest = knight_jump($path[-1]);
+ for (@dest) {
+ if (!$seen{$_}) {
+ push @paths, [[@path, $_], {%seen, $_=>1}];
+ }
+ }
+ }
+ return $min_path-1; # remove start position
+}
+
+sub row_col {
+ my($coord) = @_;
+ $coord =~ /^[a-h][1-8]$/ or die;
+ my @coord = split //, $coord;
+ my $row = 8 - $coord[1];
+ my $col = (ord($coord[0])-ord('a'));
+ return ($row, $col);
+}
+
+sub coord {
+ my($row, $col) = @_;
+ return if $row<0 || $row>7;
+ return if $col<0 || $col>7;
+ return chr($col+ord('a')).(8-$row);
+}
+
+sub knight_jump {
+ my($start) = @_;
+ my($row, $col) = row_col($start);
+ my @dest;
+ my $end;
+
+ $end = coord($row-2, $col-1); push @dest, $end if defined $end;
+ $end = coord($row-2, $col+1); push @dest, $end if defined $end;
+ $end = coord($row+2, $col-1); push @dest, $end if defined $end;
+ $end = coord($row+2, $col+1); push @dest, $end if defined $end;
+ $end = coord($row-1, $col-2); push @dest, $end if defined $end;
+ $end = coord($row+1, $col-2); push @dest, $end if defined $end;
+ $end = coord($row-1, $col+2); push @dest, $end if defined $end;
+ $end = coord($row+1, $col+2); push @dest, $end if defined $end;
+
+ return @dest;
+}
diff --git a/challenge-281/paulo-custodio/t/test-1.yaml b/challenge-281/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..fb74dd612e
--- /dev/null
+++ b/challenge-281/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,55 @@
+- setup:
+ cleanup:
+ args: a1
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: b1
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: g1
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: h1
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: a2
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: b2
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: g8
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: h8
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: d3
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: g5
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: e6
+ input:
+ output: true
diff --git a/challenge-281/paulo-custodio/t/test-2.yaml b/challenge-281/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..0cab37a46f
--- /dev/null
+++ b/challenge-281/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: g2 a8
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: g2 h2
+ input:
+ output: 3