diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-01-01 07:21:34 -0600 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-01-01 07:21:34 -0600 |
| commit | 9ce5d954d0e8633492d97451dcaa3f93c103831f (patch) | |
| tree | 432b4ceceb9f54a9bc110c7d72ce3ae8b5e50e32 | |
| parent | 9a04a55cb9ff968ec242cb1c1a99a3b492dda072 (diff) | |
| download | perlweeklychallenge-club-9ce5d954d0e8633492d97451dcaa3f93c103831f.tar.gz perlweeklychallenge-club-9ce5d954d0e8633492d97451dcaa3f93c103831f.tar.bz2 perlweeklychallenge-club-9ce5d954d0e8633492d97451dcaa3f93c103831f.zip | |
Week 250 task 1 done
| -rw-r--r-- | challenge-250/bob-lied/perl/ch-1.pl | 44 |
1 files changed, 44 insertions, 0 deletions
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; +} |
