diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-01-01 16:46:21 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-01-05 17:13:30 +0100 |
| commit | a5df1d5e9605c44e2fd909c192aa8336c66fa103 (patch) | |
| tree | cfcdf4a8a2d794d65a5f1276dae8277c4c62c5f4 | |
| parent | 771b67fe896d14f2244e14255e847aa34a3e5a7e (diff) | |
| download | perlweeklychallenge-club-a5df1d5e9605c44e2fd909c192aa8336c66fa103.tar.gz perlweeklychallenge-club-a5df1d5e9605c44e2fd909c192aa8336c66fa103.tar.bz2 perlweeklychallenge-club-a5df1d5e9605c44e2fd909c192aa8336c66fa103.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-250/jo-37/perl/ch-1.pl | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-250/jo-37/perl/ch-1.pl b/challenge-250/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..e734e957f3 --- /dev/null +++ b/challenge-250/jo-37/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples, $base); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-base=B] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-base=B + use B as base. Default: 10 + +N... + list of numbers + +EOS + + +### Input and Output + +say smallest_index($base // 10, @ARGV); + + +### Implementation + +sub smallest_index { + my $base = shift; + while (my ($i, $n) = each @_) { + return $i if $n == $i % $base; + } + -1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is smallest_index(10,=> 0, 1, 2), 0, 'example 1'; + is smallest_index(10,=> 4, 3, 2, 1), 2, 'example 2'; + is smallest_index(10,=> 1, 2, 3, 4, 5, 6, 7, 8, 9, 0), -1, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is smallest_index(2,=> 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1), 9, 'base 2'; + } + + done_testing; + exit; +} |
