diff options
| author | Andrew Shitov <andy@shitov.ru> | 2020-09-30 14:34:38 +0200 |
|---|---|---|
| committer | Andrew Shitov <andy@shitov.ru> | 2020-09-30 14:34:38 +0200 |
| commit | b4dc318d75475dd533c9a16289ce8c0f698865a8 (patch) | |
| tree | 0c76d5584ee9d1efff5abb2a8c1f49e3d30a540c /challenge-080 | |
| parent | 96f3ffb9600ca187d982c537939c096b084fe409 (diff) | |
| download | perlweeklychallenge-club-b4dc318d75475dd533c9a16289ce8c0f698865a8.tar.gz perlweeklychallenge-club-b4dc318d75475dd533c9a16289ce8c0f698865a8.tar.bz2 perlweeklychallenge-club-b4dc318d75475dd533c9a16289ce8c0f698865a8.zip | |
Solutions 080-1 in C++ and Raku
Diffstat (limited to 'challenge-080')
| -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..e006218f32 --- /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; +} |
