aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-214/paulo-custodio/Makefile2
-rw-r--r--challenge-214/paulo-custodio/perl/ch-1.pl95
-rw-r--r--challenge-214/paulo-custodio/perl/ch-2.pl74
-rw-r--r--challenge-214/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-214/paulo-custodio/t/test-2.yaml20
5 files changed, 211 insertions, 0 deletions
diff --git a/challenge-214/paulo-custodio/Makefile b/challenge-214/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-214/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-214/paulo-custodio/perl/ch-1.pl b/challenge-214/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..0296e952f1
--- /dev/null
+++ b/challenge-214/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+
+# Challenge 214
+#
+# Task 1: Rank Score
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of scores (>=1).
+#
+# Write a script to rank each score in descending order. First three will get
+# medals i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get the
+# ranking number.
+#
+# Using the standard model of giving equal scores equal rank, then
+# advancing that number of ranks.
+#
+#
+# Example 1
+#
+# Input: @scores = (1,2,4,3,5)
+# Output: (5,4,S,B,G)
+#
+# Score 1 is the 5th rank.
+# Score 2 is the 4th rank.
+# Score 4 is the 2nd rank i.e. Silver (S).
+# Score 3 is the 3rd rank i.e. Bronze (B).
+# Score 5 is the 1st rank i.e. Gold (G).
+#
+# Example 2
+#
+# Input: @scores = (8,5,6,7,4)
+# Output: (G,4,B,S,5)
+#
+# Score 8 is the 1st rank i.e. Gold (G).
+# Score 4 is the 4th rank.
+# Score 6 is the 3rd rank i.e. Bronze (B).
+# Score 7 is the 2nd rank i.e. Silver (S).
+# Score 4 is the 5th rank.
+#
+# Example 3
+#
+# Input: @list = (3,5,4,2)
+# Output: (B,G,S,4)
+#
+# Example 4
+#
+# Input: @scores = (2,5,2,1,7,5,1)
+# Output: (4,S,4,6,G,S,6)
+
+use Modern::Perl;
+
+# in: list of numbers
+# out: list of items with the same value reverse-ordered by value
+# each item is [index, value]
+sub rank_values {
+ my(@list) = @_;
+ my @sorted = reverse sort {$a->[1] <=> $b->[1]}
+ map {[$_, $list[$_]]} 0..$#list;
+ my @output;
+ while (@sorted) {
+ my @head;
+ my $first_value = $sorted[0][1];
+ while (@sorted && $sorted[0][1] == $first_value) {
+ push @head, shift @sorted;
+ }
+ push @output, \@head;
+ }
+ return @output;
+}
+
+# in: list of numbers
+# out: corresponding list of ranking
+sub standard_ranking {
+ my(@list) = @_;
+ my @ranked = rank_values(@list);
+ my @ranks;
+ my $rank = 1;
+ while (@ranked) {
+ my @head = @{shift @ranked};
+ for (@head) {
+ $ranks[$_->[0]] = $rank;
+ }
+ $rank += @head;
+ }
+
+ for (@ranks) {
+ s/^1$/G/;
+ s/^2$/S/;
+ s/^3$/B/;
+ }
+
+ return @ranks;
+}
+
+say join(" ", standard_ranking(@ARGV));
diff --git a/challenge-214/paulo-custodio/perl/ch-2.pl b/challenge-214/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..29c12ad197
--- /dev/null
+++ b/challenge-214/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+
+# Challenge 214
+#
+# Task 2: Collect Points
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of numbers.
+#
+# You will perform a series of removal operations. For each operation, you
+# remove from the list N (one or more) equal and consecutive numbers, and
+# add to your score N × N.
+#
+# Determine the maximum possible score.
+# Example 1:
+#
+# Input: @numbers = (2,4,3,3,3,4,5,4,2)
+# Output: 23
+#
+# We see three 3's next to each other so let us remove that first and
+# collect 3 x 3 points.
+# So now the list is (2,4,4,5,4,2).
+# Let us now remove 5 so that all 4's can be next to each other and
+# collect 1 x 1 point.
+# So now the list is (2,4,4,4,2).
+# Time to remove three 4's and collect 3 x 3 points.
+# Now the list is (2,2).
+# Finally remove both 2's and collect 2 x 2 points.
+# So the total points collected is 9 + 1 + 9 + 4 => 23.
+#
+# Example 2:
+#
+# Input: @numbers = (1,2,2,2,2,1)
+# Output: 20
+#
+# Remove four 2's first and collect 4 x 4 points.
+# Now the list is (1,1).
+# Finally remove the two 1's and collect 2 x 2 points.
+# So the total points collected is 16 + 4 => 20.
+#
+# Example 3:
+#
+# Input: @numbers = (1)
+# Output: 1
+#
+# Example 4:
+#
+# Input: @numbers = (2,2,2,1,1,2,2,2)
+# Output: 40
+#
+# Remove two 1's = 2 x 2 points.
+# Now the list is (2,2,2,2,2,2).
+# Then reomove six 2's = 6 x 6 points.
+
+use Modern::Perl;
+
+sub remove_numbers {
+ my(@in) = @_;
+ return scalar(@in) if @in < 2;
+ my $best_score = 0;
+ for (my $s = 0; $s < @in; $s++) {
+ my $e = $s;
+ while ($e < @in && $in[$s]==$in[$e]) {
+ $e++;
+ }
+ my @new_list = (@in[0..$s-1], @in[$e..$#in]);
+ my $score = ($e-$s)*($e-$s) + remove_numbers(@new_list);
+ $best_score = $score if $best_score < $score;
+ $s = $e-1;
+ }
+ return $best_score;
+}
+
+say remove_numbers(@ARGV);
diff --git a/challenge-214/paulo-custodio/t/test-1.yaml b/challenge-214/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..a4e13cd4ac
--- /dev/null
+++ b/challenge-214/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 2 4 3 5
+ input:
+ output: 5 4 S B G
+- setup:
+ cleanup:
+ args: 8 5 6 7 4
+ input:
+ output: G 4 B S 5
+- setup:
+ cleanup:
+ args: 3 5 4 2
+ input:
+ output: B G S 4
+- setup:
+ cleanup:
+ args: 2 5 2 1 7 5 1
+ input:
+ output: 4 S 4 6 G S 6
diff --git a/challenge-214/paulo-custodio/t/test-2.yaml b/challenge-214/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..104f6ef58c
--- /dev/null
+++ b/challenge-214/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 2 4 3 3 3 4 5 4 2
+ input:
+ output: 23
+- setup:
+ cleanup:
+ args: 1 2 2 2 2 1
+ input:
+ output: 20
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 2 2 2 1 1 2 2 2
+ input:
+ output: 40