diff options
Diffstat (limited to 'challenge-101')
| -rw-r--r-- | challenge-101/paulo-custodio/ada/ch_1.adb | 124 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/ada/ch_2.adb | 65 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-1.bas | 116 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-2.bas | 47 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/lua/ch-1.lua | 108 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/lua/ch-2.lua | 44 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/python/ch-1.py | 93 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/python/ch-2.py | 32 |
8 files changed, 629 insertions, 0 deletions
diff --git a/challenge-101/paulo-custodio/ada/ch_1.adb b/challenge-101/paulo-custodio/ada/ch_1.adb new file mode 100644 index 0000000000..825cb53996 --- /dev/null +++ b/challenge-101/paulo-custodio/ada/ch_1.adb @@ -0,0 +1,124 @@ +-- 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. + +with Ada.Command_Line; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_1 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- integer formatting + package Integer_IO is new Ada.Text_IO.Integer_IO(Integer); + + -- input list + number_list : array(1 .. CL.Argument_Count) of Integer; + 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 + for i in number_list'Range loop + number_list(i) := Integer'Value(CL.Argument(i)); + if CL.Argument(i)'Length > field_width then + field_width := CL.Argument(i)'Length; + end if; + end loop; + end collect_numbers; + + -- get smallest rectangle + procedure smallest_rect(width, height : out Integer) is + begin + width := number_list'First; + height := number_list'Last; + for i in number_list'Range loop + if (number_list'Last mod i) = 0 then + width := i; + height := number_list'Last / i; + exit when width >= height; + end if; + end loop; + end smallest_rect; + + -- pack the numbers in a spiral + procedure pack_numbers(width, heigth : Integer) is + row, col, idx : Integer; + begin + -- fill matrix with -1 - to mark places that are free + for i in number_list'Range loop + for j in number_list'Range loop + number_rect(i, j) := -1; + end loop; + end loop; + + idx := number_list'First; + row := height; + col := 1; + while idx <= number_list'Last loop + -- go East + while col <= width loop + exit when number_rect(row, col) >= 0; + number_rect(row, col) := number_list(idx); + idx := idx + 1; + col := col + 1; + end loop; + col := col - 1; + row := row - 1; + + -- go North + while row >= 1 loop + exit when number_rect(row, col) >= 0; + number_rect(row, col) := number_list(idx); + idx := idx + 1; + row := row - 1; + end loop; + row := row + 1; + col := col - 1; + + -- go West + while col >= 1 loop + exit when number_rect(row, col) >= 0; + number_rect(row, col) := number_list(idx); + idx := idx + 1; + col := col - 1; + end loop; + col := col + 1; + row := row + 1; + + -- go South + while row <= height loop + exit when number_rect(row, col) >= 0; + number_rect(row, col) := number_list(idx); + idx := idx + 1; + row := row + 1; + end loop; + row := row - 1; + col := col + 1; + end loop; + end pack_numbers; + + -- print the rectangle + procedure print_rect(width, heigth : Integer) is + begin + for row in 1 .. height loop + for col in 1 .. width loop + Integer_IO.Put(number_rect(row, col), field_width+1); + end loop; + Put_Line(""); + end loop; + end print_rect; + +begin + collect_numbers; + smallest_rect(width, height); + pack_numbers(width, height); + print_rect(width, height); +end ch_1; diff --git a/challenge-101/paulo-custodio/ada/ch_2.adb b/challenge-101/paulo-custodio/ada/ch_2.adb new file mode 100644 index 0000000000..37a6384012 --- /dev/null +++ b/challenge-101/paulo-custodio/ada/ch_2.adb @@ -0,0 +1,65 @@ +-- 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. + +with Ada.Command_Line; +with Ada.Strings.Fixed; use Ada.Strings.Fixed; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_2 is + -- command line arguments + package CL renames Ada.Command_Line; + + -- integer formatting + package Integer_IO is new Ada.Text_IO.Integer_IO(Integer); + + 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) + - (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); + d(3) := sign(P0, P3, P1); + + has_neg := (d(1) < 0.0) or (d(2) < 0.0) or (d(3) < 0.0); + has_pos := (d(1) > 0.0) or (d(2) > 0.0) or (d(3) > 0.0); + + if not (has_neg and has_pos) then + return 1; + else + return 0; + end if; + end point_in_triangle; + +begin + -- read points + for i in 1 .. 3 loop + P(i).x := Float'Value(CL.Argument(i*2-1)); + P(i).y := Float'Value(CL.Argument(i*2)); + end loop; + P0.x := 0.0; + P0.y := 0.0; + Integer_IO.Put(point_in_triangle(P0, P(1), P(2), P(3)), 0); + Put_Line(""); +end ch_2; diff --git a/challenge-101/paulo-custodio/basic/ch-1.bas b/challenge-101/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..948f35eea7 --- /dev/null +++ b/challenge-101/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,116 @@ +' 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. + +const max_numbers as integer = 100 + +dim shared number_list(max_numbers) as integer +dim shared number_rect(max_numbers,max_numbers) as integer +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() + dim i as integer + i = 1 + do while command(i)<>"" + number_list(i) = val(command(i)) + if len(command(i))>num_width then + num_width = len(command(i)) + end if + num_numbers = i + i = i + 1 + loop +end sub + +' get smallest rectangle +sub smallest_rect(byref rect_width as integer, byref rect_height as integer) + dim i as integer + rect_width = 1 + rect_height = num_numbers + for i=1 to num_numbers + if (num_numbers mod i) = 0 then + rect_width = i + rect_height = int(num_numbers / i) + if rect_width >= rect_height then exit for + end if + next +end sub + +' pack the numbers in a spiral +sub pack_numbers(rect_width as integer, rect_height as Integer) + dim row as integer, col as integer, idx as integer + + ' fill matrix with -1 - to mark places that are free + for row=1 to rect_height + for col=1 to rect_width + 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 + if number_rect(row, col) >= 0 then exit do + number_rect(row, col) = number_list(idx) + idx = idx + 1 + col = col + 1 + loop + col = col - 1 + row = row - 1 + + ' go North + do while row >= 1 + if number_rect(row, col) >= 0 then exit do + number_rect(row, col) = number_list(idx) + idx = idx + 1 + row = row - 1 + loop + row = row + 1 + col = col - 1 + + ' go West + do while col >= 1 + if number_rect(row, col) >= 0 then exit do + number_rect(row, col) = number_list(idx) + idx = idx + 1 + col = col - 1 + loop + col = col + 1 + row = row + 1 + + ' go South + do while row <= rect_height + if number_rect(row, col) >= 0 then exit do + number_rect(row, col) = number_list(idx) + idx = idx + 1 + row = row + 1 + loop + row = row - 1 + col = col + 1 + loop +end sub + +' print the rectangle +sub print_rect(rect_width as integer, rect_height as Integer) + dim row as integer, col as integer + for row=1 to rect_height + for col=1 to rect_width + print right(space(10) & str(number_rect(row, col)), num_width+1); + next + print + next +end sub + +' main +collect_numbers +smallest_rect rect_width, rect_height +pack_numbers rect_width, rect_height +print_rect rect_width, rect_height diff --git a/challenge-101/paulo-custodio/basic/ch-2.bas b/challenge-101/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..9a54875dc4 --- /dev/null +++ b/challenge-101/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,47 @@ +' 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. + +type Point + x as Double + y as Double +end type + +function sign(P1 as Point, P2 as Point, P3 as Point) as Double +sign = (P1.x - P3.x) * (P2.y - P3.y) _ + - (P2.x - P3.x) * (P1.y - P3.y) +end function + +function point_in_triangle(P0 as Point, P1 as Point, P2 as Point, P3 as Point) as Integer + dim d(3) as Double, has_neg as Boolean, has_pos as Boolean + + d(1) = sign(P0, P1, P2) + d(2) = sign(P0, P2, P3) + d(3) = sign(P0, P3, P1) + + has_neg = (d(1) < 0.0) or (d(2) < 0.0) or (d(3) < 0.0) + has_pos = (d(1) > 0.0) or (d(2) > 0.0) or (d(3) > 0.0) + + if not (has_neg and has_pos) then + point_in_triangle = 1 + else + point_in_triangle = 0 + end if +end function + +' main +dim P(3) as Point, P0 as Point, i as Integer + +for i=1 to 3 + P(i).x = val(Command(i*2-1)) + P(i).y = val(Command(i*2)) +next +P0.x = 0.0 +P0.y = 0.0 +print trim(str(point_in_triangle(P0, P(1), P(2), P(3)))) diff --git a/challenge-101/paulo-custodio/lua/ch-1.lua b/challenge-101/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..168993f879 --- /dev/null +++ b/challenge-101/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,108 @@ +#!/usr/bin/env lua + +--[[ +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. +--]] + +-- find out m,n +function smallest_rect(n) + local low = 1 + local high = n + for i=1, math.floor(math.sqrt(n)) do + if math.floor(math.fmod(n, i)) == 0 then + low, high = i, math.floor(n/i) + end + end + return low, high +end + +-- find max width of elements, convert to int +function max_width(numbers) + local num_width = 1 + for i=1, #numbers do + if #numbers[i] < num_width then + num_width = #numbers[i] + numbers[i] = tonumber(numbers[i]) + end + end + return num_width +end + +-- build empty rectangle +function build_empty_rectangle(m, n) + local rect = {} + for r=1, m do + rect[r] = {} + for c=1, n do + rect[r][c] = "" + end + end + return rect +end + +-- build spiral rectangle +function spiral(numbers) + local m, n = smallest_rect(#numbers) + local num_width = max_width(numbers) + local rect = build_empty_rectangle(m, n) + local r, c = m, 1 + local i = 1 + while i <= #numbers do + -- go East + while c <= n do + if rect[r][c] ~= "" then break end + rect[r][c] = string.format("%"..tostring(num_width+1).."d", + numbers[i]) + i = i + 1 + c = c + 1 + end + c = c - 1 + r = r - 1 + -- go North + while r >= 1 do + if rect[r][c] ~= "" then break end + rect[r][c] = string.format("%"..tostring(num_width+1).."d", + numbers[i]) + i = i + 1 + r = r - 1 + end + r = r + 1 + c = c - 1 + -- go West + while c >= 1 do + if rect[r][c] ~= "" then break end + rect[r][c] = string.format("%"..tostring(num_width+1).."d", + numbers[i]) + i = i + 1 + c = c - 1 + end + c = c + 1 + r = r + 1 + -- go South + while r <= m do + if rect[r][c] ~= "" then break end + rect[r][c] = string.format("%"..tostring(num_width+1).."d", + numbers[i]) + i = i + 1 + r = r + 1 + end + r = r - 1 + c = c + 1 + end + + -- print result + for r=1, m do + for c=1, n do + io.write(rect[r][c]) + end + io.write("\n") + end +end + +spiral(arg) diff --git a/challenge-101/paulo-custodio/lua/ch-2.lua b/challenge-101/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..a748e1bd02 --- /dev/null +++ b/challenge-101/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,44 @@ +#!/usr/bin/env lua + +--[[ +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. +--]] + +function sign(x1,y1,x2,y2,x3,y3) + return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3) +end + +function point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3) + local d1 = sign(xp,yp, x1,y1, x2,y2) + local d2 = sign(xp,yp, 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 + 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) + + if has_neg and has_pos then + return 0 + else + return 1 + end +end + +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/python/ch-1.py b/challenge-101/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..ff71f11953 --- /dev/null +++ b/challenge-101/paulo-custodio/python/ch-1.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python + +# 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. + +import sys +import math + +def spiral(numbers): + + # find max width of elements, convert to int + def max_width(numbers): + num_width = 1 + for i in range(0, len(numbers)): + if len(numbers[i]) < num_width: + num_width = len(numbers[i]) + numbers[i] = int(numbers[i]) + return num_width + + # find out m,n + def smallest_rect(n): + l, h = 1, n + for i in range(1, int(math.sqrt(n)+1)): + if ((n % i) == 0): + l, h = i, int(n / i) + return l, h + + # build empty rectangle + rect = [] + def build_rect(numbers): + m, n = smallest_rect(len(numbers)) + for r in range (0, m+1): + rect.append(["" for c in range(0, n+1)]) + return m, n + + # build spiral rectangle + num_width = max_width(numbers) + m, n = build_rect(numbers) + + r, c = m, 1 + i = 0 + while (i < len(numbers)): + # go East + while (c <= n): + if (rect[r][c] != ""): + break + rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) + i += 1 + c += 1 + c -= 1 + r -= 1 + # go North + while (r >= 1): + if (rect[r][c] != ""): + break + rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) + i += 1 + r -= 1 + r += 1 + c -= 1 + # go West + while (c >= 1): + if (rect[r][c] != ""): + break + rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) + i += 1 + c -= 1 + c += 1 + r += 1 + # go South + while (r <= m): + if (rect[r][c] != ""): + break + rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i]) + i += 1 + r += 1 + r -= 1 + c += 1 + + for r in range (1, m+1): + output = "" + for c in range (1, n+1): + output += rect[r][c] + print(output) + +# main +numbers = sys.argv[1:] +spiral(numbers) diff --git a/challenge-101/paulo-custodio/python/ch-2.py b/challenge-101/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1ea5f422f7 --- /dev/null +++ b/challenge-101/paulo-custodio/python/ch-2.py @@ -0,0 +1,32 @@ +#! /usr/bin/env python + +# 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. + +import sys + +def point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3): + def sign(x1,y1,x2,y2,x3,y3): + return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3) + + d1 = sign(xp,yp, x1,y1, x2,y2) + d2 = sign(xp,yp, x2,y2, x3,y3) + d3 = sign(xp,yp, x3,y3, x1,y1) + + has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0) + has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0) + + if (not (has_neg and has_pos)): + return 1 + else: + return 0 + +x1,y1,x2,y2,x3,y3 = (float(sys.argv[i]) for i in range(1, 7)) +print(point_in_triangle(0.0,0.0, x1,y1,x2,y2,x3,y3)) |
