diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-09 10:50:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 10:50:05 +0100 |
| commit | f379ca7e2858abaed96aed3e2b0066e8df0152b0 (patch) | |
| tree | f79e1e42e1b63d1602070d8b145860aa6c448fc2 | |
| parent | 0cd22fef3ec226bb7ee56c0d4a4d82e0c0b5d792 (diff) | |
| parent | 71aadeda028ce76a2a0915e571d7f03dc43b7e21 (diff) | |
| download | perlweeklychallenge-club-f379ca7e2858abaed96aed3e2b0066e8df0152b0.tar.gz perlweeklychallenge-club-f379ca7e2858abaed96aed3e2b0066e8df0152b0.tar.bz2 perlweeklychallenge-club-f379ca7e2858abaed96aed3e2b0066e8df0152b0.zip | |
Merge pull request #9895 from pme/challenge-264
challenge-264
| -rwxr-xr-x | challenge-264/peter-meszaros/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-264/peter-meszaros/perl/ch-2.pl | 68 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-264/peter-meszaros/perl/ch-1.pl b/challenge-264/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..b72c185072 --- /dev/null +++ b/challenge-264/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# +# You are given a string, $str, made up of only alphabetic characters +# [a..zA..Z]. +# +# Write a script to return the greatest english letter in the given string. +# +# A letter is greatest if it occurs as lower and upper case. Also letter +# ‘b’ is greater than ‘a’ if ‘b’ appears after ‘a’ in the English alphabet. +# +# Example 1 +# +# Input: $str = 'PeRlwEeKLy' +# Output: L +# +# There are two letters E and L that appears as lower and upper. +# The letter L appears after E, so the L is the greatest english letter. +# +# Example 2 +# +# Input: $str = 'ChaLlenge' +# Output: L +# Example 3 +# +# Input: $str = 'The' +# Output: '' +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ['PeRlwEeKLy', 'L', 'Example 1'], + ['ChaLlenge', 'L', 'Example 2'], + ['The', '', 'Example 3'], +]; + +sub greatest_english_letter +{ + my $w = shift; + + my %h; + for my $l (split(//, $w)) { + my $ul = uc $l; + $h{$ul} |= (($l eq $ul) ? 1 : 2); + } + + return (sort grep {$h{$_} == 3} keys %h)[-1] // ''; +} + +for (@$cases) { + is(greatest_english_letter($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-264/peter-meszaros/perl/ch-2.pl b/challenge-264/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..ae0050a4e6 --- /dev/null +++ b/challenge-264/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +# +# You are given two arrays of integers, @source and @indices. The @indices can +# only contains integers 0 <= i < size of @source. +# +# Write a script to create target array by insert at index $indices[i] the +# value $source[i]. +# Example 1 +# +# Input: @source = (0, 1, 2, 3, 4) +# @indices = (0, 1, 2, 2, 1) +# Output: (0, 4, 1, 3, 2) +# +# @source @indices @target +# 0 0 (0) +# 1 1 (0, 1) +# 2 2 (0, 1, 2) +# 3 2 (0, 1, 3, 2) +# 4 1 (0, 4, 1, 3, 2) +# +# Example 2 +# Input: @source = (1, 2, 3, 4, 0) +# @indices = (0, 1, 2, 3, 0) +# Output: (0, 1, 2, 3, 4) +# +# @source @indices @target +# 1 0 (1) +# 2 1 (1, 2) +# 3 2 (1, 2, 3) +# 4 3 (1, 2, 3, 4) +# 0 0 (0, 1, 2, 3, 4) +# +# Example 3 +# +# Input: @source = (1) +# @indices = (0) +# Output: (1) +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[0, 1, 2, 3, 4], [0, 1, 2, 2, 1]], [0, 4, 1, 3, 2], 'Example 1'], + [[[1, 2, 3, 4, 0], [0, 1, 2, 3, 0]], [0, 1, 2, 3, 4], 'Example 2'], + [[[1], [0]], [1], 'Example 3'], +]; + +sub target_array +{ + my $sources = $_[0]->[0]; + my $indices = $_[0]->[1]; + + my @res; + splice @res, $indices->[$_], 0, $sources->[$_] for 0..$#$sources; + + return \@res; +} + +for (@$cases) { + is(target_array($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
