From 2b13942fb565b3ef6419c6b2b43ea4fc8a7019ad Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 17 Jan 2024 14:18:13 +0000 Subject: Add Perl solution --- challenge-249/paulo-custodio/Makefile | 2 + challenge-249/paulo-custodio/perl/ch-1.pl | 68 ++++++++++++++++++++++++++++++ challenge-249/paulo-custodio/perl/ch-2.pl | 63 +++++++++++++++++++++++++++ challenge-249/paulo-custodio/t/test-1.yaml | 10 +++++ challenge-249/paulo-custodio/t/test-2.yaml | 15 +++++++ 5 files changed, 158 insertions(+) create mode 100644 challenge-249/paulo-custodio/Makefile create mode 100644 challenge-249/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-249/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-249/paulo-custodio/t/test-1.yaml create mode 100644 challenge-249/paulo-custodio/t/test-2.yaml 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) -- cgit From 275b39e13dc81f9d8ad0cfff0731e6d69c57cf7d Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 17 Jan 2024 16:28:24 +0000 Subject: Add Perl solution --- challenge-248/paulo-custodio/Makefile | 2 ++ challenge-248/paulo-custodio/perl/ch-1.pl | 56 ++++++++++++++++++++++++++++++ challenge-248/paulo-custodio/t/test-1.yaml | 10 ++++++ 3 files changed, 68 insertions(+) create mode 100644 challenge-248/paulo-custodio/Makefile create mode 100644 challenge-248/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-248/paulo-custodio/t/test-1.yaml 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) -- cgit