Instructions

Test suite

Solution

Given two lists determine if the first list is contained within the second list, if the second list is contained within the first list, if both lists are contained within each other or if none of these are true.

Specifically, a list A is a sublist of list B if by dropping 0 or more elements from the front of B and 0 or more elements from the back of B you get a list that's completely equal to A.

Examples:

- A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B
- A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B
- A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B
- A = [1, 2, 3], B = [1, 2, 3], A is equal to B
- A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B
- A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
local is_sublist = require('sublist')
describe('sublist', function()
it('should consider an empty list to be a sublist of an empty list', function()
assert.equal(true, is_sublist({}, {}))
end)
it('should consider an empty list to be a sublist of a non-empty list', function()
assert.equal(true, is_sublist({}, { 1, 2, 3 }))
end)
it('should consider a list to be a sublist of itself', function()
assert.equal(true, is_sublist({ 1, 2, 3 }, { 1, 2, 3 }))
end)
it('should not consider a subset to be a sublist', function()
assert.equal(false, is_sublist({ 1, 2, 3 }, { 2, 1, 3 }))
end)
it('should find a sublist at the beginning of a list', function()
assert.equal(true, is_sublist({ 11, 22, 33 }, { 11, 22, 33, 44, 55 }))
end)
it('should find a sublist in the middle of a list', function()
assert.equal(true, is_sublist({ 12, 13, 14 }, { 11, 12, 13, 14, 15 }))
end)
it('should find a sublist at the end of a list', function()
assert.equal(true, is_sublist({ 30, 40, 50 }, { 10, 20, 30, 40, 50 }))
end)
it('should be able to determine when a list is not a sublist', function()
assert.equal(false, is_sublist({ 1, 2, 3 }, { 5, 6, 7, 8, 9 }))
end)
it('should not consider almost sublists to be sublists', function()
assert.equal(false, is_sublist({ 3, 4, 5 }, { 1, 2, 4, 5, 6 }))
assert.equal(false, is_sublist({ 3, 4, 5 }, { 1, 2, 3, 4, 6 }))
end)
it('should find a sublist when there are multiple instances of the sublist', function()
assert.equal(true, is_sublist({ 1, 2, 3 }, { 0, 1, 2, 3, 4, 1, 2, 3, 6 }))
end)
end)
```

```
local function make_set(a)
local set = {}
for _, l in ipairs(a) do
set[l] = true
end
return set
end
return function (a, b)
local c = make_set(b)
for _, v in pairs(a) do
if c[v] == nil then return false end
end
return true
end
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,276 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

Great use of Lua tables to minimize search time :)

You are finding a subset here. But {1,2} is not sublist of {2,1}!

@fyrchik you're absolutely right, I didn't notice that before. Looks like this problem is missing a test.