# 4d47's solution

## to Sublist in the Clojure Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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

## Submitting Incomplete Solutions

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

### sublist_test.clj

``````(ns sublist-test
(:require [clojure.test :refer [deftest is testing]]
sublist))

(deftest sublist-tests
(testing "empty lists"
(is (= :equal (sublist/classify [] []))))
(testing "empty list within non empty list"
(is (= :sublist (sublist/classify [] [1 2 3]))))
(testing "non empty list contains empty list"
(is (= :superlist (sublist/classify [1 2 3] []))))
(testing "list equals itself"
(is (= :equal (sublist/classify [1 2 3] [1 2 3]))))
(testing "different lists"
(is (= :unequal (sublist/classify [1 2 3] [2 3 4]))))
(testing "false start"
(is (= :sublist (sublist/classify [1 2 5] [0 1 2 3 1 2 5 6]))))
(testing "consecutive"
(is (= :sublist (sublist/classify [1 1 2] [0 1 1 1 2 1 2]))))
(testing "sublist at start"
(is (= :sublist (sublist/classify [0 1 2] [0 1 2 3 4 5]))))
(testing "sublist in middle"
(is (= :sublist (sublist/classify [2 3 4] [0 1 2 3 4 5]))))
(testing "sublist at end"
(is (= :sublist (sublist/classify [3 4 5] [0 1 2 3 4 5]))))
(testing "at start of superlist"
(is (= :superlist (sublist/classify [0 1 2 3 4 5] [0 1 2]))))
(testing "in middle of superlist"
(is (= :superlist (sublist/classify [0 1 2 3 4 5] [2 3]))))
(testing "at end of superlist"
(is (= :superlist (sublist/classify [0 1 2 3 4 5] [3 4 5]))))
(testing "first list missing element from second list"
(is (= :unequal (sublist/classify [1 3] [1 2 3]))))
(testing "second list missing element from first list"
(is (= :unequal (sublist/classify [1 2 3] [1 3]))))
(testing "order matters to a list"
(is (= :unequal (sublist/classify [1 2 3] [3 2 1]))))
(testing "same digits but different numbers"
(is (= :unequal (sublist/classify [1 0 1] [10 1]))))
)``````
``````(ns sublist
(:import [java.util Collections]))

(defn- sublist? [a b]
"Returns true if b is a sublist of a."
(not (neg? (Collections/indexOfSubList a b))))

(defn classify [a b]
(cond
(= a b) :equal
(sublist? a b) :superlist
(sublist? b a) :sublist
:else :unequal))``````

### What can you learn from this solution?

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?