diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-07 07:01:41 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-07 07:01:41 +0000 |
| commit | a93b6ae2bca9e39ac76b4c5e2879b349940731b0 (patch) | |
| tree | 5dcb6ba807159563e1fd082c81694a670479c29c /challenge-101 | |
| parent | b36412240457bb8e41e5fdc7c912b084a3afd4df (diff) | |
| parent | 6ba83654820bad00ba3c679bbe18764269dabe0f (diff) | |
| download | perlweeklychallenge-club-a93b6ae2bca9e39ac76b4c5e2879b349940731b0.tar.gz perlweeklychallenge-club-a93b6ae2bca9e39ac76b4c5e2879b349940731b0.tar.bz2 perlweeklychallenge-club-a93b6ae2bca9e39ac76b4c5e2879b349940731b0.zip | |
Merge pull request #3663 from pauloscustodio/paulo-custodio
Paulo Custodio
Diffstat (limited to 'challenge-101')
| -rw-r--r-- | challenge-101/paulo-custodio/ada/ch_1.adb | 7 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/ada/ch_2.adb | 2 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-1.bas | 5 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-2.bas | 2 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/c/ch-1.c | 129 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/c/ch-2.c | 56 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/cpp/ch-1.cpp | 120 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/cpp/ch-2.cpp | 54 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/lua/ch-1.lua | 5 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/lua/ch-2.lua | 2 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/perl/ch-1.pl | 5 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/python/ch-1.py | 5 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/python/ch-2.py | 2 |
14 files changed, 395 insertions, 1 deletions
diff --git a/challenge-101/paulo-custodio/ada/ch_1.adb b/challenge-101/paulo-custodio/ada/ch_1.adb index 825cb53996..fd9f20df4c 100644 --- a/challenge-101/paulo-custodio/ada/ch_1.adb +++ b/challenge-101/paulo-custodio/ada/ch_1.adb @@ -1,4 +1,9 @@ --- You are given an array @A of items (integers say, but they can be anything). +-- Challenge 101 +-- +-- TASK #1 Pack a Spiral +-- Submitted by: Stuart Little +-- +-- -- You are given an array @A of items (integers say, but they can be anything). -- -- Your task is to pack that array into an MxN matrix spirally counterclockwise, -- as tightly as possible. diff --git a/challenge-101/paulo-custodio/ada/ch_2.adb b/challenge-101/paulo-custodio/ada/ch_2.adb index 37a6384012..f52e3faa33 100644 --- a/challenge-101/paulo-custodio/ada/ch_2.adb +++ b/challenge-101/paulo-custodio/ada/ch_2.adb @@ -1,3 +1,5 @@ +-- Challenge 101 +-- -- TASK #2 Origin-containing Triangle -- Submitted by: Stuart Little -- You are given three points in the plane, as a list of six co-ordinates: diff --git a/challenge-101/paulo-custodio/basic/ch-1.bas b/challenge-101/paulo-custodio/basic/ch-1.bas index 948f35eea7..f958245d3e 100644 --- a/challenge-101/paulo-custodio/basic/ch-1.bas +++ b/challenge-101/paulo-custodio/basic/ch-1.bas @@ -1,3 +1,8 @@ +' Challenge 101 +' +' TASK #1 Pack a Spiral +' Submitted by: Stuart Little +' ' You are given an array @A of items (integers say, but they can be anything). ' ' Your task is to pack that array into an MxN matrix spirally counterclockwise, diff --git a/challenge-101/paulo-custodio/basic/ch-2.bas b/challenge-101/paulo-custodio/basic/ch-2.bas index 9a54875dc4..30c62f9fb6 100644 --- a/challenge-101/paulo-custodio/basic/ch-2.bas +++ b/challenge-101/paulo-custodio/basic/ch-2.bas @@ -1,3 +1,5 @@ +' Challenge 101 +' ' TASK #2 Origin-containing Triangle ' Submitted by: Stuart Little ' You are given three points in the plane, as a list of six co-ordinates: diff --git a/challenge-101/paulo-custodio/c/ch-1.c b/challenge-101/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..066f1b628b --- /dev/null +++ b/challenge-101/paulo-custodio/c/ch-1.c @@ -0,0 +1,129 @@ +/* +Challenge 101 + +TASK #1 Pack a Spiral +Submitted by: Stuart Little + +You are given an array @A of items (integers say, but they can be anything). + +Your task is to pack that array into an MxN matrix spirally counterclockwise, +as tightly as possible. + +Tightly means the absolute value |M-N| of the difference has to be as small +as possible. +*/ + +#include <stdio.h> +#include <stdlib.h> + +int numbers_width; + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int* collect_numbers(int argc, char* argv[]) { + int* numbers = check_mem(malloc(argc * sizeof(int))); + for (int i = 0; i < argc; i++) { + numbers[i] = atoi(argv[i]); + if (numbers[i] >= 1000 && numbers_width < 4) numbers_width = 4; + else if (numbers[i] >= 100 && numbers_width < 3) numbers_width = 3; + else if (numbers[i] >= 10 && numbers_width < 2) numbers_width = 2; + else if (numbers_width < 1) numbers_width = 1; + } + return numbers; +} + +void smallest_rect(int n, int* width, int* height) { + *width = 1; *height = n; + for (int i = 1; i <= n; i++) { + if ((n % i) == 0) { + *width = i; *height = n / i; + if (*width >= *height) + break; + } + } +} + +int** make_empty_rect(int width, int height) { + int**rect = check_mem(malloc(height * sizeof(int*))); + for (int r = 0; r < height; r++) { + rect[r] = check_mem(malloc(width * sizeof(int))); + for (int c = 0; c < width; c++) + rect[r][c] = -1; + } + return rect; +} + +void free_rect(int**rect, int height) { + for (int r = 0; r < height; r++) + free(rect[r]); + free(rect); +} + +void fill_spiral(int width, int height, int** rect, int count, int* numbers) { + int i = 0; + int r = height - 1; + int c = 0; + while (i < count) { + // go East + while (c < width && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + c++; + } + c--; r--; + + // go North + while (r >= 0 && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + r--; + } + r++; c--; + + // go West + while (c >= 0 && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + c--; + } + c++; r++; + + // go South + while (r < height && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + r++; + } + r--; c++; + } +} + +void print_spiral(int width, int height, int** rect) { + for (int r = 0; r < height; r++) { + for (int c = 0; c < width; c++) + printf("%*d", numbers_width + 1, rect[r][c]); + putchar('\n'); + } +} + +void spiral(int count, char* nums[]) { + int* numbers = collect_numbers(count, nums); + int height, width; + smallest_rect(count, &width, &height); + int** rect = make_empty_rect(width, height); + fill_spiral(width, height, rect, count, numbers); + print_spiral(width, height, rect); + free(numbers); + free_rect(rect, height); +} + +int main(int argc, char* argv[]) { + if (argc == 1) { + fputs("Usage: ch-1 N...", stderr); + exit(EXIT_FAILURE); + } + else + spiral(--argc, ++argv); +} diff --git a/challenge-101/paulo-custodio/c/ch-2.c b/challenge-101/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..f7925c1d45 --- /dev/null +++ b/challenge-101/paulo-custodio/c/ch-2.c @@ -0,0 +1,56 @@ +/* +Challenge 101 + +TASK #2 Origin-containing Triangle +Submitted by: Stuart Little +You are given three points in the plane, as a list of six co-ordinates: +A=(x1,y1), B=(x2,y2) and C=(x3,y3). + +Write a script to find out if the triangle formed by the given three +co-ordinates contain origin (0,0). + +Print 1 if found otherwise 0. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +typedef struct Point { + double x, y; +} Point; + +double sign(Point p1, Point p2, Point p3) { + return (p1.x - p3.x) * (p2.y - p3.y) + - (p2.x - p3.x) * (p1.y - p3.y); +} + +int point_in_triangle(Point p0, Point p1, Point p2, Point p3) { + double d1 = sign(p0, p1, p2); + double d2 = sign(p0, p2, p3); + double d3 = sign(p0, p3, p1); + + bool has_neg = (d1 < 0.0) || (d2 < 0.0) || (d3 < 0.0); + bool has_pos = (d1 > 0.0) || (d2 > 0.0) || (d3 > 0.0); + + if (!(has_neg && has_pos)) + return 1; + else + return 0; +} + +int main(int argc, char* argv[]) { + if (argc != 7) { + fputs("Usage: ch-2 x1 y1 x2 y2 x3 y3", stderr); + exit(EXIT_FAILURE); + } + else { + Point p[3], p0; + for (int i = 0; i < 3; i++) { + p[i].x = atof(argv[i * 2 + 1]); + p[i].y = atof(argv[i * 2 + 2]); + } + p0.x = p0.y = 0.0; + printf("%d\n", point_in_triangle(p0, p[0], p[1], p[2])); + } +} diff --git a/challenge-101/paulo-custodio/cpp/ch-1.cpp b/challenge-101/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..9d81d1916e --- /dev/null +++ b/challenge-101/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,120 @@ +/* +Challenge 101 + +TASK #1 Pack a Spiral +Submitted by: Stuart Little + +You are given an array @A of items (integers say, but they can be anything). + +Your task is to pack that array into an MxN matrix spirally counterclockwise, +as tightly as possible. + +Tightly means the absolute value |M-N| of the difference has to be as small +as possible. +*/ + +#include <iostream> +#include <iomanip> +#include <vector> + +int numbers_width; + +std::vector<int> collect_numbers(int argc, char* argv[]) { + std::vector<int> numbers; + for (int i = 0; i < argc; i++) { + numbers.push_back(atoi(argv[i])); + if (numbers[i] >= 1000 && numbers_width < 4) numbers_width = 4; + else if (numbers[i] >= 100 && numbers_width < 3) numbers_width = 3; + else if (numbers[i] >= 10 && numbers_width < 2) numbers_width = 2; + else if (numbers_width < 1) numbers_width = 1; + } + return numbers; +} + +void smallest_rect(int n, int& width, int& height) { + width = 1; height = n; + for (int i = 1; i <= n; i++) { + if ((n % i) == 0) { + width = i; height = n / i; + if (width >= height) + break; + } + } +} + +std::vector<std::vector<int>> make_empty_rect(int width, int height) { + std::vector<std::vector<int>> rect; + for (int r = 0; r < height; r++) { + rect.push_back({}); + for (int c = 0; c < width; c++) + rect[r].push_back(-1); + } + return rect; +} + +void fill_spiral(std::vector<std::vector<int>>& rect, + std::vector<int>& numbers) { + int width = static_cast<int>(rect[0].size()); + int height = static_cast<int>(rect.size()); + int count = static_cast<int>(numbers.size()); + int i = 0; + int r = height - 1; + int c = 0; + while (i < count) { + // go East + while (c < width && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + c++; + } + c--; r--; + + // go North + while (r >= 0 && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + r--; + } + r++; c--; + + // go West + while (c >= 0 && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + c--; + } + c++; r++; + + // go South + while (r < height && rect[r][c] < 0) { + rect[r][c] = numbers[i++]; + r++; + } + r--; c++; + } +} + +void print_spiral(std::vector<std::vector<int>>& rect) { + int width = static_cast<int>(rect[0].size()); + int height = static_cast<int>(rect.size()); + for (int r = 0; r < height; r++) { + for (int c = 0; c < width; c++) + std::cout << std::setw(numbers_width + 1) << rect[r][c]; + std::cout << std::endl; + } +} + +void spiral(int count, char* nums[]) { + std::vector<int> numbers = collect_numbers(count, nums); + int height, width; + smallest_rect(count, width, height); + std::vector<std::vector<int>> rect = make_empty_rect(width, height); + fill_spiral(rect, numbers); + print_spiral(rect); +} + +int main(int argc, char* argv[]) { + if (argc == 1) { + std::cerr << "Usage: ch-1 N..." << std::endl; + exit(EXIT_FAILURE); + } + else + spiral(--argc, ++argv); +} diff --git a/challenge-101/paulo-custodio/cpp/ch-2.cpp b/challenge-101/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..0f1ce5bceb --- /dev/null +++ b/challenge-101/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,54 @@ +/* +Challenge 101 + +TASK #2 Origin-containing Triangle +Submitted by: Stuart Little +You are given three points in the plane, as a list of six co-ordinates: +A=(x1,y1), B=(x2,y2) and C=(x3,y3). + +Write a script to find out if the triangle formed by the given three +co-ordinates contain origin (0,0). + +Print 1 if found otherwise 0. +*/ + +#include <iostream> + +typedef struct Point { + double x, y; +} Point; + +double sign(Point p1, Point p2, Point p3) { + return (p1.x - p3.x) * (p2.y - p3.y) + - (p2.x - p3.x) * (p1.y - p3.y); +} + +int point_in_triangle(Point p0, Point p1, Point p2, Point p3) { + double d1 = sign(p0, p1, p2); + double d2 = sign(p0, p2, p3); + double d3 = sign(p0, p3, p1); + + bool has_neg = (d1 < 0.0) || (d2 < 0.0) || (d3 < 0.0); + bool has_pos = (d1 > 0.0) || (d2 > 0.0) || (d3 > 0.0); + + if (!(has_neg && has_pos)) + return 1; + else + return 0; +} + +int main(int argc, char* argv[]) { + if (argc != 7) { + std::cerr << "Usage: ch-2 x1 y1 x2 y2 x3 y3" << std::endl; + exit(EXIT_FAILURE); + } + else { + Point p[3], p0; + for (int i = 0; i < 3; i++) { + p[i].x = atof(argv[i * 2 + 1]); + p[i].y = atof(argv[i * 2 + 2]); + } + p0.x = p0.y = 0.0; + std::cout << point_in_triangle(p0, p[0], p[1], p[2]) << std::endl; + } +} diff --git a/challenge-101/paulo-custodio/lua/ch-1.lua b/challenge-101/paulo-custodio/lua/ch-1.lua index 168993f879..7eac6f60f5 100644 --- a/challenge-101/paulo-custodio/lua/ch-1.lua +++ b/challenge-101/paulo-custodio/lua/ch-1.lua @@ -1,6 +1,11 @@ #!/usr/bin/env lua --[[ +Challenge 101 + +TASK #1 Pack a Spiral +Submitted by: Stuart Little + You are given an array @A of items (integers say, but they can be anything). Your task is to pack that array into an MxN matrix spirally counterclockwise, diff --git a/challenge-101/paulo-custodio/lua/ch-2.lua b/challenge-101/paulo-custodio/lua/ch-2.lua index a748e1bd02..47a12b657e 100644 --- a/challenge-101/paulo-custodio/lua/ch-2.lua +++ b/challenge-101/paulo-custodio/lua/ch-2.lua @@ -1,6 +1,8 @@ #!/usr/bin/env lua --[[ +Challenge 101 + TASK #2 Origin-containing Triangle Submitted by: Stuart Little You are given three points in the plane, as a list of six co-ordinates: diff --git a/challenge-101/paulo-custodio/perl/ch-1.pl b/challenge-101/paulo-custodio/perl/ch-1.pl index 3e637254fb..b203d06266 100644 --- a/challenge-101/paulo-custodio/perl/ch-1.pl +++ b/challenge-101/paulo-custodio/perl/ch-1.pl @@ -1,5 +1,10 @@ #!/usr/bin/perl +# Challenge 101 +# +# TASK #1 Pack a Spiral +# Submitted by: Stuart Little +# # You are given an array @A of items (integers say, but they can be anything). # # Your task is to pack that array into an MxN matrix spirally counterclockwise, diff --git a/challenge-101/paulo-custodio/perl/ch-2.pl b/challenge-101/paulo-custodio/perl/ch-2.pl index c95d924af1..b5e7a71f64 100644 --- a/challenge-101/paulo-custodio/perl/ch-2.pl +++ b/challenge-101/paulo-custodio/perl/ch-2.pl @@ -1,5 +1,7 @@ #!/usr/bin/perl +# Challenge 101 +# # TASK #2 Origin-containing Triangle # Submitted by: Stuart Little # You are given three points in the plane, as a list of six co-ordinates: diff --git a/challenge-101/paulo-custodio/python/ch-1.py b/challenge-101/paulo-custodio/python/ch-1.py index ff71f11953..57d08d0847 100644 --- a/challenge-101/paulo-custodio/python/ch-1.py +++ b/challenge-101/paulo-custodio/python/ch-1.py @@ -1,5 +1,10 @@ #!/usr/bin/env python +# Challenge 101 +# +# TASK #1 > Pack a Spiral +# Submitted by: Stuart Little +# # You are given an array @A of items (integers say, but they can be anything). # # Your task is to pack that array into an MxN matrix spirally counterclockwise, diff --git a/challenge-101/paulo-custodio/python/ch-2.py b/challenge-101/paulo-custodio/python/ch-2.py index 1ea5f422f7..0e510d9eca 100644 --- a/challenge-101/paulo-custodio/python/ch-2.py +++ b/challenge-101/paulo-custodio/python/ch-2.py @@ -1,5 +1,7 @@ #! /usr/bin/env python +# Challenge 101 +# # TASK #2 > Origin-containing Triangle # Submitted by: Stuart Little # You are given three points in the plane, as a list of six co-ordinates: |
