aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-17 22:18:06 +0000
committerGitHub <noreply@github.com>2024-01-17 22:18:06 +0000
commitea10cb74a2ed5a0f5a5330378fff1a0aaa60237a (patch)
tree20d27f6fdae2907d12d498898b17c3477e70ca81
parentcbcd672cc514ac25bcc6266061b83072aae913af (diff)
parent275b39e13dc81f9d8ad0cfff0731e6d69c57cf7d (diff)
downloadperlweeklychallenge-club-ea10cb74a2ed5a0f5a5330378fff1a0aaa60237a.tar.gz
perlweeklychallenge-club-ea10cb74a2ed5a0f5a5330378fff1a0aaa60237a.tar.bz2
perlweeklychallenge-club-ea10cb74a2ed5a0f5a5330378fff1a0aaa60237a.zip
Merge pull request #9421 from pauloscustodio/master
Add Perl solution
-rw-r--r--challenge-248/paulo-custodio/Makefile2
-rw-r--r--challenge-248/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-248/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-249/paulo-custodio/Makefile2
-rw-r--r--challenge-249/paulo-custodio/perl/ch-1.pl68
-rw-r--r--challenge-249/paulo-custodio/perl/ch-2.pl63
-rw-r--r--challenge-249/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-249/paulo-custodio/t/test-2.yaml15
8 files changed, 226 insertions, 0 deletions
diff --git a/challenge-248/paulo-custodio/Makefile b/challenge-248/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-248/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-248/paulo-custodio/perl/ch-1.pl b/challenge-248/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..503126e926
--- /dev/null
+++ b/challenge-248/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+# Challenge 248
+#
+# Task 1: Shortest Distance
+# Submitted by: Mohammad S Anwar
+#
+# You are given a string and a character in the given string.
+#
+# Write a script to return an array of integers of size same as length of the given string such that:
+#
+# distance[i] is the distance from index i to the closest occurence of
+# the given character in the given string.
+#
+# The distance between two indices i and j is abs(i - j).
+#
+# Example 1
+#
+# Input: $str = "loveleetcode", $char = "e"
+# Output: (3,2,1,0,1,0,0,1,2,2,1,0)
+#
+# The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
+# The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
+# The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
+# For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5,
+# but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
+# The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
+#
+# Example 2
+#
+# Input: $str = "aaab", $char = "b"
+# Output: (3,2,1,0)
+
+use Modern::Perl;
+use List::Util 'min';
+
+my @dists = calc_dists(@ARGV);
+say "(", join(",", @dists), ")";
+
+sub calc_dists {
+ my($str, $char) = @_;
+ my @str = split //, $str;
+ my @dist = ((1e10) x @str);
+ for my $i (0 .. $#str) {
+ if ($str[$i] eq $char) {
+ $dist[$i] = 0;
+ for my $j (1 .. $i) {
+ $dist[$i-$j] = min($dist[$i-$j], $j);
+ }
+ for my $j (1 .. $#str-$i) {
+ $dist[$i+$j] = min($dist[$i+$j], $j);
+ }
+ }
+ }
+ return @dist;
+}
diff --git a/challenge-248/paulo-custodio/t/test-1.yaml b/challenge-248/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..89db32f4c2
--- /dev/null
+++ b/challenge-248/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: loveleetcode e
+ input:
+ output: (3,2,1,0,1,0,0,1,2,2,1,0)
+- setup:
+ cleanup:
+ args: aaab b
+ input:
+ output: (3,2,1,0)
diff --git a/challenge-249/paulo-custodio/Makefile b/challenge-249/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-249/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-249/paulo-custodio/perl/ch-1.pl b/challenge-249/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..54f7c9c593
--- /dev/null
+++ b/challenge-249/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/env perl
+
+# Challenge 249
+#
+# Task 1: Equal Pairs
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers with even number of elements.
+#
+# Write a script to divide the given array into equal pairs such that:
+#
+# a) Each element belongs to exactly one pair.
+# b) The elements present in a pair are equal.
+#
+#
+# Example 1
+#
+# Input: @ints = (3, 2, 3, 2, 2, 2)
+# Output: (2, 2), (3, 3), (2, 2)
+#
+# There are 6 elements in @ints.
+# They should be divided into 6 / 2 = 3 pairs.
+# @ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the conditions.
+#
+# Example 2
+#
+# Input: @ints = (1, 2, 3, 4)
+# Output: ()
+#
+# There is no way to divide @ints 2 pairs such that the pairs satisfy every condition.
+
+use Modern::Perl;
+
+my @pairs = make_pairs(@ARGV);
+print_pairs(@pairs);
+
+sub make_pairs {
+ my(@ints) = @_;
+ my @pairs;
+ if (@ints == 0 || (@ints % 2) == 1) {
+ return ();
+ }
+pair:
+ while (@ints) {
+ my $a = 0;
+ for my $b (1 .. $#ints) {
+ if ($ints[$a] == $ints[$b]) {
+ push @pairs, [$ints[$a], $ints[$b]];
+ splice @ints, $b, 1;
+ shift @ints;
+ next pair;
+ }
+ }
+ return ();
+ }
+ return @pairs;
+}
+
+sub print_pairs {
+ my(@pairs) = @_;
+ if (@pairs) {
+ say join ", ", map {"(".$_->[0].", ".$_->[1].")"} @pairs;
+ }
+ else {
+ say "()";
+ }
+
+}
diff --git a/challenge-249/paulo-custodio/perl/ch-2.pl b/challenge-249/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..c8a710804a
--- /dev/null
+++ b/challenge-249/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+
+# Challenge 249
+#
+# Task 2: DI String Match
+# Submitted by: Mohammad S Anwar
+#
+# You are given a string s, consisting of only the characters "D" and "I".
+#
+# Find a permutation of the integers [0 .. length(s)] such that for each character s[i] in the string:
+#
+# s[i] == 'I' ⇒ perm[i] < perm[i + 1]
+# s[i] == 'D' ⇒ perm[i] > perm[i + 1]
+#
+#
+# Example 1
+#
+# Input: $str = "IDID"
+# Output: (0, 4, 1, 3, 2)
+#
+# Example 2
+#
+# Input: $str = "III"
+# Output: (0, 1, 2, 3)
+#
+# Example 3
+#
+# Input: $str = "DDI"
+# Output: (3, 2, 0, 1)
+
+use Modern::Perl;
+use Math::Combinatorics;
+
+my @perm = find_permutation(@ARGV);
+say "(", join(", ", @perm), ")";
+
+sub find_permutation {
+ my($s) = @_;
+ my @s = split //, $s;
+ my @n = (0 .. length($s));
+ my $combinat = Math::Combinatorics->new(count => scalar(@n), data => \@n);
+ my @permu;
+ while (@permu = $combinat->next_permutation) {
+ if (check_permu(\@s, \@permu)) {
+ return @permu;
+ }
+ }
+ return ();
+}
+
+sub check_permu {
+ my($s, $permu) = @_;
+ my @s = @$s;
+ my @permu = @$permu;
+
+ for my $i (0 .. $#permu-1) {
+ if (!($s[$i] eq 'I' && $permu[$i] < $permu[$i+1] ||
+ $s[$i] eq 'D' && $permu[$i] > $permu[$i+1])) {
+ return 0;
+ }
+ }
+ return 1;
+}
diff --git a/challenge-249/paulo-custodio/t/test-1.yaml b/challenge-249/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..93a44ba0ed
--- /dev/null
+++ b/challenge-249/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 3 2 3 2 2 2
+ input:
+ output: (3, 3), (2, 2), (2, 2)
+- setup:
+ cleanup:
+ args: 1 2 3 4
+ input:
+ output: ()
diff --git a/challenge-249/paulo-custodio/t/test-2.yaml b/challenge-249/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..5c50228af8
--- /dev/null
+++ b/challenge-249/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: IDID
+ input:
+ output: (0, 2, 1, 4, 3)
+- setup:
+ cleanup:
+ args: III
+ input:
+ output: (0, 1, 2, 3)
+- setup:
+ cleanup:
+ args: DDI
+ input:
+ output: (2, 1, 0, 3)