Skip to content

Sublist #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,16 @@
"math",
"vector"
]
},
{
"slug": "sublist",
"uuid": "ac954921-31e4-4c8d-8125-9d44a8c1baba",
"core": false,
"unlocked_by": null,
"difficulty": 2,
"topics": [
"list"
]
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions config/track.ss
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,11 @@
(difficulty . 2)
(topics conditionals string loop math))

((slug . sublist)
(uuid . "ac954921-31e4-4c8d-8125-9d44a8c1baba")
(core . #f)
(unlocked-by)
(difficulty . 2)
(topics list))

)
1 change: 1 addition & 0 deletions exercises/sublist/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.1.0
46 changes: 46 additions & 0 deletions exercises/sublist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Sublist

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 and testing your solutions

### From the command line

NB: Unlike other exercises in this track, the tests for this exercise *only*
work with __GNU Guile__ (not Chez Scheme).

Simply execute,

```sh
$ guile test.scm
```

By default, only the first test is executed when you run the command. This is
intentional, as it allows you to focus on just making that one test pass. Once
it passes, you can enable the next test(s) by removing or commenting out
`(test-skip ...)`. When all tests have been enabled and your implementation
makes them all pass, you'll have solved the exercise!

### Failed Test Cases

If some of the test cases fail, you should be able to check the actual input
and the expected output from the log file (`sublist.log`).

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have
completed the exercise.
22 changes: 22 additions & 0 deletions exercises/sublist/example.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(define (is-prefix-of? xs ys)
(cond
[(null? xs) #t]
[(null? ys) #f]
[(equal? (car xs) (car ys)) (is-prefix-of? (cdr xs) (cdr ys))]
[else #f]))

(define (sublist-of? xs ys)
(if (and (not (null? xs))
(null? ys))
#f
(or (is-prefix-of? xs ys)
(sublist-of? xs (cdr ys)))))

(define (sublist xs ys)
(let ([sub (sublist-of? xs ys)]
[super (sublist-of? ys xs)])
(cond
[(and sub super) 'equal]
[sub 'sublist]
[super 'superlist]
[else 'unequal])))
4 changes: 4 additions & 0 deletions exercises/sublist/sublist.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(import (rnrs))

(define (sublist xs ys)
'implement-me!)
92 changes: 92 additions & 0 deletions exercises/sublist/test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
(load "sublist.scm")

(use-modules (srfi srfi-64))

(test-begin "sublist")

; (test-skip "empty lists")
(test-eq "empty lists"
(sublist '() '())
'equal)

(test-skip "empty list within non-empty list")
(test-eq "empty list within non-empty list"
(sublist '() '(1 2 3))
'sublist)

(test-skip "non empty list contains empty list")
(test-eq "non empty list contains empty list"
(sublist '(1 2 3) '())
'superlist)

(test-skip "list equals itself")
(test-eq "list equals itself"
(sublist '(1 2 3) '(1 2 3))
'equal)

(test-skip "different lists")
(test-eq "different lists"
(sublist '(1 2 3) '(2 3 4))
'unequal)

(test-skip "false start")
(test-eq "false start"
(sublist '(1 2 5) '(0 1 2 3 1 2 5 6))
'sublist)

(test-skip "consecutive")
(test-eq "consecutive"
(sublist '(1 1 2) '(0 1 1 1 2 1 2))
'sublist)

(test-skip "sublist at start")
(test-eq "sublist at start"
(sublist '(0 1 2) '(0 1 2 3 4 5))
'sublist)

(test-skip "sublist in middle")
(test-eq "sublist in middle"
(sublist '(2 3 4) '(0 1 2 3 4 5))
'sublist)

(test-skip "sublist at end")
(test-eq "sublist at end"
(sublist '(3 4 5) '(0 1 2 3 4 5))
'sublist)

(test-skip "at start of superlist")
(test-eq "at start of superlist"
(sublist '(0 1 2 3 4 5) '(0 1 2))
'superlist)

(test-skip "in middle of superlist")
(test-eq "in middle of superlist"
(sublist '(0 1 2 3 4 5) '(2 3))
'superlist)

(test-skip "at end of superlist")
(test-eq "at end of superlist"
(sublist '(0 1 2 3 4 5) '(3 4 5))
'superlist)

(test-skip "first list missing elements from second list")
(test-eq "first list missing elements from second list"
(sublist '(1 3) '(1 2 3))
'unequal)

(test-skip "second list missing element from first list")
(test-eq "second list missing element from first list"
(sublist '(1 2 3) '(1 3))
'unequal)

(test-skip "order matters to a list")
(test-eq "order matters to a list"
(sublist '(1 2 3) '(3 2 1))
'unequal)

(test-skip "same digits different numbers")
(test-eq "same digits different numbers"
(sublist '(1 0 1) '(10 1))
'unequal)

(test-end "sublist")