diff options
| author | ntovar <tovar.nelo@gmail.com> | 2023-11-13 10:19:21 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2023-11-13 10:19:21 -0500 |
| commit | 4333c415ec1503cffa0618876e2ded4f6eb2ce8d (patch) | |
| tree | 00121627979e3bd168d6e1b14b873e2f6b1df575 /challenge-242/paulo-custodio/cpp/ch-1.cpp | |
| parent | 04f30726f8970a4ac098e39f897f8761ba6ee8f8 (diff) | |
| parent | aeed5ae2bdafdcf14de24d172392278b8ba0b44f (diff) | |
| download | perlweeklychallenge-club-4333c415ec1503cffa0618876e2ded4f6eb2ce8d.tar.gz perlweeklychallenge-club-4333c415ec1503cffa0618876e2ded4f6eb2ce8d.tar.bz2 perlweeklychallenge-club-4333c415ec1503cffa0618876e2ded4f6eb2ce8d.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-242/paulo-custodio/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-242/paulo-custodio/cpp/ch-1.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/challenge-242/paulo-custodio/cpp/ch-1.cpp b/challenge-242/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..5475fbaaf2 --- /dev/null +++ b/challenge-242/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,101 @@ +/* +Challenge 242 + +Task 1: Missing Members +Submitted by: Mohammad S Anwar +You are given two arrays of integers. + +Write a script to find out the missing members in each other arrays. + +Example 1 +Input: @arr1 = (1, 2, 3) + @arr2 = (2, 4, 6) +Output: ([1, 3], [4, 6]) + +(1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +(2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +Example 2 +Input: @arr1 = (1, 2, 3, 3) + @arr2 = (1, 1, 2, 2) +Output: ([3]) + +(1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +(1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). +*/ + +#include <algorithm> +#include <iostream> +#include <regex> +#include <string> +#include <vector> +using namespace std; + +vector<string> regex_split(const string& s, const regex& sep_regex = regex{ "\\s+" }) { + sregex_token_iterator iter(s.begin(), s.end(), sep_regex, -1); + sregex_token_iterator end; + return { iter, end }; +} + +vector<int> parse_list(const string& text) { + vector<int> result; + auto nums = regex_split(text, regex{ "[\\[\\](),; \\t]+" }); + for (auto& num : nums) { + if (!num.empty()) { + int n = stoi(num); + result.push_back(n); + } + } + sort(result.begin(), result.end()); + return result; +} + +vector<int> find_not_in(const vector<int>& arr1, const vector<int>& arr2) { + vector<int> result; + for (auto& n : arr1) { + if (!binary_search(arr2.begin(), arr2.end(), n)) + if (!binary_search(result.begin(), result.end(), n)) + result.push_back(n); + } + return result; +} + +void print_list(const vector<int>& arr) { + cout << "["; + string sep = ""; + for (auto& n : arr) { + cout << sep << n; + sep = ", "; + } + cout << "]"; +} + +void print_result(const vector<int>& arr1, const vector<int>& arr2) { + cout << "("; + string sep = ""; + if (!arr1.empty()) { + cout << sep; + print_list(arr1); + sep = ", "; + } + if (!arr2.empty()) { + cout << sep; + print_list(arr2); + sep = ", "; + } + cout << ")"; +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + cerr << "Usage: ch-1 '(a,b,c)' '(d,e,f)'" << endl; + exit(EXIT_FAILURE); + } + + auto arr1 = parse_list(argv[1]); + auto arr2 = parse_list(argv[2]); + + auto not_in1 = find_not_in(arr1, arr2); + auto not_in2 = find_not_in(arr2, arr1); + + print_result(not_in1, not_in2); +} |
