Skip to content

Commit 4dfa49b

Browse files
sync rectangles (#562)
1 parent 06e8124 commit 4dfa49b

File tree

2 files changed

+104
-45
lines changed

2 files changed

+104
-45
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local map = function(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function render_strings(strings)
10+
return table.concat(map(strings, function(row)
11+
return "'" .. row .. "', --"
12+
end), ',\n ')
13+
end
14+
15+
return {
16+
module_name = 'rectangles',
17+
18+
generate_test = function(case)
19+
local template = [[
20+
assert.equal(%s, rectangles.count({
21+
%s
22+
}))]]
23+
24+
return template:format(case.expected, render_strings(case.input.strings))
25+
end
26+
}

exercises/practice/rectangles/rectangles_spec.lua

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,112 @@
11
local rectangles = require('rectangles')
22

3-
-- LuaFormatter off
43
describe('rectangles', function()
5-
it('should find 0 rectangles in an empty grid', function()
4+
it('no rows', function()
65
assert.equal(0, rectangles.count({}))
76
end)
87

9-
it('should find 0 rectangles in a non-empty grid', function()
8+
it('no columns', function()
109
assert.equal(0, rectangles.count({
11-
' ',
12-
' '
10+
'' --
1311
}))
1412
end)
1513

16-
it('should find a single rectangle', function()
14+
it('no rectangles', function()
15+
assert.equal(0, rectangles.count({
16+
' ' --
17+
}))
18+
end)
19+
20+
it('one rectangle', function()
1721
assert.equal(1, rectangles.count({
18-
'+-+',
19-
'| |',
20-
'+-+'
22+
'+-+', -- ,
23+
'| |', -- ,
24+
'+-+' --
2125
}))
2226
end)
2327

24-
it('should find connected rectangles', function()
28+
it('two rectangles without shared parts', function()
2529
assert.equal(2, rectangles.count({
26-
' +-+',
27-
' | |',
28-
'+-+-+',
29-
'| | ',
30-
'+-+ '
30+
' +-+', -- ,
31+
' | |', -- ,
32+
'+-+-+', -- ,
33+
'| | ', -- ,
34+
'+-+ ' --
3135
}))
3236
end)
3337

34-
it('should find nested rectangles', function()
38+
it('five rectangles with shared parts', function()
3539
assert.equal(5, rectangles.count({
36-
' +-+',
37-
' | |',
38-
'+-+-+',
39-
'| | |',
40-
'+-+-+'
40+
' +-+', -- ,
41+
' | |', -- ,
42+
'+-+-+', -- ,
43+
'| | |', -- ,
44+
'+-+-+' --
45+
}))
46+
end)
47+
48+
it('rectangle of height 1 is counted', function()
49+
assert.equal(1, rectangles.count({
50+
'+--+', -- ,
51+
'+--+' --
4152
}))
4253
end)
4354

44-
it('should not count incomplete rectangles', function()
55+
it('rectangle of width 1 is counted', function()
4556
assert.equal(1, rectangles.count({
46-
' +-+',
47-
' |',
48-
'+-+-+',
49-
'| | -',
50-
'+-+-+'
57+
'++', -- ,
58+
'||', -- ,
59+
'++' --
60+
}))
61+
end)
62+
63+
it('1x1 square is counted', function()
64+
assert.equal(1, rectangles.count({
65+
'++', -- ,
66+
'++' --
67+
}))
68+
end)
69+
70+
it('only complete rectangles are counted', function()
71+
assert.equal(1, rectangles.count({
72+
' +-+', -- ,
73+
' |', -- ,
74+
'+-+-+', -- ,
75+
'| | -', -- ,
76+
'+-+-+' --
77+
}))
78+
end)
79+
80+
it('rectangles can be of different sizes', function()
81+
assert.equal(3, rectangles.count({
82+
'+------+----+', -- ,
83+
'| | |', -- ,
84+
'+---+--+ |', -- ,
85+
'| | |', -- ,
86+
'+---+-------+' --
5187
}))
5288
end)
5389

54-
it('should not count rectangles without 4 corners', function()
90+
it('corner is required for a rectangle to be complete', function()
5591
assert.equal(2, rectangles.count({
56-
'+------+----+',
57-
'| | |',
58-
'+------+ |',
59-
'| | |',
60-
'+---+-------+'
92+
'+------+----+', -- ,
93+
'| | |', -- ,
94+
'+------+ |', -- ,
95+
'| | |', -- ,
96+
'+---+-------+' --
6197
}))
6298
end)
6399

64-
it('should find rectangles in large input', function()
100+
it('large input with many rectangles', function()
65101
assert.equal(60, rectangles.count({
66-
' ',
67-
' +---+--+----+ ',
68-
' | +--+----+ ',
69-
' +---+--+ | ',
70-
' | +--+----+ ',
71-
' +---+--+--+-+ ',
72-
' +---+--+--+-+ ',
73-
' +------+ | | ',
74-
' +-+ ',
75-
' '
102+
'+---+--+----+', -- ,
103+
'| +--+----+', -- ,
104+
'+---+--+ |', -- ,
105+
'| +--+----+', -- ,
106+
'+---+--+--+-+', -- ,
107+
'+---+--+--+-+', -- ,
108+
'+------+ | |', -- ,
109+
' +-+' --
76110
}))
77111
end)
78112
end)
79-
-- LuaFormatter on

0 commit comments

Comments
 (0)