diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-07-02 19:43:01 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-07-02 19:43:01 +0100 |
| commit | 0ecbc6fb2a0ae9e9e5ccd6092d772579466d48a4 (patch) | |
| tree | 96ec09c70bdba08745512c8c6e2e5b30392e7cfd | |
| parent | 4833efb812b0f6b044b40b0269f623ffd221edb8 (diff) | |
| download | perlweeklychallenge-club-0ecbc6fb2a0ae9e9e5ccd6092d772579466d48a4.tar.gz perlweeklychallenge-club-0ecbc6fb2a0ae9e9e5ccd6092d772579466d48a4.tar.bz2 perlweeklychallenge-club-0ecbc6fb2a0ae9e9e5ccd6092d772579466d48a4.zip | |
Add C, C++ and D solutions to challenge 117
| -rw-r--r-- | challenge-117/paulo-custodio/c/ch-1.c | 54 | ||||
| -rw-r--r-- | challenge-117/paulo-custodio/c/ch-2.c | 79 | ||||
| -rw-r--r-- | challenge-117/paulo-custodio/cpp/ch-1.cpp | 54 | ||||
| -rw-r--r-- | challenge-117/paulo-custodio/cpp/ch-2.cpp | 63 | ||||
| -rw-r--r-- | challenge-117/paulo-custodio/d/ch_1.d | 51 | ||||
| -rw-r--r-- | challenge-117/paulo-custodio/d/ch_2.d | 61 |
6 files changed, 362 insertions, 0 deletions
diff --git a/challenge-117/paulo-custodio/c/ch-1.c b/challenge-117/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..7e7f32e758 --- /dev/null +++ b/challenge-117/paulo-custodio/c/ch-1.c @@ -0,0 +1,54 @@ +/* +Challenge 117 + +TASK #1 › Missing Row +Submitted by: Mohammad S Anwar +You are given text file with rows numbered 1-15 in random order but there is a +catch one row in missing in the file. + +11, Line Eleven +1, Line one +9, Line Nine +13, Line Thirteen +2, Line two +6, Line Six +8, Line Eight +10, Line Ten +7, Line Seven +4, Line Four +14, Line Fourteen +3, Line three +15, Line Fifteen +5, Line Five + +Write a script to find the missing row number. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#define MAXLINE 1024 +#define NUM_LINES 15 + +int main(void) { + bool found[NUM_LINES]; + memset(found, 0, sizeof(found)); + + char line[MAXLINE]; + while ((fgets(line, sizeof(line), stdin)) != NULL) { + int n = atoi(line); + if (n >= 1 && n <= NUM_LINES) + found[n - 1] = true; + } + + const char* separator = ""; + for (int n = 1; n <= NUM_LINES; n++) { + if (!found[n - 1]) { + printf("%s%d", separator, n); + separator = ", "; + } + } + printf("\n"); +} diff --git a/challenge-117/paulo-custodio/c/ch-2.c b/challenge-117/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..3103a1e7e7 --- /dev/null +++ b/challenge-117/paulo-custodio/c/ch-2.c @@ -0,0 +1,79 @@ +/* +Challenge 117 + +TASK #2 - Find Possible Paths +Submitted by: E. Choroba +You are given size of a triangle. + +Write a script to find all possible paths from top to the bottom right +corner. + +In each step, we can either move horizontally to the right (H), or move +downwards to the left (L) or right (R). + +BONUS: Try if it can handle triangle of size 10 or 20. + +Example 1: +Input: $N = 2 + + S + / \ + / _ \ + /\ /\ + /__\ /__\ E + +Output: RR, LHR, LHLH, LLHH, RLH, LRH +Example 2: +Input: $N = 1 + + S + / \ + / _ \ E + +Output: R, LH +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +const char* separator = ""; + +void* check_mem(void* p) { + if (!p) { + fputs("out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +void find_paths(int size, const char* path, int row, int col) { + if (row == size && col == size) { // reached end + printf("%s%s", separator, path); + separator = ", "; + } + else { // recurse + int len = strlen(path) + 2; + char* new_path = check_mem(malloc(len)); + + if (row < size) { + sprintf(new_path, "%sL", path); + find_paths(size, new_path, row + 1, col); + + sprintf(new_path, "%sR", path); + find_paths(size, new_path, row + 1, col + 1); + } + if (col < row) { + sprintf(new_path, "%sH", path); + find_paths(size, new_path, row, col + 1); + } + free(new_path); + } +} + +int main(int argc, char* argv[]) { + int size = 1; + if (argc == 2) size = atoi(argv[1]); + find_paths(size, "", 0, 0); + printf("\n"); +} diff --git a/challenge-117/paulo-custodio/cpp/ch-1.cpp b/challenge-117/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..04f987ee39 --- /dev/null +++ b/challenge-117/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,54 @@ +/* +Challenge 117 + +TASK #1 › Missing Row +Submitted by: Mohammad S Anwar +You are given text file with rows numbered 1-15 in random order but there is a +catch one row in missing in the file. + +11, Line Eleven +1, Line one +9, Line Nine +13, Line Thirteen +2, Line two +6, Line Six +8, Line Eight +10, Line Ten +7, Line Seven +4, Line Four +14, Line Fourteen +3, Line three +15, Line Fifteen +5, Line Five + +Write a script to find the missing row number. +*/ + +#include <iostream> +#include <sstream> +#include <string> +using namespace std; + +const int NUM_LINES = 15; + +int main() { + bool found[NUM_LINES] = { 0 }; + + string line; + while (getline(cin, line)) { + istringstream iss{ line }; + int n; + iss >> n; + if (n >= 1 && n <= NUM_LINES) + found[n - 1] = true; + } + + string separator = ""; + for (int n = 1; n <= NUM_LINES; n++) { + if (!found[n - 1]) { + cout << separator << n; + separator = ", "; + } + } + cout << endl; +} diff --git a/challenge-117/paulo-custodio/cpp/ch-2.cpp b/challenge-117/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..5b51d2bce4 --- /dev/null +++ b/challenge-117/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,63 @@ +/* +Challenge 117 + +TASK #2 - Find Possible Paths +Submitted by: E. Choroba +You are given size of a triangle. + +Write a script to find all possible paths from top to the bottom right +corner. + +In each step, we can either move horizontally to the right (H), or move +downwards to the left (L) or right (R). + +BONUS: Try if it can handle triangle of size 10 or 20. + +Example 1: +Input: $N = 2 + + S + / \ + / _ \ + /\ /\ + /__\ /__\ E + +Output: RR, LHR, LHLH, LLHH, RLH, LRH +Example 2: +Input: $N = 1 + + S + / \ + / _ \ E + +Output: R, LH +*/ + +#include <iostream> +#include <string> +using namespace std; + +string separator = ""; + +void find_paths(int size, const string& path, int row, int col) { + if (row == size && col == size) { // reached end + cout << separator << path; + separator = ", "; + } + else { // recurse + if (row < size) { + find_paths(size, path + "L", row + 1, col); + find_paths(size, path + "R", row + 1, col + 1); + } + if (col < row) { + find_paths(size, path + "H", row, col + 1); + } + } +} + +int main(int argc, char* argv[]) { + int size = 1; + if (argc == 2) size = atoi(argv[1]); + find_paths(size, "", 0, 0); + cout << endl; +} diff --git a/challenge-117/paulo-custodio/d/ch_1.d b/challenge-117/paulo-custodio/d/ch_1.d new file mode 100644 index 0000000000..935ce9da7f --- /dev/null +++ b/challenge-117/paulo-custodio/d/ch_1.d @@ -0,0 +1,51 @@ +/* +Challenge 117 + +TASK #1 - Missing Row +Submitted by: Mohammad S Anwar +You are given text file with rows numbered 1-15 in random order but there is a +catch one row in missing in the file. + +11, Line Eleven +1, Line one +9, Line Nine +13, Line Thirteen +2, Line two +6, Line Six +8, Line Eight +10, Line Ten +7, Line Seven +4, Line Four +14, Line Fourteen +3, Line three +15, Line Fifteen +5, Line Five + +Write a script to find the missing row number. +*/ + +import std.stdio; +import std.conv; +import std.array; + +enum NUM_LINES = 15; + +void main() { + bool[NUM_LINES] found = false; + + string line; + while ((line = readln()) !is null) { + int n = to!int(line.split(",")[0]); + if (n >= 1 && n <= NUM_LINES) + found[n - 1] = true; + } + + string separator = ""; + for (int n = 1; n <= NUM_LINES; n++) { + if (!found[n - 1]) { + write(separator, n); + separator = ", "; + } + } + writeln(""); +} diff --git a/challenge-117/paulo-custodio/d/ch_2.d b/challenge-117/paulo-custodio/d/ch_2.d new file mode 100644 index 0000000000..1cb659597e --- /dev/null +++ b/challenge-117/paulo-custodio/d/ch_2.d @@ -0,0 +1,61 @@ +/* +Challenge 117 + +TASK #2 - Find Possible Paths +Submitted by: E. Choroba +You are given size of a triangle. + +Write a script to find all possible paths from top to the bottom right +corner. + +In each step, we can either move horizontally to the right (H), or move +downwards to the left (L) or right (R). + +BONUS: Try if it can handle triangle of size 10 or 20. + +Example 1: +Input: $N = 2 + + S + / \ + / _ \ + /\ /\ + /__\ /__\ E + +Output: RR, LHR, LHLH, LLHH, RLH, LRH +Example 2: +Input: $N = 1 + + S + / \ + / _ \ E + +Output: R, LH +*/ + +import std.stdio; +import std.conv; + +string separator = ""; + +void find_paths(int size, string path, int row, int col) { + if (row == size && col == size) { // reached end + write(separator, path); + separator = ", "; + } + else { // recurse + if (row < size) { + find_paths(size, path ~ "L", row + 1, col); + find_paths(size, path ~ "R", row + 1, col + 1); + } + if (col < row) { + find_paths(size, path ~ "H", row, col + 1); + } + } +} + +void main(string[] args) { + int size = to!int(args[1]); + find_paths(size, "", 0, 0); + writeln(""); +} |
