diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-26 10:43:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-26 10:43:58 +0100 |
| commit | b023909c82c6bb008d6cfa2239b4ffaf976fc9c0 (patch) | |
| tree | 0aa036ce7125051228e7b8cae23551cf9daa38f7 /challenge-209/paulo-custodio/cpp/ch-2.cpp | |
| parent | 3c907b5d42ebde85cb3ee174b68cdd0f6da2e8e2 (diff) | |
| parent | 05c416dd8e744f4c761d49ef5a0a720eb8198e90 (diff) | |
| download | perlweeklychallenge-club-b023909c82c6bb008d6cfa2239b4ffaf976fc9c0.tar.gz perlweeklychallenge-club-b023909c82c6bb008d6cfa2239b4ffaf976fc9c0.tar.bz2 perlweeklychallenge-club-b023909c82c6bb008d6cfa2239b4ffaf976fc9c0.zip | |
Merge pull request #7782 from pauloscustodio/master
Add Perl, C, C++, BASIC and Forth solutions
Diffstat (limited to 'challenge-209/paulo-custodio/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-209/paulo-custodio/cpp/ch-2.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
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..1b9ef82fd7 --- /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 <algorithm> +#include <iostream> +#include <string> +#include <vector> + +struct Account { + std::string name; + std::vector<std::string> 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<Account>& 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<Account>& 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: ch-2 name emails... name emails..." << std::endl; + return EXIT_FAILURE; + } + + std::vector<Account> 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; + } +} |
