diff options
Diffstat (limited to 'challenge-101')
| -rw-r--r-- | challenge-101/paulo-custodio/ada/ch_1.adb | 30 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/ada/ch_2.adb | 22 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-1.bas | 28 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-2.bas | 8 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/c/ch-1.c | 8 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/cpp/ch-1.cpp | 26 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/lua/ch-1.lua | 18 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/lua/ch-2.lua | 10 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/perl/ch-2.pl | 10 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/python/ch-1.py | 24 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/python/ch-2.py | 10 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/t/test-2.yaml | 2 |
13 files changed, 107 insertions, 107 deletions
diff --git a/challenge-101/paulo-custodio/ada/ch_1.adb b/challenge-101/paulo-custodio/ada/ch_1.adb index fd9f20df4c..f1c4071cdb 100644 --- a/challenge-101/paulo-custodio/ada/ch_1.adb +++ b/challenge-101/paulo-custodio/ada/ch_1.adb @@ -1,19 +1,19 @@ -- 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, +-- +-- 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 +-- +-- ‘Tightly’ means the absolute value |M-N| of the difference has to be as small -- as possible. with Ada.Command_Line; with Ada.Strings.Fixed; use Ada.Strings.Fixed; -with Ada.Text_IO; use Ada.Text_IO; +with Ada.Text_IO; use Ada.Text_IO; procedure ch_1 is -- command line arguments @@ -27,7 +27,7 @@ procedure ch_1 is number_rect : array(1 .. CL.Argument_Count, 1 .. CL.Argument_Count) of Integer; width, height : Integer; field_width : Integer := 1; - + -- collect from command line procedure collect_numbers is begin @@ -38,7 +38,7 @@ procedure ch_1 is end if; end loop; end collect_numbers; - + -- get smallest rectangle procedure smallest_rect(width, height : out Integer) is begin @@ -63,7 +63,7 @@ procedure ch_1 is number_rect(i, j) := -1; end loop; end loop; - + idx := number_list'First; row := height; col := 1; @@ -77,7 +77,7 @@ procedure ch_1 is end loop; col := col - 1; row := row - 1; - + -- go North while row >= 1 loop exit when number_rect(row, col) >= 0; @@ -87,7 +87,7 @@ procedure ch_1 is end loop; row := row + 1; col := col - 1; - + -- go West while col >= 1 loop exit when number_rect(row, col) >= 0; @@ -97,7 +97,7 @@ procedure ch_1 is end loop; col := col + 1; row := row + 1; - + -- go South while row <= height loop exit when number_rect(row, col) >= 0; @@ -109,7 +109,7 @@ procedure ch_1 is col := col + 1; end loop; end pack_numbers; - + -- print the rectangle procedure print_rect(width, heigth : Integer) is begin @@ -120,7 +120,7 @@ procedure ch_1 is Put_Line(""); end loop; end print_rect; - + begin collect_numbers; smallest_rect(width, height); diff --git a/challenge-101/paulo-custodio/ada/ch_2.adb b/challenge-101/paulo-custodio/ada/ch_2.adb index f52e3faa33..be2268e7bc 100644 --- a/challenge-101/paulo-custodio/ada/ch_2.adb +++ b/challenge-101/paulo-custodio/ada/ch_2.adb @@ -2,17 +2,17 @@ -- -- TASK #2 › Origin-containing Triangle -- Submitted by: Stuart Little --- You are given three points in the plane, as a list of six co-ordinates: +-- 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 +-- +-- 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. with Ada.Command_Line; with Ada.Strings.Fixed; use Ada.Strings.Fixed; -with Ada.Text_IO; use Ada.Text_IO; +with Ada.Text_IO; use Ada.Text_IO; procedure ch_2 is -- command line arguments @@ -24,21 +24,21 @@ procedure ch_2 is type point is record x, y : Float; end record; - + P : array (1 .. 3) of point; P0 : point; function point_in_triangle(P0, P1, P2, P3 : Point) return Integer is - + function sign(P1, P2, P3 : Point) return Float is begin - return (P1.x - P3.x) * (P2.y - P3.y) + return (P1.x - P3.x) * (P2.y - P3.y) - (P2.x - P3.x) * (P1.y - P3.y); end sign; - + d : array(1..3) of Float; has_neg, has_pos : Boolean; - + begin d(1) := sign(P0, P1, P2); d(2) := sign(P0, P2, P3); @@ -53,7 +53,7 @@ procedure ch_2 is return 0; end if; end point_in_triangle; - + begin -- read points for i in 1 .. 3 loop diff --git a/challenge-101/paulo-custodio/basic/ch-1.bas b/challenge-101/paulo-custodio/basic/ch-1.bas index f958245d3e..e68f212358 100644 --- a/challenge-101/paulo-custodio/basic/ch-1.bas +++ b/challenge-101/paulo-custodio/basic/ch-1.bas @@ -1,14 +1,14 @@ ' 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, +' +' 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 +' +' ‘Tightly’ means the absolute value |M-N| of the difference has to be as small ' as possible. const max_numbers as integer = 100 @@ -19,12 +19,12 @@ dim shared num_width as integer, rect_width as integer, rect_height as integer dim shared num_numbers as integer ' collect from command line -sub collect_numbers() +sub collect_numbers() dim i as integer i = 1 do while command(i)<>"" number_list(i) = val(command(i)) - if len(command(i))>num_width then + if len(command(i))>num_width then num_width = len(command(i)) end if num_numbers = i @@ -56,13 +56,13 @@ sub pack_numbers(rect_width as integer, rect_height as Integer) number_rect(row, col) = -1 next next - + idx = 1 row = rect_height col = 1 do while idx <= num_numbers ' go East - do while col <= rect_width + do while col <= rect_width if number_rect(row, col) >= 0 then exit do number_rect(row, col) = number_list(idx) idx = idx + 1 @@ -70,7 +70,7 @@ sub pack_numbers(rect_width as integer, rect_height as Integer) loop col = col - 1 row = row - 1 - + ' go North do while row >= 1 if number_rect(row, col) >= 0 then exit do @@ -80,7 +80,7 @@ sub pack_numbers(rect_width as integer, rect_height as Integer) loop row = row + 1 col = col - 1 - + ' go West do while col >= 1 if number_rect(row, col) >= 0 then exit do @@ -90,9 +90,9 @@ sub pack_numbers(rect_width as integer, rect_height as Integer) loop col = col + 1 row = row + 1 - + ' go South - do while row <= rect_height + do while row <= rect_height if number_rect(row, col) >= 0 then exit do number_rect(row, col) = number_list(idx) idx = idx + 1 diff --git a/challenge-101/paulo-custodio/basic/ch-2.bas b/challenge-101/paulo-custodio/basic/ch-2.bas index 30c62f9fb6..68b0ad627b 100644 --- a/challenge-101/paulo-custodio/basic/ch-2.bas +++ b/challenge-101/paulo-custodio/basic/ch-2.bas @@ -2,12 +2,12 @@ ' ' TASK #2 › Origin-containing Triangle ' Submitted by: Stuart Little -' You are given three points in the plane, as a list of six co-ordinates: +' 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 +' +' 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. type Point diff --git a/challenge-101/paulo-custodio/c/ch-1.c b/challenge-101/paulo-custodio/c/ch-1.c index 066f1b628b..b2a166312b 100644 --- a/challenge-101/paulo-custodio/c/ch-1.c +++ b/challenge-101/paulo-custodio/c/ch-1.c @@ -6,10 +6,10 @@ 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, +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 +‘Tightly’ means the absolute value |M-N| of the difference has to be as small as possible. */ @@ -60,7 +60,7 @@ int** make_empty_rect(int width, int height) { } void free_rect(int**rect, int height) { - for (int r = 0; r < height; r++) + for (int r = 0; r < height; r++) free(rect[r]); free(rect); } @@ -124,6 +124,6 @@ int main(int argc, char* argv[]) { fputs("Usage: ch-1 N...", stderr); exit(EXIT_FAILURE); } - else + else spiral(--argc, ++argv); } diff --git a/challenge-101/paulo-custodio/cpp/ch-1.cpp b/challenge-101/paulo-custodio/cpp/ch-1.cpp index 9d81d1916e..c16fae2ad2 100644 --- a/challenge-101/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-101/paulo-custodio/cpp/ch-1.cpp @@ -6,10 +6,10 @@ 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, +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 +‘Tightly’ means the absolute value |M-N| of the difference has to be as small as possible. */ @@ -20,7 +20,7 @@ as possible. int numbers_width; std::vector<int> collect_numbers(int argc, char* argv[]) { - std::vector<int> numbers; + 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; @@ -43,20 +43,20 @@ void smallest_rect(int n, int& width, int& height) { } std::vector<std::vector<int>> make_empty_rect(int width, int height) { - std::vector<std::vector<int>> rect; + std::vector<std::vector<int>> rect; for (int r = 0; r < height; r++) { - rect.push_back({}); + 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()); +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; @@ -92,8 +92,8 @@ void fill_spiral(std::vector<std::vector<int>>& rect, } void print_spiral(std::vector<std::vector<int>>& rect) { - int width = static_cast<int>(rect[0].size()); - int height = static_cast<int>(rect.size()); + 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]; @@ -115,6 +115,6 @@ int main(int argc, char* argv[]) { std::cerr << "Usage: ch-1 N..." << std::endl; exit(EXIT_FAILURE); } - else + else spiral(--argc, ++argv); } diff --git a/challenge-101/paulo-custodio/lua/ch-1.lua b/challenge-101/paulo-custodio/lua/ch-1.lua index 7eac6f60f5..602143d1ee 100644 --- a/challenge-101/paulo-custodio/lua/ch-1.lua +++ b/challenge-101/paulo-custodio/lua/ch-1.lua @@ -8,10 +8,10 @@ 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, +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 +‘Tightly’ means the absolute value |M-N| of the difference has to be as small as possible. --]] @@ -44,7 +44,7 @@ function build_empty_rectangle(m, n) local rect = {} for r=1, m do rect[r] = {} - for c=1, n do + for c=1, n do rect[r][c] = "" end end @@ -67,7 +67,7 @@ function spiral(numbers) i = i + 1 c = c + 1 end - c = c - 1 + c = c - 1 r = r - 1 -- go North while r >= 1 do @@ -77,7 +77,7 @@ function spiral(numbers) i = i + 1 r = r - 1 end - r = r + 1 + r = r + 1 c = c - 1 -- go West while c >= 1 do @@ -87,7 +87,7 @@ function spiral(numbers) i = i + 1 c = c - 1 end - c = c + 1 + c = c + 1 r = r + 1 -- go South while r <= m do @@ -97,13 +97,13 @@ function spiral(numbers) i = i + 1 r = r + 1 end - r = r - 1 + r = r - 1 c = c + 1 end - + -- print result for r=1, m do - for c=1, n do + for c=1, n do io.write(rect[r][c]) end io.write("\n") diff --git a/challenge-101/paulo-custodio/lua/ch-2.lua b/challenge-101/paulo-custodio/lua/ch-2.lua index 47a12b657e..54b02aa59d 100644 --- a/challenge-101/paulo-custodio/lua/ch-2.lua +++ b/challenge-101/paulo-custodio/lua/ch-2.lua @@ -5,10 +5,10 @@ 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: +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 +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. @@ -24,12 +24,12 @@ function point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3) local d3 = sign(xp,yp, x3,y3, x1,y1) local has_neg - if (d1 < 0) or (d2 < 0) or (d3 < 0) then + if (d1 < 0) or (d2 < 0) or (d3 < 0) then has_neg = true else has_neg = false end - + local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0) local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0) @@ -40,7 +40,7 @@ function point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3) end end -io.write(point_in_triangle(0,0, +io.write(point_in_triangle(0,0, tonumber(arg[1]), tonumber(arg[2]), tonumber(arg[3]), tonumber(arg[4]), tonumber(arg[5]), tonumber(arg[6])), "\n") diff --git a/challenge-101/paulo-custodio/perl/ch-1.pl b/challenge-101/paulo-custodio/perl/ch-1.pl index b203d06266..f6b8f3097e 100644 --- a/challenge-101/paulo-custodio/perl/ch-1.pl +++ b/challenge-101/paulo-custodio/perl/ch-1.pl @@ -1,16 +1,16 @@ #!/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, +# +# 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 +# +# ‘Tightly’ means the absolute value |M-N| of the difference has to be as small # as possible. use strict; @@ -44,13 +44,13 @@ sub smallest_rect { sub spiral { my($m, $n, @list) = @_; - + # find max width of elements my $width = 1; for (@list) { $width = length($_) if length($_) > $width; } - + # build rectangle my @rect; my $r = $m; my $c = 1; @@ -80,6 +80,6 @@ sub spiral { } $r--; $c++; } - + return @rect; } diff --git a/challenge-101/paulo-custodio/perl/ch-2.pl b/challenge-101/paulo-custodio/perl/ch-2.pl index b5e7a71f64..d9efb89857 100644 --- a/challenge-101/paulo-custodio/perl/ch-2.pl +++ b/challenge-101/paulo-custodio/perl/ch-2.pl @@ -1,15 +1,15 @@ #!/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: +# 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 +# +# 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. use strict; diff --git a/challenge-101/paulo-custodio/python/ch-1.py b/challenge-101/paulo-custodio/python/ch-1.py index 57d08d0847..3f5fd5f79e 100644 --- a/challenge-101/paulo-custodio/python/ch-1.py +++ b/challenge-101/paulo-custodio/python/ch-1.py @@ -1,23 +1,23 @@ #!/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, +# +# 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 +# +# 'Tightly' means the absolute value |M-N| of the difference has to be as small # as possible. import sys import math def spiral(numbers): - + # find max width of elements, convert to int def max_width(numbers): num_width = 1 @@ -46,7 +46,7 @@ def spiral(numbers): # build spiral rectangle num_width = max_width(numbers) m, n = build_rect(numbers) - + r, c = m, 1 i = 0 while (i < len(numbers)): @@ -57,7 +57,7 @@ def spiral(numbers): rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) i += 1 c += 1 - c -= 1 + c -= 1 r -= 1 # go North while (r >= 1): @@ -66,7 +66,7 @@ def spiral(numbers): rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) i += 1 r -= 1 - r += 1 + r += 1 c -= 1 # go West while (c >= 1): @@ -75,7 +75,7 @@ def spiral(numbers): rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) i += 1 c -= 1 - c += 1 + c += 1 r += 1 # go South while (r <= m): @@ -84,7 +84,7 @@ def spiral(numbers): rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) i += 1 r += 1 - r -= 1 + r -= 1 c += 1 for r in range (1, m+1): diff --git a/challenge-101/paulo-custodio/python/ch-2.py b/challenge-101/paulo-custodio/python/ch-2.py index 0e510d9eca..b706fb9636 100644 --- a/challenge-101/paulo-custodio/python/ch-2.py +++ b/challenge-101/paulo-custodio/python/ch-2.py @@ -1,15 +1,15 @@ #! /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: +# 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 +# +# 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. import sys diff --git a/challenge-101/paulo-custodio/t/test-2.yaml b/challenge-101/paulo-custodio/t/test-2.yaml index a90cb7c6ef..c4e3e91391 100644 --- a/challenge-101/paulo-custodio/t/test-2.yaml +++ b/challenge-101/paulo-custodio/t/test-2.yaml @@ -1,6 +1,6 @@ - setup: cleanup: - args: 0 1 1 0 2 2 + args: 0 1 1 0 2 2 input: output: 0 - setup: |
