From 4d7e6b0ab91d0ee7edf3f6656c80729a7e6c9156 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 18 Aug 2024 14:10:28 +0100 Subject: Add Perl solution to challenge 223 --- challenge-223/paulo-custodio/Makefile | 2 + challenge-223/paulo-custodio/perl/ch-1.pl | 45 +++++++++++++++++++++ challenge-223/paulo-custodio/perl/ch-2.pl | 64 ++++++++++++++++++++++++++++++ challenge-223/paulo-custodio/t/test-1.yaml | 15 +++++++ challenge-223/paulo-custodio/t/test-2.yaml | 10 +++++ 5 files changed, 136 insertions(+) create mode 100644 challenge-223/paulo-custodio/Makefile create mode 100644 challenge-223/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-223/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-223/paulo-custodio/t/test-1.yaml create mode 100644 challenge-223/paulo-custodio/t/test-2.yaml 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..433eb4daf1 --- /dev/null +++ b/challenge-223/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/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 -- cgit