Skip to content

Commit 53d1906

Browse files
Add rectangles exercise (#83)
1 parent 6d07876 commit 53d1906

File tree

8 files changed

+244
-0
lines changed

8 files changed

+244
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@
529529
"prerequisites": [],
530530
"difficulty": 7
531531
},
532+
{
533+
"slug": "rectangles",
534+
"name": "Rectangles",
535+
"uuid": "93586bb2-3ee5-425f-ba18-ca9f1eecb17a",
536+
"practices": [],
537+
"prerequisites": [],
538+
"difficulty": 7
539+
},
532540
{
533541
"slug": "dominoes",
534542
"name": "Dominoes",
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.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"blurb": "Count the rectangles in an ASCII diagram."
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local def is_vertical_line (strings: [][]u8) (bottom: i64) (top: i64) (left: i64): bool =
2+
let result = loop result = true for row in top...bottom do
3+
result && (strings[row][left] == '|' || strings[row][left] == '+')
4+
in
5+
result
6+
7+
def rectangles [m] [n] (strings: [m][n]u8): i32 =
8+
let result = loop result = 0 for bottom in 1..<m do
9+
let result = loop result = result for right in 1..<n do
10+
if strings[bottom][right] != '+' then result else
11+
let (result, _) = loop (result, top) = (result, bottom - 1) while top >= 0 do
12+
if strings[top][right] == '|' then (result, top - 1) else
13+
if strings[top][right] != '+' then (result, -1) else
14+
let (result, _) = loop (result, left) = (result, right - 1) while left >= 0 do
15+
if is_vertical_line strings bottom top left then (result + 1, left - 1) else
16+
if (strings[bottom][left] == '-' || strings[bottom][left] == '+') && (strings[top][left] == '-' || strings[top][left] == '+') then (result, left - 1) else
17+
(result, -1)
18+
in
19+
(result, top - 1)
20+
in
21+
result
22+
in
23+
result
24+
in
25+
result
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
include = false
15+
16+
[076929ed-27e8-45dc-b14b-08279944dc49]
17+
description = "no columns"
18+
include = false
19+
20+
[0a8abbd1-a0a4-4180-aa4e-65c1b1a073fa]
21+
description = "no rectangles"
22+
23+
[a4ba42e9-4e7f-4973-b7c7-4ce0760ac6cd]
24+
description = "one rectangle"
25+
26+
[ced06550-83da-4d23-98b7-d24152e0db93]
27+
description = "two rectangles without shared parts"
28+
29+
[5942d69a-a07c-41c8-8b93-2d13877c706a]
30+
description = "five rectangles with shared parts"
31+
32+
[82d70be4-ab37-4bf2-a433-e33778d3bbf1]
33+
description = "rectangle of height 1 is counted"
34+
35+
[57f1bc0e-2782-401e-ab12-7c01d8bfc2e0]
36+
description = "rectangle of width 1 is counted"
37+
38+
[ef0bb65c-bd80-4561-9535-efc4067054f9]
39+
description = "1x1 square is counted"
40+
41+
[e1e1d444-e926-4d30-9bf3-7d8ec9a9e330]
42+
description = "only complete rectangles are counted"
43+
44+
[ca021a84-1281-4a56-9b9b-af14113933a4]
45+
description = "rectangles can be of different sizes"
46+
47+
[51f689a7-ef3f-41ae-aa2f-5ea09ad897ff]
48+
description = "corner is required for a rectangle to be complete"
49+
50+
[d78fe379-8c1b-4d3c-bdf7-29bfb6f6dc66]
51+
description = "large input with many rectangles"
52+
53+
[6ef24e0f-d191-46da-b929-4faca24b4cd2]
54+
description = "rectangles must have four sides"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def rectangles (strings: [][]u8): i32 = ???
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import "rectangles"
2+
3+
-- no rectangles
4+
-- ==
5+
-- input { [" "] }
6+
-- output { 0 }
7+
8+
-- one rectangle
9+
-- ==
10+
-- input { ["+-+", "| |", "+-+"] }
11+
-- output { 1 }
12+
13+
-- two rectangles without shared parts
14+
-- ==
15+
-- input { [" +-+", " | |", "+-+-+", "| | ", "+-+ "] }
16+
-- output { 2 }
17+
18+
-- five rectangles with shared parts
19+
-- ==
20+
-- input { [" +-+", " | |", "+-+-+", "| | |", "+-+-+"] }
21+
-- output { 5 }
22+
23+
-- rectangle of height 1 is counted
24+
-- ==
25+
-- input { ["+--+", "+--+"] }
26+
-- output { 1 }
27+
28+
-- rectangle of width 1 is counted
29+
-- ==
30+
-- input { ["++", "||", "++"] }
31+
-- output { 1 }
32+
33+
-- 1x1 square is counted
34+
-- ==
35+
-- input { ["++", "++"] }
36+
-- output { 1 }
37+
38+
-- only complete rectangles are counted
39+
-- ==
40+
-- input { [" +-+", " |", "+-+-+", "| | -", "+-+-+"] }
41+
-- output { 1 }
42+
43+
-- rectangles can be of different sizes
44+
-- ==
45+
-- input { ["+------+----+", "| | |", "+---+--+ |", "| | |", "+---+-------+"] }
46+
-- output { 3 }
47+
48+
-- corner is required for a rectangle to be complete
49+
-- ==
50+
-- input { ["+------+----+", "| | |", "+------+ |", "| | |", "+---+-------+"] }
51+
-- output { 2 }
52+
53+
-- large input with many rectangles
54+
-- ==
55+
-- input { ["+---+--+----+", "| +--+----+", "+---+--+ |", "| +--+----+", "+---+--+--+-+", "+---+--+--+-+", "+------+ | |", " +-+"] }
56+
-- output { 60 }
57+
58+
-- rectangles must have four sides
59+
-- ==
60+
-- input { ["+-+ +-+", "| | | |", "+-+-+-+", " | | ", "+-+-+-+", "| | | |", "+-+ +-+"] }
61+
-- output { 5 }
62+
63+
let main (strings: [][]u8): i32 =
64+
rectangles strings

generators/exercises/rectangles.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def gen_test_case(prop, description, inp, expected, f):
2+
strings = str(inp["strings"]).replace("'", '"')
3+
4+
f.write(f"-- {description}\n")
5+
f.write("-- ==\n")
6+
f.write("-- input { " + strings + " }\n")
7+
f.write("-- output { " + str(expected) + " }\n\n")
8+
9+
10+
def gen_main(f):
11+
f.write("let main (strings: [][]u8): i32 =\n")
12+
f.write(" rectangles strings\n")

0 commit comments

Comments
 (0)