diff options
| -rw-r--r-- | challenge-080/ash/cpp/ch-1.cpp | 38 | ||||
| -rw-r--r-- | challenge-080/ash/raku/ch-1.raku | 25 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-080/ash/cpp/ch-1.cpp b/challenge-080/ash/cpp/ch-1.cpp new file mode 100644 index 0000000000..278833993b --- /dev/null +++ b/challenge-080/ash/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +/* + Task 1 from + https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +*/ + +#include <iostream> +#include <vector> +#include <map> + +using namespace std; + +int missing_n(vector<int> n) { + map<int, bool> v; + + int max = n[0]; + for (auto x : n) { + v[x] = true; + if (x > max) max = x; + } + + for (auto c = 1; c < max; c++) { + if (!v[c]) { + return c; + break; + } + } + + return max + 1; +} + +int main() { + cout << (missing_n(vector<int> {5, 2, -2, 0}) == 1) << endl; + cout << (missing_n(vector<int> {1, 8, -1}) == 2) << endl; + cout << (missing_n(vector<int> {5, 2, -2, 0}) == 1) << endl; + + cout << (missing_n(vector<int> {5, 2, 3, 1, -2, 0}) == 4) << endl; + cout << (missing_n(vector<int> {5, 2, 3, 1, 4, -2, 0}) == 6) << endl; +} diff --git a/challenge-080/ash/raku/ch-1.raku b/challenge-080/ash/raku/ch-1.raku new file mode 100644 index 0000000000..d8de2c2957 --- /dev/null +++ b/challenge-080/ash/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku +# +# Task 1 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ + +use Test; + +is missing-n(5, 2, -2, 0), 1; +is missing-n(1, 8, -1), 2; +is missing-n(2, 0, -1), 1; + +# If the smallest missing is the biggest :-) +is missing-n(5, 2, 3, 1, -2, 0), 4; +is missing-n(5, 2, 3, 1, 4, -2, 0), 6; + +sub missing-n(*@n is copy) { + @n.=grep: * > 0; + + my $missing = max(@n); + for 1 .. $missing + 1 { + $missing = $_ and last unless @n.grep($_); + } + + return $missing; +} |
