diff options
| author | Simon Proctor <simon.proctor@zoopla.co.uk> | 2021-02-01 12:01:57 +0000 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zoopla.co.uk> | 2021-02-01 12:01:57 +0000 |
| commit | 87f34df3cbddc3c530c7984f31b2d442ffe2f184 (patch) | |
| tree | 008d8a1363535d1ec099740c2c5e100d3ce488c3 | |
| parent | ac60bddb13f96402c3026283fe223388ed54fc27 (diff) | |
| download | perlweeklychallenge-club-87f34df3cbddc3c530c7984f31b2d442ffe2f184.tar.gz perlweeklychallenge-club-87f34df3cbddc3c530c7984f31b2d442ffe2f184.tar.bz2 perlweeklychallenge-club-87f34df3cbddc3c530c7984f31b2d442ffe2f184.zip | |
Doing part 2 first this week. Will look at the seek stuff in a bit.
| -rw-r--r-- | challenge-098/simon-proctor/raku/ch-2.raku | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-098/simon-proctor/raku/ch-2.raku b/challenge-098/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..092b3fe86f --- /dev/null +++ b/challenge-098/simon-proctor/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku + +use v6; + +#| Given an int and a sorted list of ints output the index the first valus has (or should have) in the list +multi sub MAIN ( Int $N, #= Integer to check the location of + *@N where @N.sort ~~ @N #= Sorted list of integers + ) { + say find-in-list($N,@N); +} + +multi sub MAIN( "test" ) is hidden-from-USAGE { + use Test; + is( find-in-list( 3, [1,2,3,4] ), 2 ); + is( find-in-list( 6, [1,3,5,7] ), 3 ); + is( find-in-list( 10, [12,14,16,18] ), 0 ); + is( find-in-list( 19, [11,13,15,17] ), 4 ); + done-testing; +} + +sub find-in-list( Int $n, @n ) { + for @n.kv -> $i, $v { + return $i if $v ~~ $n; + return $i if $v > $n; + } + return @n.elems; +} |
