aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-13 10:36:19 +0000
committerGitHub <noreply@github.com>2024-02-13 10:36:19 +0000
commitfc5efac65802f0563e25a56a12f28f346386b294 (patch)
tree163e24a25db511f97502eae9983c783d02eec13d
parent3dcd566e3a5ad4be358164295951dbc6c1cf25f1 (diff)
parent925bd0f4015a78d20f4cf8d02d6e713800068466 (diff)
downloadperlweeklychallenge-club-fc5efac65802f0563e25a56a12f28f346386b294.tar.gz
perlweeklychallenge-club-fc5efac65802f0563e25a56a12f28f346386b294.tar.bz2
perlweeklychallenge-club-fc5efac65802f0563e25a56a12f28f346386b294.zip
Merge pull request #9581 from spadacciniweb/PWC-256
Add PWC-256 in Perl
-rw-r--r--challenge-256/spadacciniweb/perl/ch-1.pl49
-rw-r--r--challenge-256/spadacciniweb/perl/ch-2.pl57
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-256/spadacciniweb/perl/ch-1.pl b/challenge-256/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..b736a2d427
--- /dev/null
+++ b/challenge-256/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+# 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 strict;
+use warnings;
+
+my @words = ("ab", "de", "ed", "bc");
+maximum_pairs(\@words);
+
+@words = ("aa", "ba", "cd", "ed");
+maximum_pairs(\@words);
+
+@words = ("uv", "qp", "st", "vu", "mn", "pq");
+maximum_pairs(\@words);
+
+exit 0;
+
+sub maximum_pairs {
+ my $words = shift || [];
+
+ my %freq;
+ foreach my $w (@$words) {
+ $freq{$w} = $freq{(scalar reverse $w)}
+ ? 2
+ : 1;
+ }
+ printf "%s -> %d\n", (join ' ', @$words ), scalar map { $freq{$_} == 2 ? 1 : () } keys %freq;
+
+ return undef;
+}
diff --git a/challenge-256/spadacciniweb/perl/ch-2.pl b/challenge-256/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..2e56a61afb
--- /dev/null
+++ b/challenge-256/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+
+# 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 strict;
+use warnings;
+use List::Util qw(min);
+
+my $str1 = "abcd";
+my $str2 = "1234";
+merge_strings($str1, $str2);
+
+$str1 = "abc";
+$str2 = "12345";
+merge_strings($str1, $str2);
+
+$str1 = "abcde";
+$str2 = "123";
+merge_strings($str1, $str2);
+
+exit 0;
+
+sub merge_strings {
+ my $str1 = shift || '';
+ my $str2 = shift || '';
+
+ my $s = '';
+ my $length = min(length($str1), length($str2) );
+ foreach my $i (0..$length) {
+ $s .= substr $str1, $i, 1;
+ $s .= substr $str2, $i, 1;
+ }
+ if (length($str1) > $length) {
+ $s .= substr $str1, $length+1;
+ } elsif (length($str2) > $length) {
+ $s .= substr $str2, $length+1;
+ }
+ printf "%s | %s -> %s\n", $str1, $str2, $s;
+
+ return undef;
+}