diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 13:06:34 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 13:06:34 +0100 |
| commit | a45d112c43dc7f72b3cd924f62b8015b24647755 (patch) | |
| tree | 5544b878bf091c470c3de7285e5e9066e0017ef0 /challenge-080/mohammad-anwar | |
| parent | 37e9f9b26e307f7c9d0bbaf65000c50c4b694964 (diff) | |
| download | perlweeklychallenge-club-a45d112c43dc7f72b3cd924f62b8015b24647755.tar.gz perlweeklychallenge-club-a45d112c43dc7f72b3cd924f62b8015b24647755.tar.bz2 perlweeklychallenge-club-a45d112c43dc7f72b3cd924f62b8015b24647755.zip | |
- Added Raku solution to "Smallest Positive Number" task.
Diffstat (limited to 'challenge-080/mohammad-anwar')
| -rw-r--r-- | challenge-080/mohammad-anwar/raku/ch-1.raku | 28 | ||||
| -rw-r--r-- | challenge-080/mohammad-anwar/raku/ch-1.t | 48 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-080/mohammad-anwar/raku/ch-1.raku b/challenge-080/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..f7c1d02b5a --- /dev/null +++ b/challenge-080/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 080 +# +# Task #1: Smallest Positive Number +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080 +# + +use v6.d; + +sub MAIN(:@N where .all ~~ Int = (2, 3, 7, 6, 8, -1, -10, 15)) { + say smallest-positive-number(@N); +} + +sub smallest-positive-number(@n where .all ~~ Int) { + + my @positive-numbers = @n.sort.grep: { $_ > 0 }; + return 1 unless @positive-numbers.elems; + + my $i = 0; + (1 .. @positive-numbers.tail).map: -> $n { + return $n if $n < @positive-numbers[$i++] + }; + + return ++@positive-numbers.tail; +} diff --git a/challenge-080/mohammad-anwar/raku/ch-1.t b/challenge-080/mohammad-anwar/raku/ch-1.t new file mode 100644 index 0000000000..523fac682d --- /dev/null +++ b/challenge-080/mohammad-anwar/raku/ch-1.t @@ -0,0 +1,48 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 080 +# +# Task #1: Smallest Positive Number +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080 +# + +use Test; + +# examples from the task +is smallest-positive-number((5, 2, -2, 0)), 1, + "testing (5, 2, -2, 0)"; +is smallest-positive-number((1, 8, -1)), 2, + "testing (1, 8, -1)"; +is smallest-positive-number((2, 0, -1)), 1, + "testing (2, 0, -1)"; + +# some other test cases +is smallest-positive-number((1, 2, 0)), 3, + "testing (1, 2, 0)"; +is smallest-positive-number((-8, -7, -6)), 1, + "testing (-8, -7, -6)"; +is smallest-positive-number((3, 4, -1, 1)), 2, + "testing (3, 4, -1, 1)"; +is smallest-positive-number((2, 3, 7, 6, 8, -1, -10, 15)), 1, + "testing (2, 3, 7, 6, 8, -1, -10, 15)"; + +done-testing; + +# +# +# SUBROUTINE + +sub smallest-positive-number(@n where .all ~~ Int) { + + my @positive-numbers = @n.sort.grep: { $_ > 0 }; + return 1 unless @positive-numbers.elems; + + my $i = 0; + (1 .. @positive-numbers.tail).map: -> $n { + return $n if $n < @positive-numbers[$i++] + }; + + return ++@positive-numbers.tail; +} |
