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-2.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-2.cpp')
| -rw-r--r-- | challenge-242/paulo-custodio/cpp/ch-2.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/challenge-242/paulo-custodio/cpp/ch-2.cpp b/challenge-242/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..62036b00db --- /dev/null +++ b/challenge-242/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,128 @@ +/* +Challenge 242 + +Task 2: Flip Matrix +Submitted by: Mohammad S Anwar +You are given n x n binary matrix. + +Write a script to flip the given matrix as below. + + +1 1 0 +0 1 1 +0 0 1 + +a) Reverse each row + +0 1 1 +1 1 0 +1 0 0 + +b) Invert each member + +1 0 0 +0 0 1 +0 1 1 + +Example 1 +Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]) +Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) +Example 2 +Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]) +Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) +*/ + +#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); + } + } + return result; +} + + +vector<vector<int>> parse_matrix(const string& text) { + vector<vector<int>> matrix; + auto rows_text = regex_split(text, regex("\\],")); + for (auto& row_text : rows_text) { + auto row = parse_list(row_text); + matrix.push_back(row); + } + return matrix; +} + +void process_matrix(vector<vector<int>>& matrix) { + for (auto& row : matrix) { + // a) Reverse each row + reverse(row.begin(), row.end()); + + // b) Invert each member + for (auto& n : row) + n = 1 - n; + } +} + +void print_list(const vector<int>& arr) { + cout << "["; + string sep = ""; + for (auto& n : arr) { + cout << sep << n; + sep = ", "; + } + cout << "]"; +} + +void print_matrix(vector<vector<int>>& matrix) { + cout << "("; + string sep = ""; + for (auto& row : matrix) { + cout << sep; + print_list(row); + sep = ", "; + } + cout << ")"; +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + cerr << "Usage: ch-2 '([a, b, c], [d, e, f], [g, h, i])'" << endl; + exit(EXIT_FAILURE); + } + + auto matrix = parse_matrix(argv[1]); + process_matrix(matrix); + print_matrix(matrix); +#if 0 + free_matrix(matrix); + + utarray_free(matrix); +#endif +} +#if 0 + + +void free_matrix(UT_array* matrix) { + for (UT_array** p = NULL; (p = utarray_next(matrix, p)) != NULL; ) { + utarray_free(*p); + *p = NULL; + } +} + +#endif |
