diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-01 14:09:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-01 14:09:46 +0000 |
| commit | 0bea5e0b6e1e3bdc0906981495ebd9dd4cfc707e (patch) | |
| tree | 0fc0b19517b7c6d99673fa6ed7791776d664c0c0 | |
| parent | cb3a2233706bd3af92d2759c55e569b6881dfaf5 (diff) | |
| parent | 44c8693d5c2ee99d39cd0f1a929454d426b0d12e (diff) | |
| download | perlweeklychallenge-club-0bea5e0b6e1e3bdc0906981495ebd9dd4cfc707e.tar.gz perlweeklychallenge-club-0bea5e0b6e1e3bdc0906981495ebd9dd4cfc707e.tar.bz2 perlweeklychallenge-club-0bea5e0b6e1e3bdc0906981495ebd9dd4cfc707e.zip | |
Merge pull request #9329 from boblied/w250
Week 250 from Bob Lied
| -rw-r--r-- | challenge-250/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-250/bob-lied/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-250/bob-lied/perl/ch-2.pl | 51 |
3 files changed, 98 insertions, 3 deletions
diff --git a/challenge-250/bob-lied/README b/challenge-250/bob-lied/README index 851f9dd290..a315d04ddf 100644 --- a/challenge-250/bob-lied/README +++ b/challenge-250/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 249 by Bob Lied +Solutions to weekly challenge 250 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-249/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-249/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-250/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-250/bob-lied diff --git a/challenge-250/bob-lied/perl/ch-1.pl b/challenge-250/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..c5f29befce --- /dev/null +++ b/challenge-250/bob-lied/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 250 Task 1 Smallest Index +#============================================================================= +# 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 +# Example 2 Input: @ints = (4, 3, 2, 1) +# Output: 2 +# Example 3 Input: @ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] +# Output: -1 +#============================================================================= + +use v5.38; + +use List::Util qw/first/; + +use Getopt::Long; +my $DoTest = 0; + +GetOptions("test" => \$DoTest); +exit(!runTest()) if $DoTest; + +say smallestIndex(@ARGV); + +sub smallestIndex(@ints) +{ + return (first { ($_ % 10) == $ints[$_] } 0 .. $#ints) // -1; +} + +sub runTest +{ + use Test2::V0; + + is( smallestIndex(0,1,2 ), 0, "Example 1"); + is( smallestIndex(4,3,2,1), 2, "Example 2"); + is( smallestIndex(1..9,0 ), -1, "Example 3"); + + done_testing; +} diff --git a/challenge-250/bob-lied/perl/ch-2.pl b/challenge-250/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..a697126c24 --- /dev/null +++ b/challenge-250/bob-lied/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-2.pl Perl Weekly Challenge 250 Task 2 Alphanumeric String Value +#============================================================================= +# 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 +# Example 2 Input: @alphanumstr = ("001", "1", "000", "0001") +# Output: 1 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say alnumstr(@ARGV); + +sub alnumstr(@str) +{ + use List::Util qw/max/; + return max map { $_ =~ m/\A\d+\Z/a ? 0+$_ : length($_) } @str; +} + +sub runTest +{ + use Test2::V0; + + is( alnumstr("perl", "2", "000", "python", "r4ku"), 6, "Example 1"); + is( alnumstr( "001", "1", "000", "0001" ), 1, "Example 1"); + + is( alnumstr( "017", "016"), 17, "Octal?"); + + is( alnumstr( "03", "02", "0x1"), 3, "Hex"); + + done_testing; +} |
