Skip to content

Commit 94c8fb8

Browse files
Add rectangles exercise
1 parent 26ffba7 commit 94c8fb8

File tree

11 files changed

+1249
-0
lines changed

11 files changed

+1249
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,14 @@
540540
"prerequisites": [],
541541
"difficulty": 5
542542
},
543+
{
544+
"slug": "rectangles",
545+
"name": "Rectangles",
546+
"uuid": "6cd0e27c-7098-4594-b523-727cfd77a9a2",
547+
"practices": [],
548+
"prerequisites": [],
549+
"difficulty": 7
550+
},
543551
{
544552
"slug": "connect",
545553
"name": "Connect",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Instructions
2+
3+
Count the rectangles in an ASCII diagram like the one below.
4+
5+
```text
6+
+--+
7+
++ |
8+
+-++--+
9+
| | |
10+
+--+--+
11+
```
12+
13+
The above diagram contains these 6 rectangles:
14+
15+
```text
16+
17+
18+
+-----+
19+
| |
20+
+-----+
21+
```
22+
23+
```text
24+
+--+
25+
| |
26+
| |
27+
| |
28+
+--+
29+
```
30+
31+
```text
32+
+--+
33+
| |
34+
+--+
35+
36+
37+
```
38+
39+
```text
40+
41+
42+
+--+
43+
| |
44+
+--+
45+
```
46+
47+
```text
48+
49+
50+
+--+
51+
| |
52+
+--+
53+
```
54+
55+
```text
56+
57+
++
58+
++
59+
60+
61+
```
62+
63+
You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"Rectangles.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Count the rectangles in an ASCII diagram."
17+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
unit Rectangles;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TStrArray = Array Of String;
9+
10+
function rectangles(
11+
const strings : TStrArray
12+
) : Integer;
13+
14+
implementation
15+
16+
uses SysUtils;
17+
18+
function rectangles(
19+
const strings : TStrArray
20+
) : Integer;
21+
var
22+
rows : Integer;
23+
columns : Integer;
24+
top : Integer;
25+
left : Integer;
26+
right : Integer;
27+
bottom : Integer;
28+
index : Integer;
29+
begin
30+
result := 0;
31+
rows := length(strings);
32+
if rows = 0 then
33+
exit;
34+
columns := length(strings[0]);
35+
if columns = 0 then
36+
exit;
37+
38+
for top := low(strings) to high(strings) - 1 do
39+
for left := 1 to columns - 1 do
40+
begin
41+
if strings[top][left] = '+' then
42+
begin
43+
right := left + 1;
44+
while (right <= columns) and ((strings[top][right] = '-') or (strings[top][right] = '+')) do
45+
begin
46+
if strings[top][right] = '+' then
47+
begin
48+
bottom := top + 1;
49+
while (bottom < rows) and ((strings[bottom][left] = '|') or (strings[bottom][left] = '+')) and ((strings[bottom][right] = '|') or (strings[bottom][right] = '+')) do
50+
begin
51+
if (strings[bottom][left] = '+') and (strings[bottom][right] = '+') then
52+
begin
53+
index := left + 1;
54+
while (index < right) and ((strings[bottom][index] = '-') or (strings[bottom][index] = '+')) do
55+
index := index + 1;
56+
if index = right then
57+
result := result + 1;
58+
end;
59+
bottom := bottom + 1;
60+
end;
61+
end;
62+
right := right + 1;
63+
end;
64+
end;
65+
end;
66+
end;
67+
68+
end.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[485b7bab-4150-40aa-a8db-73013427d08c]
13+
description = "no rows"
14+
15+
[076929ed-27e8-45dc-b14b-08279944dc49]
16+
description = "no columns"
17+
18+
[0a8abbd1-a0a4-4180-aa4e-65c1b1a073fa]
19+
description = "no rectangles"
20+
21+
[a4ba42e9-4e7f-4973-b7c7-4ce0760ac6cd]
22+
description = "one rectangle"
23+
24+
[ced06550-83da-4d23-98b7-d24152e0db93]
25+
description = "two rectangles without shared parts"
26+
27+
[5942d69a-a07c-41c8-8b93-2d13877c706a]
28+
description = "five rectangles with shared parts"
29+
30+
[82d70be4-ab37-4bf2-a433-e33778d3bbf1]
31+
description = "rectangle of height 1 is counted"
32+
33+
[57f1bc0e-2782-401e-ab12-7c01d8bfc2e0]
34+
description = "rectangle of width 1 is counted"
35+
36+
[ef0bb65c-bd80-4561-9535-efc4067054f9]
37+
description = "1x1 square is counted"
38+
39+
[e1e1d444-e926-4d30-9bf3-7d8ec9a9e330]
40+
description = "only complete rectangles are counted"
41+
42+
[ca021a84-1281-4a56-9b9b-af14113933a4]
43+
description = "rectangles can be of different sizes"
44+
45+
[51f689a7-ef3f-41ae-aa2f-5ea09ad897ff]
46+
description = "corner is required for a rectangle to be complete"
47+
48+
[d78fe379-8c1b-4d3c-bdf7-29bfb6f6dc66]
49+
description = "large input with many rectangles"
50+
51+
[6ef24e0f-d191-46da-b929-4faca24b4cd2]
52+
description = "rectangles must have four sides"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
unit Rectangles;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TStrArray = Array Of String;
9+
10+
function rectangles(
11+
const strings : TStrArray
12+
) : Integer;
13+
14+
implementation
15+
16+
uses SysUtils;
17+
18+
function rectangles(
19+
const strings : TStrArray
20+
) : Integer;
21+
begin
22+
23+
raise ENotImplemented.Create('Please implement your solution.'); result := length(strings);
24+
25+
end;
26+
27+
end.

0 commit comments

Comments
 (0)