aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-08 10:32:12 +0100
committerGitHub <noreply@github.com>2021-06-08 10:32:12 +0100
commitf222def231897cc2879d94fb1b591039c9d31940 (patch)
tree8d3ad85882b223850d52d8364304cb2b171d190d
parent55c13b2c9496b0802389c90c367ec6edc59d00f7 (diff)
parent1117ab37226ead53080f0e4354d590bae7a3844f (diff)
downloadperlweeklychallenge-club-f222def231897cc2879d94fb1b591039c9d31940.tar.gz
perlweeklychallenge-club-f222def231897cc2879d94fb1b591039c9d31940.tar.bz2
perlweeklychallenge-club-f222def231897cc2879d94fb1b591039c9d31940.zip
Merge pull request #4226 from pauloscustodio/paulo-custodio
Add Perl solutions to challenges 114,115 and 116
-rwxr-xr-x[-rw-r--r--]challenge-108/paulo-custodio/perl/ch-1.pl6
-rwxr-xr-x[-rw-r--r--]challenge-108/paulo-custodio/perl/ch-2.pl4
-rwxr-xr-x[-rw-r--r--]challenge-108/paulo-custodio/test.pl2
-rw-r--r--challenge-114/paulo-custodio/perl/ch-1.pl35
-rw-r--r--challenge-114/paulo-custodio/perl/ch-2.pl44
-rw-r--r--challenge-114/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-114/paulo-custodio/t/test-2.yaml10
-rwxr-xr-xchallenge-114/paulo-custodio/test.pl7
-rw-r--r--challenge-115/paulo-custodio/perl/ch-1.pl46
-rw-r--r--challenge-115/paulo-custodio/perl/ch-2.pl43
-rw-r--r--challenge-115/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-115/paulo-custodio/t/test-2.yaml20
-rwxr-xr-xchallenge-115/paulo-custodio/test.pl7
-rw-r--r--challenge-116/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-116/paulo-custodio/perl/ch-2.pl38
-rw-r--r--challenge-116/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-116/paulo-custodio/t/test-2.yaml15
-rwxr-xr-xchallenge-116/paulo-custodio/test.pl7
18 files changed, 369 insertions, 6 deletions
diff --git a/challenge-108/paulo-custodio/perl/ch-1.pl b/challenge-108/paulo-custodio/perl/ch-1.pl
index 333532278a..87e7548d1b 100644..100755
--- a/challenge-108/paulo-custodio/perl/ch-1.pl
+++ b/challenge-108/paulo-custodio/perl/ch-1.pl
@@ -1,11 +1,11 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 108
#
-# TASK #1 › Locate Memory
+# TASK #1 - Locate Memory
# Submitted by: Mohammad S Anwar
#
-# Write a script to declare a variable or constant and print it’s location in
+# Write a script to declare a variable or constant and print it's location in
# the memory.
use Modern::Perl;
diff --git a/challenge-108/paulo-custodio/perl/ch-2.pl b/challenge-108/paulo-custodio/perl/ch-2.pl
index d76209a4e8..c709a3010b 100644..100755
--- a/challenge-108/paulo-custodio/perl/ch-2.pl
+++ b/challenge-108/paulo-custodio/perl/ch-2.pl
@@ -1,8 +1,8 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 108
#
-# TASK #2 › Bell Numbers
+# TASK #2 - Bell Numbers
# Submitted by: Mohammad S Anwar
#
# Write a script to display top 10 Bell Numbers. Please refer to wikipedia page
diff --git a/challenge-108/paulo-custodio/test.pl b/challenge-108/paulo-custodio/test.pl
index 01ed2b83cd..cf1ced98e0 100644..100755
--- a/challenge-108/paulo-custodio/test.pl
+++ b/challenge-108/paulo-custodio/test.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
use strict;
use warnings;
diff --git a/challenge-114/paulo-custodio/perl/ch-1.pl b/challenge-114/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..c543df4a7e
--- /dev/null
+++ b/challenge-114/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+# Challenge 114
+#
+# TASK #1 - Next Palindrome Number
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N.
+#
+# Write a script to find out the next Palindrome Number higher than the given
+# integer $N.
+#
+# Example
+# Input: $N = 1234
+# Output: 1331
+#
+# Input: $N = 999
+# Output: 1001
+
+use Modern::Perl;
+
+my $N = shift || 0;
+say next_palindrome($N);
+
+sub is_palindrome {
+ my($n) = @_;
+ return reverse($n)==$n;
+}
+
+sub next_palindrome {
+ my($n) = @_;
+ while (1) {
+ $n++;
+ return $n if is_palindrome($n);
+ }
+}
diff --git a/challenge-114/paulo-custodio/perl/ch-2.pl b/challenge-114/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..c76fcaf286
--- /dev/null
+++ b/challenge-114/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+# Challenge 114
+#
+# TASK #2 - Higher Integer Set Bits
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N.
+#
+# Write a script to find the next higher integer having the same number of
+# 1 bits in binary representation as $N.
+#
+# Example
+# Input: $N = 3
+# Output: 5
+#
+# Binary representation of $N is 011. There are two 1 bits. So the next higher
+# integer is 5 having the same the number of 1 bits i.e. 101.
+#
+# Input: $N = 12
+# Output: 17
+#
+# Binary representation of $N is 1100. There are two 1 bits. So the next higher
+# integer is 17 having the same number of 1 bits i.e. 10001.
+
+use Modern::Perl;
+
+my $N = shift || 0;
+say next_same_1bits($N);
+
+sub num_1bits {
+ my($n) = @_;
+ my $sum = 0;
+ map {$sum += $_} split //, sprintf("%b", $n);
+ return $sum;
+}
+
+sub next_same_1bits {
+ my($n) = @_;
+ my $num1s = num_1bits($n);
+ while (1) {
+ $n++;
+ return $n if num_1bits($n) == $num1s;
+ }
+}
diff --git a/challenge-114/paulo-custodio/t/test-1.yaml b/challenge-114/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..8a671155df
--- /dev/null
+++ b/challenge-114/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1234
+ input:
+ output: 1331
+- setup:
+ cleanup:
+ args: 999
+ input:
+ output: 1001
diff --git a/challenge-114/paulo-custodio/t/test-2.yaml b/challenge-114/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..5f1d95cf31
--- /dev/null
+++ b/challenge-114/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 5
+- setup:
+ cleanup:
+ args: 12
+ input:
+ output: 17
diff --git a/challenge-114/paulo-custodio/test.pl b/challenge-114/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-114/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-115/paulo-custodio/perl/ch-1.pl b/challenge-115/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..6d28fbd2d6
--- /dev/null
+++ b/challenge-115/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# Challenge 115
+#
+# TASK #1 - String Chain
+# Submitted by: Mohammad S Anwar
+# You are given an array of strings.
+#
+# Write a script to find out if the given strings can be chained to form a
+# circle. Print 1 if found otherwise 0.
+#
+# A string $S can be put before another string $T in circle if the last
+# character of $S is same as first character of $T.
+#
+# Examples:
+# Input: @S = ("abc", "dea", "cd")
+# Output: 1 as we can form circle e.g. "abc", "cd", "dea".
+#
+# Input: @S = ("ade", "cbd", "fgh")
+# Output: 0 as we can't form circle.
+
+use Modern::Perl;
+
+my @words = @ARGV;
+@words or die "Usage: ch-1.pl words...\n";
+
+say is_circle(\@words) ? 1 : 0;
+
+sub is_circle {
+ my($pending, @words) = @_;
+ my $found_solution;
+
+ if (@$pending == 0) {
+ $found_solution ||= substr($words[-1],-1,1) eq substr($words[0],0,1);
+ }
+ else {
+ for my $word (@$pending) {
+ if (@words == 0 ||
+ substr($words[-1],-1,1) eq substr($word,0,1)) {
+ my @new_pending = grep {$_ ne $word} @$pending;
+ $found_solution ||= is_circle(\@new_pending, @words, $word);
+ }
+ }
+ }
+ return $found_solution;
+}
diff --git a/challenge-115/paulo-custodio/perl/ch-2.pl b/challenge-115/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..afb6efc0ab
--- /dev/null
+++ b/challenge-115/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# Challenge 115
+#
+# TASK #2 - Largest Multiple
+# Submitted by: Mohammad S Anwar
+# You are given a list of positive integers (0-9), single digit.
+#
+# Write a script to find the largest multiple of 2 that can be formed from the
+# list.
+#
+# Examples
+# Input: @N = (1, 0, 2, 6)
+# Output: 6210
+#
+# Input: @N = (1, 4, 2, 8)
+# Output: 8412
+#
+# Input: @N = (4, 1, 7, 6)
+# Output: 7614
+
+use Modern::Perl;
+
+my @nums = @ARGV;
+@nums or die "Usage: ch-1.pl words...\n";
+say largest_mult2(@nums);
+
+sub largest_mult2 {
+ my(@nums) = @_;
+
+ # select smallest even number for last element
+ my @even = sort {$a->[1] <=> $b->[1]}
+ grep {$_->[1] % 2 == 0}
+ map {[$_, $nums[$_]]} 0..$#nums;
+ return 0 if !@even; # no even numbers
+ my($index, $last) = @{$even[0]};
+ splice(@nums, $index, 1); # remove it from @nums
+
+ # sort the other elements in descending order
+ @nums = sort {$b <=> $a} @nums;
+
+ return 0+join('',@nums,$last);
+}
diff --git a/challenge-115/paulo-custodio/t/test-1.yaml b/challenge-115/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..017eb0af04
--- /dev/null
+++ b/challenge-115/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: abc dea cd
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: ade cbd fgh
+ input:
+ output: 0
diff --git a/challenge-115/paulo-custodio/t/test-2.yaml b/challenge-115/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..6acb5ad25d
--- /dev/null
+++ b/challenge-115/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 0 2 6
+ input:
+ output: 6210
+- setup:
+ cleanup:
+ args: 1 4 2 8
+ input:
+ output: 8412
+- setup:
+ cleanup:
+ args: 4 1 7 6
+ input:
+ output: 7614
+- setup:
+ cleanup:
+ args: 1 3 5 7
+ input:
+ output: 0
diff --git a/challenge-115/paulo-custodio/test.pl b/challenge-115/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-115/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-116/paulo-custodio/perl/ch-1.pl b/challenge-116/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..ba664c35a3
--- /dev/null
+++ b/challenge-116/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+# Challenge 116
+#
+# TASK #1 - Number Sequence
+# Submitted by: Mohammad S Anwar
+# You are given a number $N >= 10.
+#
+# Write a script to split the given number such that the difference between two
+# consecutive numbers is always 1 and it shouldn't have leading 0.
+#
+# Print the given number if it impossible to split the number.
+#
+# Example
+# Input: $N = 1234
+# Output: 1,2,3,4
+#
+# Input: $N = 91011
+# Output: 9,10,11
+#
+# Input: $N = 10203
+# Output: 10203 as it is impossible to split satisfying the conditions.
+
+use Modern::Perl;
+
+my $N = shift;
+
+print_sequences($N);
+
+sub print_sequences {
+ my($rest, @prev) = @_;
+ my $found_solution = 0;
+
+ if ($rest eq '') {
+ if (!$found_solution) {
+ say join(",", @prev);
+ $found_solution = 1;
+ }
+ }
+ else {
+ for (1 .. length($rest)) {
+ my $pref = substr($rest,0,$_);
+ my $suff = substr($rest,$_);
+ next if $suff =~ /^0/;
+ if (@prev) {
+ if ($prev[-1] + 1 == $pref) {
+ $found_solution ||= print_sequences($suff, @prev, $pref);
+ }
+ }
+ else {
+ $found_solution ||= print_sequences($suff, @prev, $pref);
+ }
+ }
+ }
+ return $found_solution;
+}
diff --git a/challenge-116/paulo-custodio/perl/ch-2.pl b/challenge-116/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..a84fe54eec
--- /dev/null
+++ b/challenge-116/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+# Challenge 116
+#
+# TASK #2 - Sum of Squares
+# Submitted by: Mohammad Meraj Zia
+# You are given a number $N >= 10.
+#
+# Write a script to find out if the given number $N is such that sum of squares
+# of all digits is a perfect square. Print 1 if it is otherwise 0.
+#
+# Example
+# Input: $N = 34
+# Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2
+#
+# Input: $N = 50
+# Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2
+#
+# Input: $N = 52
+# Output: 0 as 5^2 + 2^2 => 25 + 4 => 29
+
+use Modern::Perl;
+
+my $N = shift || 0;
+say sum_of_squares_is_perfect_square($N) ? 1 : 0;
+
+sub sum_of_squares_is_perfect_square {
+ my($num) = @_;
+ return 0 if $num < 10;
+ my $sum = 0;
+ for my $digit (split(//, $num)) {
+ $sum += $digit*$digit;
+ }
+ my $sqrt_int = int(sqrt($sum));
+ return $sqrt_int*$sqrt_int==$sum;
+}
+
+
diff --git a/challenge-116/paulo-custodio/t/test-1.yaml b/challenge-116/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..bbf1ea8802
--- /dev/null
+++ b/challenge-116/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1234
+ input:
+ output: 1,2,3,4
+- setup:
+ cleanup:
+ args: 91011
+ input:
+ output: 9,10,11
+- setup:
+ cleanup:
+ args: 10203
+ input:
+ output: 10203
diff --git a/challenge-116/paulo-custodio/t/test-2.yaml b/challenge-116/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..c6fd04f152
--- /dev/null
+++ b/challenge-116/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 34
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 50
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 52
+ input:
+ output: 0
diff --git a/challenge-116/paulo-custodio/test.pl b/challenge-116/paulo-custodio/test.pl
new file mode 100755
index 0000000000..cf1ced98e0
--- /dev/null
+++ b/challenge-116/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';