From f036ddd90eca9f7472fed1034d5e223e46a7543f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 22 Mar 2023 19:34:38 +0000 Subject: Add C and C++ solutions --- challenge-209/paulo-custodio/cpp/ch-2.cpp | 101 ++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 challenge-209/paulo-custodio/cpp/ch-2.cpp (limited to 'challenge-209/paulo-custodio/cpp/ch-2.cpp') diff --git a/challenge-209/paulo-custodio/cpp/ch-2.cpp b/challenge-209/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..0767787b2d --- /dev/null +++ b/challenge-209/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,101 @@ +/* +Challenge 209 + +Task 2: Merge Account +Submitted by: Mohammad S Anwar + +You are given an array of accounts i.e. name with list of email addresses. + +Write a script to merge the accounts where possible. The accounts can only +be merged if they have at least one email address in common. + +Example 1: + +Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"], + ["B", "b1@b.com"], + ["A", "a3@a.com", "a1@a.com"] ] + ] + +Output: [ ["A", "a1@a.com", "a2@a.com", "a3@a.com"], + ["B", "b1@b.com"] ] + +Example 2: + +Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"], + ["B", "b1@b.com"], + ["A", "a3@a.com"], + ["B", "b2@b.com", "b1@b.com"] ] + +Output: [ ["A", "a1@a.com", "a2@a.com"], + ["A", "a3@a.com"], + ["B", "b1@b.com", "b2@b.com"] ] +*/ + +#include +#include +#include +#include + +struct Account { + std::string name; + std::vector emails; + + Account(const std::string& name_) : name(name_) {} + + void merge(const Account& other) { + emails.insert(emails.end(), other.emails.begin(), other.emails.end()); // concatenate + std::sort(emails.begin(), emails.end()); // sort + auto it = std::unique(emails.begin(), emails.end()); // uniq + emails.resize(std::distance(emails.begin(), it)); + } +}; + +bool find_common(std::vector& accs, size_t& a, size_t& b) { + for (a = 0; a < accs.size() - 1; a++) { + for (size_t i = 0; i < accs[a].emails.size(); i++) { + std::string& email = accs[a].emails[i]; + for (b = a + 1; b < accs.size(); b++) { + auto it = std::find(accs[b].emails.begin(), accs[b].emails.end(), email); + if (it != accs[b].emails.end()) + return true; + } + } + } + return false; +} + +void merge_accounts(std::vector& accs) { + size_t a, b; + while (find_common(accs, a, b)) { + accs[a].merge(accs[b]); + accs.erase(accs.begin() + b); + } +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr << "usage: name emails... name emails..." << std::endl; + return EXIT_FAILURE; + } + + std::vector accs; + + for (int i = 0; i < argc; i++) { + std::string name = argv[i++]; + accs.emplace_back(name); + for (; i < argc && std::string(argv[i])!= ","; i++) { + std::string email = argv[i]; + accs.back().emails.push_back(email); + } + } + + merge_accounts(accs); + + for (size_t i = 0; i < accs.size(); i++) { + std::cout << accs[i].name << " "; + for (size_t j = 0; j < accs[i].emails.size(); j++) + std::cout << accs[i].emails[j] << " "; + std::cout << std::endl; + } +} -- cgit From 6737a18810400c88848bb9364f9abf5297a5caef Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 25 Mar 2023 15:51:06 +0000 Subject: Add Perl, C, C++, BASIC and Forth solutions --- challenge-209/paulo-custodio/cpp/ch-2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-209/paulo-custodio/cpp/ch-2.cpp') diff --git a/challenge-209/paulo-custodio/cpp/ch-2.cpp b/challenge-209/paulo-custodio/cpp/ch-2.cpp index 0767787b2d..1b9ef82fd7 100644 --- a/challenge-209/paulo-custodio/cpp/ch-2.cpp +++ b/challenge-209/paulo-custodio/cpp/ch-2.cpp @@ -75,7 +75,7 @@ void merge_accounts(std::vector& accs) { int main(int argc, char* argv[]) { argv++; argc--; if (argc == 0) { - std::cerr << "usage: name emails... name emails..." << std::endl; + std::cerr << "usage: ch-2 name emails... name emails..." << std::endl; return EXIT_FAILURE; } -- cgit