aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-25 15:36:47 +0100
committerGitHub <noreply@github.com>2024-08-25 15:36:47 +0100
commit82a6118b99ab04012965c620561cb5a5b2b448eb (patch)
treed331d1cb97129e2c531336b138868ee31da1e184
parent8baafd72a0450662ccc19e695ee80ab7a919bc08 (diff)
parentb3e0f7ee3d115439ec90862917604706aa50032d (diff)
downloadperlweeklychallenge-club-82a6118b99ab04012965c620561cb5a5b2b448eb.tar.gz
perlweeklychallenge-club-82a6118b99ab04012965c620561cb5a5b2b448eb.tar.bz2
perlweeklychallenge-club-82a6118b99ab04012965c620561cb5a5b2b448eb.zip
Merge pull request #10693 from pauloscustodio/master
Add Perl solution to challenge 254
-rw-r--r--challenge-254/paulo-custodio/Makefile2
-rw-r--r--challenge-254/paulo-custodio/perl/ch-1.pl31
-rw-r--r--challenge-254/paulo-custodio/perl/ch-2.pl31
-rw-r--r--challenge-254/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-254/paulo-custodio/t/test-2.yaml20
-rw-r--r--challenge-255/paulo-custodio/Makefile2
-rw-r--r--challenge-255/paulo-custodio/perl/ch-1.pl28
-rw-r--r--challenge-255/paulo-custodio/perl/ch-2.pl36
-rw-r--r--challenge-255/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-255/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-256/paulo-custodio/Makefile2
-rw-r--r--challenge-256/paulo-custodio/perl/ch-1.pl39
-rw-r--r--challenge-256/paulo-custodio/perl/ch-2.pl30
-rw-r--r--challenge-256/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-256/paulo-custodio/t/test-2.yaml15
-rw-r--r--challenge-257/paulo-custodio/Makefile2
-rw-r--r--challenge-257/paulo-custodio/perl/ch-1.pl37
-rw-r--r--challenge-257/paulo-custodio/t/test-1.yaml20
18 files changed, 350 insertions, 0 deletions
diff --git a/challenge-254/paulo-custodio/Makefile b/challenge-254/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-254/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-254/paulo-custodio/perl/ch-1.pl b/challenge-254/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..6789b429b5
--- /dev/null
+++ b/challenge-254/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+# Challenge 254
+#
+# Task 1: Three Power
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer, $n.
+#
+# Write a script to return true if the given integer is a power of three
+# otherwise return false.
+#
+# Example 1
+# Input: $n = 27
+# Output: true
+#
+# 27 = 3 ^ 3
+# Example 2
+# Input: $n = 0
+# Output: true
+#
+# 0 = 0 ^ 3
+# Example 3
+# Input: $n = 6
+# Output: false
+
+use Modern::Perl;
+
+my $n = shift || 0;
+my $root = $n ** (1/3);
+my $have_root = (int($root) == $root);
+say $have_root ? 'true' : 'false';
diff --git a/challenge-254/paulo-custodio/perl/ch-2.pl b/challenge-254/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..8637ffc586
--- /dev/null
+++ b/challenge-254/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+# Challenge 254
+#
+# Task 2: Reverse Vowels
+# Submitted by: Mohammad S Anwar
+# You are given a string, $s.
+#
+# Write a script to reverse all the vowels (a, e, i, o, u) in the given string.
+#
+# Example 1
+# Input: $s = "Raku"
+# Output: "Ruka"
+# Example 2
+# Input: $s = "Perl"
+# Output: "Perl"
+# Example 3
+# Input: $s = "Julia"
+# Output: "Jaliu"
+# Example 4
+# Input: $s = "Uiua"
+# Output: "Auiu"
+
+use Modern::Perl;
+
+my $word = shift || "";
+my @vowels;
+$word =~ s/[aeiou]/ push @vowels, $&; $& /gei;
+$word =~ s/[aeiou]/ pop @vowels /gei;
+$word =~ s/(.)(.*)/ uc($1).lc($2) /e;
+say $word;
diff --git a/challenge-254/paulo-custodio/t/test-1.yaml b/challenge-254/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..19b49b312f
--- /dev/null
+++ b/challenge-254/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 27
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: 0
+ input:
+ output: true
+- setup:
+ cleanup:
+ args: 6
+ input:
+ output: false
diff --git a/challenge-254/paulo-custodio/t/test-2.yaml b/challenge-254/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..91791a52fd
--- /dev/null
+++ b/challenge-254/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: Raku
+ input:
+ output: Ruka
+- setup:
+ cleanup:
+ args: Perl
+ input:
+ output: Perl
+- setup:
+ cleanup:
+ args: Julia
+ input:
+ output: Jaliu
+- setup:
+ cleanup:
+ args: Uiua
+ input:
+ output: Auiu
diff --git a/challenge-255/paulo-custodio/Makefile b/challenge-255/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-255/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-255/paulo-custodio/perl/ch-1.pl b/challenge-255/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..edf0c5bb5e
--- /dev/null
+++ b/challenge-255/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+# Challenge 255
+#
+# Task 1: Odd Character
+# Submitted by: Mohammad Sajid Anwar
+# You are given two strings, $s and $t. The string $t is generated using the
+# shuffled characters of the string $s with an additional character.
+#
+# Write a script to find the additional character in the string $t..
+#
+# Example 1
+# Input: $s = "Perl" $t = "Preel"
+# Output: "e"
+# Example 2
+# Input: $s = "Weekly" $t = "Weeakly"
+# Output: "a"
+# Example 3
+# Input: $s = "Box" $t = "Boxy"
+# Output: "y"
+
+use Modern::Perl;
+
+my($word, $shuffled) = @ARGV;
+for my $ch (split //, $word) {
+ $shuffled =~ s/$ch//i;
+}
+say $shuffled;
diff --git a/challenge-255/paulo-custodio/perl/ch-2.pl b/challenge-255/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..5f123273fd
--- /dev/null
+++ b/challenge-255/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 255
+#
+# Task 2: Most Frequent Word
+# Submitted by: Mohammad Sajid Anwar
+# You are given a paragraph $p and a banned word $w.
+#
+# Write a script to return the most frequent word that is not banned.
+#
+# Example 1
+# Input: $p = "Joe hit a ball, the hit ball flew far after it was hit."
+# $w = "hit"
+# Output: "ball"
+#
+# The banned word "hit" occurs 3 times.
+# The other word "ball" occurs 2 times.
+# Example 2
+# Input: $p = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."
+# $w = "the"
+# Output: "Perl"
+#
+# The banned word "the" occurs 3 times.
+# The other word "Perl" occurs 2 times.
+
+use Modern::Perl;
+
+my($banned, @paragraph) = @ARGV;
+my %count;
+my @words =
+ map { $_->[0] }
+ sort { $b->[1] <=> $a->[1] }
+ map { [$_, $count{$_}++] }
+ grep { lc($banned) ne lc($_) }
+ split /\W/, "@paragraph";
+say $words[0];
diff --git a/challenge-255/paulo-custodio/t/test-1.yaml b/challenge-255/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..86c04b3e79
--- /dev/null
+++ b/challenge-255/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: Perl Preel
+ input:
+ output: e
+- setup:
+ cleanup:
+ args: Weekly Weeakly
+ input:
+ output: a
+- setup:
+ cleanup:
+ args: Box Boxy
+ input:
+ output: y
diff --git a/challenge-255/paulo-custodio/t/test-2.yaml b/challenge-255/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..fb73e3f567
--- /dev/null
+++ b/challenge-255/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: hit Joe hit a ball, the hit ball flew far after it was hit.
+ input:
+ output: ball
+- setup:
+ cleanup:
+ args: the Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.
+ input:
+ output: Perl
diff --git a/challenge-256/paulo-custodio/Makefile b/challenge-256/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-256/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-256/paulo-custodio/perl/ch-1.pl b/challenge-256/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d2a0fef0f2
--- /dev/null
+++ b/challenge-256/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+# Challenge 256
+#
+# Task 1: Maximum Pairs
+# Submitted by: Mohammad Sajid Anwar
+# You are given an array of distinct words, @words.
+#
+# Write a script to find the maximum pairs in the given array. The words
+# $words[i] and $words[j] can be a pair one is reverse of the other.
+#
+# Example 1
+# Input: @words = ("ab", "de", "ed", "bc")
+# Output: 1
+#
+# There is one pair in the given array: "de" and "ed"
+# Example 2
+# Input: @words = ("aa", "ba", "cd", "ed")
+# Output: 0
+# Example 3
+# Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")
+# Output: 2
+
+use Modern::Perl;
+
+my @words = @ARGV;
+my $count = 0;
+for my $i (0 .. $#words-1) {
+ for my $j ($i+1 .. $#words) {
+ $count++ if is_pair($words[$i], $words[$j]);
+ }
+}
+say $count;
+
+
+sub is_pair {
+ my($a, $b) = @_;
+ return $a eq join '', reverse split //, $b;
+}
diff --git a/challenge-256/paulo-custodio/perl/ch-2.pl b/challenge-256/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..b564539172
--- /dev/null
+++ b/challenge-256/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+# Challenge 256
+#
+# Task 2: Merge Strings
+# Submitted by: Mohammad Sajid Anwar
+# You are given two strings, $str1 and $str2.
+#
+# Write a script to merge the given strings by adding in alternative order
+# starting with the first string. If a string is longer than the other then
+# append the remaining at the end.
+#
+# Example 1
+# Input: $str1 = "abcd", $str2 = "1234"
+# Output: "a1b2c3d4"
+# Example 2
+# Input: $str1 = "abc", $str2 = "12345"
+# Output: "a1b2c345"
+# Example 3
+# Input: $str1 = "abcde", $str2 = "123"
+# Output: "a1b2c3de"
+
+use Modern::Perl;
+use List::Util 'zip';
+
+my($a, $b) = @ARGV;
+my @a = split //, $a;
+my @b = split //, $b;
+my @merge = map {($_->[0]//'').($_->[1]//'')} zip \@a, \@b;
+say join '', @merge;
diff --git a/challenge-256/paulo-custodio/t/test-1.yaml b/challenge-256/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..c921aeb3a7
--- /dev/null
+++ b/challenge-256/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: ab de ed bc
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: aa ba cd ed
+ input:
+ output: 0
+- setup:
+ cleanup:
+ args: uv qp st vu mn pq
+ input:
+ output: 2
diff --git a/challenge-256/paulo-custodio/t/test-2.yaml b/challenge-256/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..8e36fce9c2
--- /dev/null
+++ b/challenge-256/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: abcd 1234
+ input:
+ output: a1b2c3d4
+- setup:
+ cleanup:
+ args: abc 12345
+ input:
+ output: a1b2c345
+- setup:
+ cleanup:
+ args: abcde 123
+ input:
+ output: a1b2c3de
diff --git a/challenge-257/paulo-custodio/Makefile b/challenge-257/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-257/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-257/paulo-custodio/perl/ch-1.pl b/challenge-257/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..ad81d3ccc3
--- /dev/null
+++ b/challenge-257/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+# Challenge 257
+#
+# Task 1: Smaller than Current
+# Submitted by: Mohammad Sajid Anwar
+# You are given a array of integers, @ints.
+#
+# Write a script to find out how many integers are smaller than current i.e.
+# foreach ints[i], count ints[j] < ints[i] where i != j.
+#
+# Example 1
+# Input: @ints = (5, 2, 1, 6)
+# Output: (2, 1, 0, 3)
+#
+# For $ints[0] = 5, there are two integers (2,1) smaller than 5.
+# For $ints[1] = 2, there is one integer (1) smaller than 2.
+# For $ints[2] = 1, there is none integer smaller than 1.
+# For $ints[3] = 6, there are three integers (5,2,1) smaller than 6.
+# Example 2
+# Input: @ints = (1, 2, 0, 3)
+# Output: (1, 2, 0, 3)
+# Example 3
+# Input: @ints = (0, 1)
+# Output: (0, 1)
+# Example 4
+# Input: @ints = (9, 4, 9, 2)
+# Output: (2, 1, 2, 0)
+
+use Modern::Perl;
+
+my(@ints) = @ARGV;
+my @smaller;
+for my $i (0..$#ints) {
+ push @smaller, scalar grep {$ints[$_] < $ints[$i]} 0..$#ints;
+}
+say "@smaller";
diff --git a/challenge-257/paulo-custodio/t/test-1.yaml b/challenge-257/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..18304767e8
--- /dev/null
+++ b/challenge-257/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 5 2 1 6
+ input:
+ output: 2 1 0 3
+- setup:
+ cleanup:
+ args: 1 2 0 3
+ input:
+ output: 1 2 0 3
+- setup:
+ cleanup:
+ args: 0 1
+ input:
+ output: 0 1
+- setup:
+ cleanup:
+ args: 9 4 9 2
+ input:
+ output: 2 1 2 0