diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2021-02-01 10:10:05 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2021-02-01 10:20:09 +0100 |
| commit | f409a7d8397e21e4d31b4132e22ed8881d925eb0 (patch) | |
| tree | ca6889616091bb82842a5ffcbb864acada8d0945 | |
| parent | c81724b6cb3ec4a583ef80d2376a08e92be208fd (diff) | |
| download | perlweeklychallenge-club-f409a7d8397e21e4d31b4132e22ed8881d925eb0.tar.gz perlweeklychallenge-club-f409a7d8397e21e4d31b4132e22ed8881d925eb0.tar.bz2 perlweeklychallenge-club-f409a7d8397e21e4d31b4132e22ed8881d925eb0.zip | |
Task 2 done right.
| -rw-r--r-- | challenge-098/luca-ferrari/raku/ch-2.p6 | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/challenge-098/luca-ferrari/raku/ch-2.p6 b/challenge-098/luca-ferrari/raku/ch-2.p6 index dc9bbdd1dd..e0a24eddc5 100644 --- a/challenge-098/luca-ferrari/raku/ch-2.p6 +++ b/challenge-098/luca-ferrari/raku/ch-2.p6 @@ -1,27 +1,24 @@ #!raku - -sub MAIN( ) { - my @N = 1,2,3,4, 7; - my $N = 6; - say "Array { @N } searching for $N"; +# Get an array of integers, the last value is the need to search for +# while all the other values are the array to search into. +# For example: +# ch-2.p6 1 2 3 7 8 9 5 +# means +# @N = 1,2,3,7,8,9 +# $N = 5 +sub MAIN( *@values where { @values.grep: * ~~ Int } ) { + my @N = @values[0 .. *-2 ]; + my $N = @values[ *-1 ]; +# say "Array { @N } searching for $N"; # get the index if the element is there - given @N.grep: $N, :k { .say && exit if $_ } - - # if here the element is not there - # for @N.List.kv -> $k, $v { - # if ( $v > $N ) { - # @N = |@N[ 0 .. $k - 1 ], $N, |@N[ $k .. * ]; - # "Insert value $N at index $k".say; - # } - # } - + given @N.grep( $N, :k ).first { .say && exit if $_ } - given @N.grep: { $_ > $N }, :k { + # if here the key is not there, let's see where to insert + given @N.grep( { $_ >= $N }, :k ).first { @N = |@N[ 0 .. $_ - 1 ], $N, |@N[ $_ .. * ]; - "Insert value $N at index $_".say; - exit; + .say && exit; } } |
