diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-17 20:38:28 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-17 20:38:28 +0000 |
| commit | a4af5299879128e7a16f4d81aa96c5b7f18f6744 (patch) | |
| tree | 087ce6356ea6e2d50b772b36215816484e5da334 /challenge-100/paulo-custodio/cpp | |
| parent | f8fb226e2de2fcbed5da6ac21eb54baa76ca935f (diff) | |
| download | perlweeklychallenge-club-a4af5299879128e7a16f4d81aa96c5b7f18f6744.tar.gz perlweeklychallenge-club-a4af5299879128e7a16f4d81aa96c5b7f18f6744.tar.bz2 perlweeklychallenge-club-a4af5299879128e7a16f4d81aa96c5b7f18f6744.zip | |
Add Awk, C, C++ and Python solutions to challenge 100
Diffstat (limited to 'challenge-100/paulo-custodio/cpp')
| -rw-r--r-- | challenge-100/paulo-custodio/cpp/ch-1.cpp | 111 | ||||
| -rw-r--r-- | challenge-100/paulo-custodio/cpp/ch-2.cpp | 119 |
2 files changed, 230 insertions, 0 deletions
diff --git a/challenge-100/paulo-custodio/cpp/ch-1.cpp b/challenge-100/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..389d36f9c9 --- /dev/null +++ b/challenge-100/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,111 @@ +/* +TASK #1 > Fun Time +Submitted by: Mohammad S Anwar +You are given a time (12 hour / 24 hour). + +Write a script to convert the given time from 12 hour format to 24 hour format +and vice versa. + +Ideally we expect a one-liner. + +Example 1: +Input: 05:15 pm or 05:15pm +Output: 17:15 +Example 2: +Input: 19:15 +Output: 07:15 pm or 07:15pm +*/ + +#include <iostream> +#include <iomanip> +#include <sstream> +#include <cctype> + +class Time { +public: + Time(int hours = 0, int minutes = 0) + : m_hours(hours), m_minutes(minutes), m_is12(false) {} + + int hours() const { return m_hours; } + int minutes() const { return m_minutes; } + bool is12() const { return m_is12; } + void set12() { m_is12 = true; } + void set24() { m_is12 = false; } + + friend std::ostream& operator<<(std::ostream& os, const Time& time); + friend std::istream& operator>>(std::istream& is, Time& time); + +private: + int m_hours, m_minutes; // always in 24 hour format + bool m_is12; +}; + +std::ostream& operator<<(std::ostream& os, const Time& time) { + if (time.m_is12) { + int hours = time.m_hours; + bool ispm = false; + if (hours == 0) + hours = 12; + else if (hours == 12) + ispm = true; + else if (hours > 12) { + ispm = true; + hours -= 12; + } + os << std::setw(2) << std::setfill('0') << hours << ":" + << std::setw(2) << std::setfill('0') << time.m_minutes + << (ispm ? "pm" : "am"); + } + else + os << std::setw(2) << std::setfill('0') << time.m_hours << ":" + << std::setw(2) << std::setfill('0') << time.m_minutes; + return os; +} + +std::istream& operator>>(std::istream& is, Time& time) { + // get hh:mm + char colon = '\0'; + if (!(is >> time.m_hours >> colon >> time.m_minutes) || colon != ':') { + std::cerr << "error parsing time" << std::endl; + exit(EXIT_FAILURE); + } + + // get optional am|pm + std::string ampm; + is >> ampm; + + // convert from 12 to 24 format + if (!ampm.empty()) { + switch (tolower(ampm[0])) { + case 'a': + if (time.m_hours == 12) time.m_hours = 0; + time.m_is12 = true; + break; + case 'p': + if (time.m_hours != 12) time.m_hours += 12; + time.m_is12 = true; + break; + default: + std::cerr << "error parsing time" << std::endl; + exit(EXIT_FAILURE); + } + } + return is; +} + + +int main(int argc, char* argv[]) { + if (argc != 2) { + fputs("Usage: ch-1 time", stderr); + return EXIT_FAILURE; + } + + Time time; + std::stringstream ss(argv[1]); + ss >> time; + if (time.is12()) + time.set24(); + else + time.set12(); + std::cout << time << std::endl; +} diff --git a/challenge-100/paulo-custodio/cpp/ch-2.cpp b/challenge-100/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..b672a5cffe --- /dev/null +++ b/challenge-100/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,119 @@ +/* +TASK #2 > Triangle Sum +Submitted by: Mohammad S Anwar +You are given triangle array. + +Write a script to find the minimum path sum from top to bottom. + +When you are on index i on the current row then you may move to either +index i or index i + 1 on the next row. + +Example 1: +Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] +Output: 8 + +Explanation: The given triangle + + 1 + 2 4 + 6 4 9 + 5 1 7 2 + +The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 + + [1] + [2] 4 + 6 [4] 9 + 5 [1] 7 2 +Example 2: +Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] +Output: 7 + +Explanation: The given triangle + + 3 + 3 1 + 5 2 3 + 4 3 1 3 + +The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7 + + [3] + 3 [1] + 5 [2] 3 + 4 3 [1] 3 +*/ + +#include <iostream> +#include <vector> +#include <cassert> +#include <cctype> + +#define MIN(a,b) ((a)<(b)?(a):(b)) + +// triangle (stored as a square) +class Triangle { +public: + bool parse(int argc, char* argv[]) { + for (int row = 0; row < argc; row++) { + add_row(); + if (!parse_row(row, argv[row])) + return false; + } + return true; + } + + int min_sum() { + return min_sum_1(0, 0, 0); + } + +private: + std::vector<std::vector<int>> m_data; + + void add_row() { + int rows = static_cast<int>(m_data.size()) + 1; + m_data.resize(rows); + for (int i = 0; i < rows; i++) + m_data[i].resize(rows); + } + + bool parse_row(int row, const char* text) { + int rows = static_cast<int>(m_data.size()); + assert(row >= 0 && row < rows); + for (int i = 0; i < rows; i++) { + while (*text && !isdigit(*text)) text++; + if (!isdigit(*text)) + return false; + m_data[row][i] = atoi(text); + while (*text && isdigit(*text)) text++; + } + return true; + } + + int min_sum_1(int sum, int row, int col) { + sum += m_data[row][col]; + int rows = static_cast<int>(m_data.size()); + if (row + 1 == rows) + return sum; + else { + int sum1 = min_sum_1(sum, row + 1, col); + int sum2 = min_sum_1(sum, row + 1, col + 1); + return MIN(sum1, sum2); + } + } +}; + +int main(int argc, char* argv[]) { + if (argc == 1) { + fputs("Usage: ch-2 row row ...", stderr); + return EXIT_FAILURE; + } + argc--; argv++; + Triangle triangle; + if (!triangle.parse(argc, argv)) { + fputs("Malformed triangle", stderr); + return EXIT_FAILURE; + } + int sum = triangle.min_sum(); + std::cout << sum << std::endl; +} |
