diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-01 14:09:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-01 14:09:00 +0000 |
| commit | cb3a2233706bd3af92d2759c55e569b6881dfaf5 (patch) | |
| tree | e6a675e6ff909aeba6b51d78235279ed9d6429e1 | |
| parent | 7c4575dde87a3440d4cde84d5f6336b8e228a9c7 (diff) | |
| parent | b51672021a559bcdd43e0f7fa15184bae31dbb44 (diff) | |
| download | perlweeklychallenge-club-cb3a2233706bd3af92d2759c55e569b6881dfaf5.tar.gz perlweeklychallenge-club-cb3a2233706bd3af92d2759c55e569b6881dfaf5.tar.bz2 perlweeklychallenge-club-cb3a2233706bd3af92d2759c55e569b6881dfaf5.zip | |
Merge pull request #9328 from pme/challenge-250
challenge-250
| -rwxr-xr-x | challenge-250/peter-meszaros/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-250/peter-meszaros/perl/ch-2.pl | 55 |
2 files changed, 117 insertions, 0 deletions
diff --git a/challenge-250/peter-meszaros/perl/ch-1.pl b/challenge-250/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..9c5eda5308 --- /dev/null +++ b/challenge-250/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints. +# +# Write a script to find the smallest index i such that i mod 10 == $ints[i] +# otherwise return -1. +# Example 1 +# +# Input: @ints = (0, 1, 2) +# Output: 0 +# +# i=0: 0 mod 10 = 0 == $ints[0]. +# i=1: 1 mod 10 = 1 == $ints[1]. +# i=2: 2 mod 10 = 2 == $ints[2]. +# All indices have i mod 10 == $ints[i], so we return the smallest index 0. +# +# Example 2 +# +# Input: @ints = (4, 3, 2, 1) +# Output: 2 +# +# i=0: 0 mod 10 = 0 != $ints[0]. +# i=1: 1 mod 10 = 1 != $ints[1]. +# i=2: 2 mod 10 = 2 == $ints[2]. +# i=3: 3 mod 10 = 3 != $ints[3]. +# 2 is the only index which has i mod 10 == $ints[i]. +# +# Example 3 +# +# Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] +# Output: -1 +# Explanation: No index satisfies i mod 10 == $ints[i]. +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [0, 1, 2], + [4, 3, 2, 1], + [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], +]; + +sub smallest_index +{ + my $l = shift; + + for my $i (0..$#$l) { + return $i if ($i % 10) == $l->[$i]; + } + + return -1; +} + +is(smallest_index($cases->[0]), 0, 'Example 1'); +is(smallest_index($cases->[1]), 2, 'Example 2'); +is(smallest_index($cases->[2]), -1, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-250/peter-meszaros/perl/ch-2.pl b/challenge-250/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..1b1ce305a0 --- /dev/null +++ b/challenge-250/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# +# You are given an array of alphanumeric strings. +# +# Write a script to return the maximum value of alphanumeric string in the +# given array. +# +# The value of alphanumeric string can be defined as +# +# a) The numeric representation of the string in base 10 if it is made up of +# digits only. +# b) otherwise the length of the string +# +# Example 1 +# +# Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku") +# Output: 6 +# +# "perl" consists of letters only so the value is 4. +# "2" is digits only so the value is 2. +# "000" is digits only so the value is 0. +# "python" consits of letters so the value is 6. +# "r4ku" consists of letters and digits so the value is 4. +# +# Example 2 +# +# Input: @alphanumstr = ("001", "1", "000", "0001") +# Output: 1 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/max/; + +my $cases = [ + ["perl", "2", "000", "python", "r4ku"], + ["001", "1", "000", "0001"], +]; + +sub alnum_string_value +{ + my $l = shift; + + return max(map { /^\d+$/ ? $_+0 : length } @$l); +} + +is(alnum_string_value($cases->[0]), 6, 'Example 1'); +is(alnum_string_value($cases->[1]), 1, 'Example 2'); +done_testing(); + +exit 0; + + |
