diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-03 11:07:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-03 11:07:42 +0100 |
| commit | ce74b51ac90c134d77cdfd57a6115adf105662ec (patch) | |
| tree | 4029817943f9c542cd6513c7fae4f421e733796a | |
| parent | c6fc70f991586bd482db4d37a326184e1fe8bad7 (diff) | |
| parent | 513a02f52eaa1fa3aad6853daf10b3e6da3482ad (diff) | |
| download | perlweeklychallenge-club-ce74b51ac90c134d77cdfd57a6115adf105662ec.tar.gz perlweeklychallenge-club-ce74b51ac90c134d77cdfd57a6115adf105662ec.tar.bz2 perlweeklychallenge-club-ce74b51ac90c134d77cdfd57a6115adf105662ec.zip | |
Merge pull request #4394 from pauloscustodio/paulo-custodio
Paulo custodio
| -rw-r--r-- | challenge-116/paulo-custodio/bc/ch-2.bc | 52 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/c/ch-1.c | 90 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/c/ch-2.c | 48 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/cpp/ch-1.cpp | 65 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/cpp/ch-2.cpp | 47 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/d/ch_1.d | 62 | ||||
| -rw-r--r-- | challenge-116/paulo-custodio/d/ch_2.d | 46 | ||||
| -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 |
13 files changed, 772 insertions, 0 deletions
diff --git a/challenge-116/paulo-custodio/bc/ch-2.bc b/challenge-116/paulo-custodio/bc/ch-2.bc new file mode 100644 index 0000000000..a55630a75e --- /dev/null +++ b/challenge-116/paulo-custodio/bc/ch-2.bc @@ -0,0 +1,52 @@ +#!/usr/bin/bc -ql + +/* +Challenge 116 + +TASK #2 - Sum of Squares +Submitted by: Mohammad Meraj Zia +You are given a number $N >= 10. + +Write a script to find out if the given number $N is such that sum of squares +of all digits is a perfect square. Print 1 if it is otherwise 0. + +Example +Input: $N = 34 +Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 + +Input: $N = 50 +Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 + +Input: $N = 52 +Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 +*/ + +scale = 10 +num = read() + +define sum_of_squares_is_perfect_square(num) { + if (num < 10) return 0; + + sum = 0; + while (num > 0) { + scale = 0 + digit = num % 10; + num = num / 10; + scale = 10 + sum += digit*digit; +/*print "num ",num," digit ",digit," sum ",sum,"\n"*/ + } + + sqrt_int = sqrt(sum); + scale = 0 + sqrt_int = sqrt_int / 1; + scale = 10 + if (sqrt_int*sqrt_int == sum) { + return 1; + } else { + return 0; + } +} + +sum_of_squares_is_perfect_square(num) +quit diff --git a/challenge-116/paulo-custodio/c/ch-1.c b/challenge-116/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..ff13fd1c11 --- /dev/null +++ b/challenge-116/paulo-custodio/c/ch-1.c @@ -0,0 +1,90 @@ +/* +Challenge 116 + +TASK #1 - Number Sequence +Submitted by: Mohammad S Anwar +You are given a number $N >= 10. + +Write a script to split the given number such that the difference between two +consecutive numbers is always 1 and it shouldn't have leading 0. + +Print the given number if it impossible to split the number. + +Example +Input: $N = 1234 +Output: 1,2,3,4 + +Input: $N = 91011 +Output: 9,10,11 + +Input: $N = 10203 +Output: 10203 as it is impossible to split satisfying the conditions. +*/ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +bool found_solution = false; + +void* check_mem(void* p) { + if (!p) { + fputs("out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +char* substr(const char* str, int start, int len) { + int sublen = strlen(str) - start; + assert(sublen >= 0); + + if (sublen > len) sublen = len; + + char* sub = check_mem(malloc(sublen + 1)); + strncpy(sub, str + start, sublen); + sub[sublen] = '\0'; + return sub; +} + +void print_sequences(int prev, const char* seq, const char* rest) { + if (*rest == '\0') { // consumed all rest + if (!found_solution) { // only first solution printed + printf("%s\n", seq); + found_solution = true; + } + } + else { + for (int i = 1; !found_solution && i <= (int)strlen(rest); i++) { + char* prefix = substr(rest, 0, i); + char* suffix = substr(rest, i, strlen(rest)); + char* new_seq = check_mem(malloc(strlen(seq) + 1 + strlen(prefix) + 1)); + int new_prev = atoi(prefix); + + if (suffix[0] != '0') { + if (prev < 0) { // first time + sprintf(new_seq, "%s", prefix); + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + else { + if (prev + 1 == new_prev) { + sprintf(new_seq, "%s,%s", seq, prefix); + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + } + } + free(prefix); + free(suffix); + free(new_seq); + } + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + print_sequences(-1, "", argv[1]); +} diff --git a/challenge-116/paulo-custodio/c/ch-2.c b/challenge-116/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..976f1ffe1f --- /dev/null +++ b/challenge-116/paulo-custodio/c/ch-2.c @@ -0,0 +1,48 @@ +/* +Challenge 116 + +TASK #2 - Sum of Squares +Submitted by: Mohammad Meraj Zia +You are given a number $N >= 10. + +Write a script to find out if the given number $N is such that sum of squares +of all digits is a perfect square. Print 1 if it is otherwise 0. + +Example +Input: $N = 34 +Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 + +Input: $N = 50 +Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 + +Input: $N = 52 +Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <math.h> + +bool sum_of_squares_is_perfect_square(int num) { + if (num < 10) return false; + + double sum = 0; + while (num > 0) { + int digit = num % 10; + num /= 10; + sum += digit*digit; + } + + double sqrt_int = floor(sqrt(sum)); + if (sqrt_int*sqrt_int == sum) + return true; + else + return false; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + printf("%d\n", sum_of_squares_is_perfect_square(n) ? 1 : 0); +} diff --git a/challenge-116/paulo-custodio/cpp/ch-1.cpp b/challenge-116/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..34e8882ae9 --- /dev/null +++ b/challenge-116/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,65 @@ +/* +Challenge 116 + +TASK #1 - Number Sequence +Submitted by: Mohammad S Anwar +You are given a number $N >= 10. + +Write a script to split the given number such that the difference between two +consecutive numbers is always 1 and it shouldn't have leading 0. + +Print the given number if it impossible to split the number. + +Example +Input: $N = 1234 +Output: 1,2,3,4 + +Input: $N = 91011 +Output: 9,10,11 + +Input: $N = 10203 +Output: 10203 as it is impossible to split satisfying the conditions. +*/ + +#include <cassert> +#include <iostream> +#include <string> +using namespace std; + +bool found_solution = false; + +void print_sequences(int prev, const string& seq, const string& rest) { + if (rest.empty()) { // consumed all rest + if (!found_solution) { // only first solution printed + cout << seq << endl; + found_solution = true; + } + } + else { + for (int i = 1; !found_solution && i <= static_cast<int>(rest.size()); i++) { + string prefix = rest.substr(0, i); + string suffix = rest.substr(i); + int new_prev = atoi(prefix.c_str()); + + if (suffix[0] != '0') { + if (prev < 0) { // first time + string new_seq = prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + else { + if (prev + 1 == new_prev) { + string new_seq = seq + "," + prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + } + } + } + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + print_sequences(-1, "", argv[1]); +} diff --git a/challenge-116/paulo-custodio/cpp/ch-2.cpp b/challenge-116/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..0215e90188 --- /dev/null +++ b/challenge-116/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,47 @@ +/* +Challenge 116 + +TASK #2 - Sum of Squares +Submitted by: Mohammad Meraj Zia +You are given a number $N >= 10. + +Write a script to find out if the given number $N is such that sum of squares +of all digits is a perfect square. Print 1 if it is otherwise 0. + +Example +Input: $N = 34 +Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 + +Input: $N = 50 +Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 + +Input: $N = 52 +Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 +*/ + +#include <iostream> +#include <cmath> +using namespace std; + +bool sum_of_squares_is_perfect_square(int num) { + if (num < 10) return false; + + double sum = 0; + while (num > 0) { + int digit = num % 10; + num /= 10; + sum += digit*digit; + } + + double sqrt_int = floor(sqrt(sum)); + if (sqrt_int*sqrt_int == sum) + return true; + else + return false; +} + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + cout << (sum_of_squares_is_perfect_square(n) ? 1 : 0) << endl; +} diff --git a/challenge-116/paulo-custodio/d/ch_1.d b/challenge-116/paulo-custodio/d/ch_1.d new file mode 100644 index 0000000000..b041438c7e --- /dev/null +++ b/challenge-116/paulo-custodio/d/ch_1.d @@ -0,0 +1,62 @@ +/* +Challenge 116 + +TASK #1 - Number Sequence +Submitted by: Mohammad S Anwar +You are given a number $N >= 10. + +Write a script to split the given number such that the difference between two +consecutive numbers is always 1 and it shouldn't have leading 0. + +Print the given number if it impossible to split the number. + +Example +Input: $N = 1234 +Output: 1,2,3,4 + +Input: $N = 91011 +Output: 9,10,11 + +Input: $N = 10203 +Output: 10203 as it is impossible to split satisfying the conditions. +*/ + +import std.stdio; +import std.conv; + +bool found_solution = false; + +void print_sequences(int prev, string seq, string rest) { + if (rest == "") { // consumed all rest + if (!found_solution) { // only first solution printed + writeln(seq); + found_solution = true; + } + } + else { + for (int i = 1; !found_solution && i <= rest.length; i++) { + string prefix = rest[0 .. i]; + string suffix = rest[i .. $]; + int new_prev = to!int(prefix); + char suffix_begin = (suffix == "") ? 0 : suffix[0]; + if (suffix_begin != '0') { + if (prev < 0) { // first time + string new_seq = prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + else { + if (prev + 1 == new_prev) { + string new_seq = seq ~ "," ~ prefix; + if (!found_solution) + print_sequences(new_prev, new_seq, suffix); + } + } + } + } + } +} + +void main(string[] args) { + print_sequences(-1, "", args[1]); +} diff --git a/challenge-116/paulo-custodio/d/ch_2.d b/challenge-116/paulo-custodio/d/ch_2.d new file mode 100644 index 0000000000..c8f17d5319 --- /dev/null +++ b/challenge-116/paulo-custodio/d/ch_2.d @@ -0,0 +1,46 @@ +/* +Challenge 116 + +TASK #2 - Sum of Squares +Submitted by: Mohammad Meraj Zia +You are given a number $N >= 10. + +Write a script to find out if the given number $N is such that sum of squares +of all digits is a perfect square. Print 1 if it is otherwise 0. + +Example +Input: $N = 34 +Ouput: 1 as 3^2 + 4^2 => 9 + 16 => 25 => 5^2 + +Input: $N = 50 +Output: 1 as 5^2 + 0^2 => 25 + 0 => 25 => 5^2 + +Input: $N = 52 +Output: 0 as 5^2 + 2^2 => 25 + 4 => 29 +*/ + +import std.stdio; +import std.conv; +import core.stdc.math; + +bool sum_of_squares_is_perfect_square(int num) { + if (num < 10) return false; + + double sum = 0; + while (num > 0) { + int digit = num % 10; + num /= 10; + sum += digit*digit; + } + + double sqrt_int = floor(sqrt(sum)); + if (sqrt_int*sqrt_int == sum) + return true; + else + return false; +} + +void main(string[] args) { + int n = to!int(args[1]); + writeln(sum_of_squares_is_perfect_square(n) ? 1 : 0); +} 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(""); +} |
