diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-26 23:22:25 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-26 23:37:59 +0000 |
| commit | 2a8d81c5c54d8df1ecdac0e0e069ff8a1ad35a42 (patch) | |
| tree | 53de6f0aa409189cc3795ce0d3526f40296a70cb /challenge-101/paulo-custodio/basic | |
| parent | f7d1df5fd867d379221ad326c79e9c76ec3076c3 (diff) | |
| download | perlweeklychallenge-club-2a8d81c5c54d8df1ecdac0e0e069ff8a1ad35a42.tar.gz perlweeklychallenge-club-2a8d81c5c54d8df1ecdac0e0e069ff8a1ad35a42.tar.bz2 perlweeklychallenge-club-2a8d81c5c54d8df1ecdac0e0e069ff8a1ad35a42.zip | |
Add Basic solution to challenge 101
Diffstat (limited to 'challenge-101/paulo-custodio/basic')
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-1.bas | 116 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/basic/ch-2.bas | 47 |
2 files changed, 163 insertions, 0 deletions
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)))) |
