# Edward-Zhou's solution

## to Sublist in the Elixir Track

Published at May 28 2019 · 0 comments
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

## Running tests

Execute the tests with:

``````\$ elixir sublist_test.exs
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## Submitting Incomplete Solutions

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

### sublist_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule SublistTest do
use ExUnit.Case

test "empty equals empty" do
assert Sublist.compare([], []) == :equal
end

@tag :pending
test "empty is a sublist of anything" do
assert Sublist.compare([], [nil]) == :sublist
end

@tag :pending
test "anything is a superlist of empty" do
assert Sublist.compare([nil], []) == :superlist
end

@tag :pending
test "1 is not 2" do
assert Sublist.compare([1], [2]) == :unequal
end

@tag :pending
test "comparing massive equal lists" do
l = Enum.to_list(1..1_000_000)
assert Sublist.compare(l, l) == :equal
end

@tag :pending
test "sublist at start" do
assert Sublist.compare([1, 2, 3], [1, 2, 3, 4, 5]) == :sublist
end

@tag :pending
test "sublist in middle" do
assert Sublist.compare([4, 3, 2], [5, 4, 3, 2, 1]) == :sublist
end

@tag :pending
test "sublist at end" do
assert Sublist.compare([3, 4, 5], [1, 2, 3, 4, 5]) == :sublist
end

@tag :pending
test "partially matching sublist at start" do
assert Sublist.compare([1, 1, 2], [1, 1, 1, 2]) == :sublist
end

@tag :pending
test "sublist early in huge list" do
assert Sublist.compare([3, 4, 5], Enum.to_list(1..1_000_000)) == :sublist
end

@tag :pending
test "huge sublist not in huge list" do
assert Sublist.compare(Enum.to_list(10..1_000_001), Enum.to_list(1..1_000_000)) == :unequal
end

@tag :pending
test "superlist at start" do
assert Sublist.compare([1, 2, 3, 4, 5], [1, 2, 3]) == :superlist
end

@tag :pending
test "superlist in middle" do
assert Sublist.compare([5, 4, 3, 2, 1], [4, 3, 2]) == :superlist
end

@tag :pending
test "superlist at end" do
assert Sublist.compare([1, 2, 3, 4, 5], [3, 4, 5]) == :superlist
end

@tag :pending
test "1 and 2 does not contain 3" do
assert Sublist.compare([1, 2], [3]) == :unequal
end

@tag :pending
test "partially matching superlist at start" do
assert Sublist.compare([1, 1, 1, 2], [1, 1, 2]) == :superlist
end

@tag :pending
test "superlist early in huge list" do
assert Sublist.compare(Enum.to_list(1..1_000_000), [3, 4, 5]) == :superlist
end

@tag :pending
test "strict equality needed" do
assert Sublist.compare([1], [1.0, 2]) == :unequal
end

@tag :pending
test "recurring values sublist" do
assert Sublist.compare([1, 2, 1, 2, 3], [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]) == :sublist
end

@tag :pending
test "recurring values unequal" do
assert Sublist.compare([1, 2, 1, 2, 3], [1, 2, 3, 1, 2, 3, 2, 3, 2, 1]) == :unequal
end
end``````

### sublist.exs

``````defmodule Sublist do
@doc """
Returns whether the first list is a sublist or a superlist of the second list
and if not whether it is equal or unequal to the second list.
"""
def compare(a, b) do
cond do
a === b -> :equal
length(a) < length(b) and sublist?(a, b) -> :sublist
length(a) > length(b) and sublist?(b, a) -> :superlist
true -> :unequal
end
end

def sublist?([], _), do: true
def sublist?(a, b) do
Stream.chunk_every(b, length(a), 1) |> Enum.member?(a)
end
end``````

### sublist_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule SublistTest do
use ExUnit.Case

test "empty equals empty" do
assert Sublist.compare([], []) == :equal
end

test "empty is a sublist of anything" do
assert Sublist.compare([], [nil]) == :sublist
end

test "anything is a superlist of empty" do
assert Sublist.compare([nil], []) == :superlist
end

test "1 is not 2" do
assert Sublist.compare([1], [2]) == :unequal
end

test "comparing massive equal lists" do
l = Enum.to_list(1..1_000_000)
assert Sublist.compare(l, l) == :equal
end

test "sublist at start" do
assert Sublist.compare([1, 2, 3], [1, 2, 3, 4, 5]) == :sublist
end

test "sublist in middle" do
assert Sublist.compare([4, 3, 2], [5, 4, 3, 2, 1]) == :sublist
end

test "sublist at end" do
assert Sublist.compare([3, 4, 5], [1, 2, 3, 4, 5]) == :sublist
end

test "partially matching sublist at start" do
assert Sublist.compare([1, 1, 2], [1, 1, 1, 2]) == :sublist
end

test "sublist early in huge list" do
assert Sublist.compare([3, 4, 5], Enum.to_list(1..1_000_000)) == :sublist
end

test "huge sublist not in huge list" do
assert Sublist.compare(Enum.to_list(10..1_000_001), Enum.to_list(1..1_000_000)) == :unequal
end

test "superlist at start" do
assert Sublist.compare([1, 2, 3, 4, 5], [1, 2, 3]) == :superlist
end

test "superlist in middle" do
assert Sublist.compare([5, 4, 3, 2, 1], [4, 3, 2]) == :superlist
end

test "superlist at end" do
assert Sublist.compare([1, 2, 3, 4, 5], [3, 4, 5]) == :superlist
end

test "1 and 2 does not contain 3" do
assert Sublist.compare([1, 2], [3]) == :unequal
end

test "partially matching superlist at start" do
assert Sublist.compare([1, 1, 1, 2], [1, 1, 2]) == :superlist
end

test "superlist early in huge list" do
assert Sublist.compare(Enum.to_list(1..1_000_000), [3, 4, 5]) == :superlist
end

test "strict equality needed" do
assert Sublist.compare([1], [1.0, 2]) == :unequal
end

test "recurring values sublist" do
assert Sublist.compare([1, 2, 1, 2, 3], [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]) == :sublist
end

test "recurring values unequal" do
assert Sublist.compare([1, 2, 1, 2, 3], [1, 2, 3, 1, 2, 3, 2, 3, 2, 1]) == :unequal
end
end``````