aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-223/paulo-custodio/Makefile2
-rw-r--r--challenge-223/paulo-custodio/perl/ch-1.pl45
-rw-r--r--challenge-223/paulo-custodio/perl/ch-2.pl62
-rw-r--r--challenge-223/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-223/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-224/paulo-custodio/Makefile2
-rw-r--r--challenge-224/paulo-custodio/perl/ch-1.pl38
-rw-r--r--challenge-224/paulo-custodio/perl/ch-2.pl65
-rw-r--r--challenge-224/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-224/paulo-custodio/t/test-2.yaml15
10 files changed, 269 insertions, 0 deletions
diff --git a/challenge-223/paulo-custodio/Makefile b/challenge-223/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-223/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-223/paulo-custodio/perl/ch-1.pl b/challenge-223/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..70caf77407
--- /dev/null
+++ b/challenge-223/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+# Challenge 223
+#
+# Task 1: Count Primes
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer, $n.
+#
+# Write a script to find the total count of primes less than or equal to the
+# given integer.
+#
+#
+# Example 1
+# Input: $n = 10
+# Output: 4
+#
+# Since there are 4 primes (2,3,5,7) less than or equal to 10.
+# Example 2
+# Input: $n = 1
+# Output: 0
+# Example 3
+# Input: $n = 20
+# Output: 8
+#
+# Since there are 4 primes (2,3,5,7,11,13,17,19) less than or equal to 20.
+
+use Modern::Perl;
+
+my $n = shift || 0;
+my $count = 0;
+for (2..$n) {
+ $count++ if is_prime($_);
+}
+say $count;
+
+sub is_prime {
+ my($n) = @_;
+ return 0 if $n <= 1;
+ return 1 if $n <= 3;
+ return 0 if ($n % 2)==0 || ($n % 3)==0;
+ for (my $i = 5; $i*$i <= $n; $i += 6) {
+ return 0 if ($n % $i)==0 || ($n % ($i+2))==0;
+ }
+ return 1;
+}
diff --git a/challenge-223/paulo-custodio/perl/ch-2.pl b/challenge-223/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..fadbbeb521
--- /dev/null
+++ b/challenge-223/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+# Challenge 223
+#
+# Task 2: Box Coins
+# Submitted by: Mohammad S Anwar
+# You are given an array representing box coins, @box.
+#
+# Write a script to collect the maximum coins until you took out all boxes.
+# If we pick box[i] then we collect the coins $box[i-1] * $box[i] * $box[i+1].
+# If $box[i+1] or $box[i-1] is out of bound then treat it as 1 coin.
+#
+#
+# Example 1:
+# Input: @box = (3, 1, 5, 8)
+# Output: 167
+#
+# Step 1: pick box [i=1] and collected coins 3 * 1 * 5 => 15. Boxes available (3, 5, 8).
+# Step 2: pick box [i=1] and collected coins 3 * 5 * 8 => 120. Boxes available (3, 8).
+# Step 3: pick box [i=0] and collected coins 1 * 3 * 8 => 24. Boxes available (8).
+# Step 4: pick box [i=0] and collected coins 1 * 8 * 1 => 8. No more box available.
+# Example 2:
+# Input: @box = (1, 5)
+# Output: 10
+#
+# Step 1: pick box [i=0] and collected coins 1 * 1 * 5 => 5. Boxes available (5).
+# Step 2: pick box [i=0] and collected coins 1 * 5 * 1 => 5. No more box available.
+
+use Modern::Perl;
+
+my @box = @ARGV;
+my $max = 0;
+collect_max(\$max, 0, @box);
+say $max;
+
+sub collect_max {
+ my($max, $sum, @box) = @_;
+
+ if (@box <= 1) {
+ $sum += $box[0] if @box;
+ if ($$max < $sum) {
+ $$max = $sum;
+ }
+ return;
+ }
+ else {
+ for my $i (0 .. $#box) {
+ my $collect = collect($i, @box);
+ my @new_box = (@box[0..$i-1], @box[$i+1..$#box]);
+ collect_max($max, $sum+$collect, @new_box);
+ }
+ }
+}
+
+sub collect {
+ my($i, @box) = @_;
+ my $collect = 1;
+ $collect *= $box[$i-1] if $i-1 >= 0;
+ $collect *= $box[$i];
+ $collect *= $box[$i+1] if $i+1 < @box;
+ return $collect;
+}
diff --git a/challenge-223/paulo-custodio/t/test-1.yaml b/challenge-223/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1beb81e0f6
--- /dev/null
+++ b/challenge-223/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: 20
+ input:
+ output: 8
diff --git a/challenge-223/paulo-custodio/t/test-2.yaml b/challenge-223/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..4ed50caa28
--- /dev/null
+++ b/challenge-223/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 3 1 5 8
+ input:
+ output: 167
+- setup:
+ cleanup:
+ args: 1 5
+ input:
+ output: 10
diff --git a/challenge-224/paulo-custodio/Makefile b/challenge-224/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-224/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-224/paulo-custodio/perl/ch-1.pl b/challenge-224/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..09c5d5d636
--- /dev/null
+++ b/challenge-224/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+# Challenge 224
+#
+# Task 1: Special Notes
+# Submitted by: Mohammad S Anwar
+# You are given two strings, $source and $target.
+#
+# Write a script to find out if using the characters (only once) from source,
+# a target string can be created.
+#
+# Example 1
+# Input: $source = "abc"
+# $target = "xyz"
+# Output: false
+# Example 2
+# Input: $source = "scriptinglanguage"
+# $target = "perl"
+# Output: true
+# Example 3
+# Input: $source = "aabbcc"
+# $target = "abc"
+# Output: true
+
+use Modern::Perl;
+
+@ARGV==2 or die "Usage: $0 source target\n";
+my($source, $target) = @ARGV;
+
+say can_make_string($target, $source) ? "true" : "false";
+
+sub can_make_string {
+ my($str, $chars) = @_;
+ for my $ch (split //, $chars) {
+ $str =~ s/$ch//i;
+ }
+ return $str eq '';
+}
diff --git a/challenge-224/paulo-custodio/perl/ch-2.pl b/challenge-224/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..fa079770d8
--- /dev/null
+++ b/challenge-224/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+
+# Challenge 224
+#
+# Task 2: Additive Number
+# Submitted by: Mohammad S Anwar
+# You are given a string containing digits 0-9 only.
+#
+# Write a script to find out if the given string is additive number. An additive
+# number is a string whose digits can form an additive sequence.
+#
+# A valid additive sequence should contain at least 3 numbers. Except the first
+# 2 numbers, each subsequent number in the sequence must be the sum of the
+# preceding two.
+#
+#
+# Example 1:
+# Input: $string = "112358"
+# Output: true
+#
+# The additive sequence can be created using the given string digits: 1,1,2,3,5,8
+# 1 + 1 => 2
+# 1 + 2 => 3
+# 2 + 3 => 5
+# 3 + 5 => 8
+# Example 2:
+# Input: $string = "12345"
+# Output: false
+#
+# No additive sequence can be created using the given string digits.
+# Example 3:
+# Input: $string = "199100199"
+# Output: true
+#
+# The additive sequence can be created using the given string digits: 1,99,100,199
+# 1 + 99 => 100
+# 99 + 100 => 199
+
+use Modern::Perl;
+
+@ARGV==1 or die "Usage: $0 num\n";
+my $num = shift;
+my $result = 0;
+is_addictive_seq(\$result, [], $num);
+say $result ? "true" : "false";
+
+sub is_addictive_seq {
+ my($result, $seq, $num) = @_;
+
+ if (@$seq >= 3 && $seq->[-3]+$seq->[-2]!=$seq->[-1]) {
+ return; # not a sequence
+ }
+ elsif (@$seq >= 3 && $num eq '') {
+ $$result = 1; # found solution
+ return;
+ }
+ elsif ($num eq '') {
+ return; # no solution
+ }
+ else { # add one more number to sequence
+ for my $i (1..length($num)) {
+ is_addictive_seq($result, [@$seq, 0+substr($num,0,$i)], substr($num,$i));
+ }
+ }
+}
diff --git a/challenge-224/paulo-custodio/t/test-1.yaml b/challenge-224/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..568f80d8d0
--- /dev/null
+++ b/challenge-224/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: abc xyz
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: scriptinglanguage perl
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: aabbcc abc
+ input:
+ output: true
diff --git a/challenge-224/paulo-custodio/t/test-2.yaml b/challenge-224/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..c6ec8b6f23
--- /dev/null
+++ b/challenge-224/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 112358
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: 12345
+ input:
+ output: false
+- setup:
+ cleanup:
+ args: 199100199
+ input:
+ output: true