Skip to content

Commit 8dc4e9c

Browse files
authored
Update sublist tests (#583)
* Update sublist tests * Update template to make sure it matches the new signature
1 parent 6a3294e commit 8dc4e9c

File tree

5 files changed

+98
-32
lines changed

5 files changed

+98
-32
lines changed
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
return function(sublist, list)
2-
if #sublist == 0 then
1+
local function is_sublist(l1, l2)
2+
if #l1 == 0 then
33
return true
44
end
55

6-
for i = 1, #list - #sublist + 1 do
7-
for j = 1, #sublist do
8-
if list[i + j - 1] ~= sublist[j] then
6+
for i = 1, #l2 - #l1 + 1 do
7+
for j = 1, #l1 do
8+
if l2[i + j - 1] ~= l1[j] then
99
break
1010
end
11-
if j == #sublist then
11+
if j == #l1 then
1212
return true
1313
end
1414
end
1515
end
1616

1717
return false
1818
end
19+
20+
return function(l1, l2)
21+
local l1_sublist = is_sublist(l1, l2)
22+
local l2_sublist = is_sublist(l2, l1)
23+
24+
if l1_sublist and l2_sublist then
25+
return 'equal'
26+
elseif l1_sublist then
27+
return 'sublist'
28+
elseif l2_sublist then
29+
return 'superlist'
30+
else
31+
return 'unequal'
32+
end
33+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
return {
2+
module_name = 'sublist',
3+
4+
generate_test = function(case)
5+
local template = [[
6+
assert.equal('%s', %s({ %s }, { %s }))]]
7+
return template:format(case.expected, case.property, table.concat(case.input.listOne, ', '),
8+
table.concat(case.input.listTwo, ', '))
9+
end
10+
}

exercises/practice/sublist/.meta/tests.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
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.
411

512
[97319c93-ebc5-47ab-a022-02a1980e1d29]
613
description = "empty lists"
@@ -47,6 +54,9 @@ description = "first list missing element from second list"
4754
[83ffe6d8-a445-4a3c-8795-1e51a95e65c3]
4855
description = "second list missing element from first list"
4956

57+
[7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89]
58+
description = "first list missing additional digits from second list"
59+
5060
[0d7ee7c1-0347-45c8-9ef5-b88db152b30b]
5161
description = "order matters to a list"
5262

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
return function(sublist, list)
1+
return function(l1, l2)
22
end
Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,75 @@
1-
local is_sublist = require('sublist')
1+
local sublist = require('sublist')
22

33
describe('sublist', function()
4-
it('should consider an empty list to be a sublist of an empty list', function()
5-
assert.equal(true, is_sublist({}, {}))
4+
it('empty lists', function()
5+
assert.equal('equal', sublist({}, {}))
66
end)
77

8-
it('should consider an empty list to be a sublist of a non-empty list', function()
9-
assert.equal(true, is_sublist({}, { 1, 2, 3 }))
8+
it('empty list within non empty list', function()
9+
assert.equal('sublist', sublist({}, { 1, 2, 3 }))
1010
end)
1111

12-
it('should consider a list to be a sublist of itself', function()
13-
assert.equal(true, is_sublist({ 1, 2, 3 }, { 1, 2, 3 }))
12+
it('non empty list contains empty list', function()
13+
assert.equal('superlist', sublist({ 1, 2, 3 }, {}))
1414
end)
1515

16-
it('should not consider a subset to be a sublist', function()
17-
assert.equal(false, is_sublist({ 1, 2, 3 }, { 2, 1, 3 }))
16+
it('list equals itself', function()
17+
assert.equal('equal', sublist({ 1, 2, 3 }, { 1, 2, 3 }))
1818
end)
1919

20-
it('should find a sublist at the beginning of a list', function()
21-
assert.equal(true, is_sublist({ 11, 22, 33 }, { 11, 22, 33, 44, 55 }))
20+
it('different lists', function()
21+
assert.equal('unequal', sublist({ 1, 2, 3 }, { 2, 3, 4 }))
2222
end)
2323

24-
it('should find a sublist in the middle of a list', function()
25-
assert.equal(true, is_sublist({ 12, 13, 14 }, { 11, 12, 13, 14, 15 }))
24+
it('false start', function()
25+
assert.equal('sublist', sublist({ 1, 2, 5 }, { 0, 1, 2, 3, 1, 2, 5, 6 }))
2626
end)
2727

28-
it('should find a sublist at the end of a list', function()
29-
assert.equal(true, is_sublist({ 30, 40, 50 }, { 10, 20, 30, 40, 50 }))
28+
it('consecutive', function()
29+
assert.equal('sublist', sublist({ 1, 1, 2 }, { 0, 1, 1, 1, 2, 1, 2 }))
3030
end)
3131

32-
it('should be able to determine when a list is not a sublist', function()
33-
assert.equal(false, is_sublist({ 1, 2, 3 }, { 5, 6, 7, 8, 9 }))
32+
it('sublist at start', function()
33+
assert.equal('sublist', sublist({ 0, 1, 2 }, { 0, 1, 2, 3, 4, 5 }))
3434
end)
3535

36-
it('should not consider almost sublists to be sublists', function()
37-
assert.equal(false, is_sublist({ 3, 4, 5 }, { 1, 2, 4, 5, 6 }))
38-
assert.equal(false, is_sublist({ 3, 4, 5 }, { 1, 2, 3, 4, 6 }))
36+
it('sublist in middle', function()
37+
assert.equal('sublist', sublist({ 2, 3, 4 }, { 0, 1, 2, 3, 4, 5 }))
3938
end)
4039

41-
it('should find a sublist when there are multiple instances of the sublist', function()
42-
assert.equal(true, is_sublist({ 1, 2, 3 }, { 0, 1, 2, 3, 4, 1, 2, 3, 6 }))
40+
it('sublist at end', function()
41+
assert.equal('sublist', sublist({ 3, 4, 5 }, { 0, 1, 2, 3, 4, 5 }))
42+
end)
43+
44+
it('at start of superlist', function()
45+
assert.equal('superlist', sublist({ 0, 1, 2, 3, 4, 5 }, { 0, 1, 2 }))
46+
end)
47+
48+
it('in middle of superlist', function()
49+
assert.equal('superlist', sublist({ 0, 1, 2, 3, 4, 5 }, { 2, 3 }))
50+
end)
51+
52+
it('at end of superlist', function()
53+
assert.equal('superlist', sublist({ 0, 1, 2, 3, 4, 5 }, { 3, 4, 5 }))
54+
end)
55+
56+
it('first list missing element from second list', function()
57+
assert.equal('unequal', sublist({ 1, 3 }, { 1, 2, 3 }))
58+
end)
59+
60+
it('second list missing element from first list', function()
61+
assert.equal('unequal', sublist({ 1, 2, 3 }, { 1, 3 }))
62+
end)
63+
64+
it('first list missing additional digits from second list', function()
65+
assert.equal('unequal', sublist({ 1, 2 }, { 1, 22 }))
66+
end)
67+
68+
it('order matters to a list', function()
69+
assert.equal('unequal', sublist({ 1, 2, 3 }, { 3, 2, 1 }))
70+
end)
71+
72+
it('same digits but different numbers', function()
73+
assert.equal('unequal', sublist({ 1, 0, 1 }, { 10, 1 }))
4374
end)
4475
end)

0 commit comments

Comments
 (0)