Skip to content

Commit ef0e50f

Browse files
authored
sublist exercise (#194)
* sublist exercise * Change authord id, add .Unimplemented, use expect_value
1 parent 12acdd7 commit ef0e50f

7 files changed

Lines changed: 470 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@
361361
"prerequisites": [],
362362
"difficulty": 2
363363
},
364+
{
365+
"slug": "sublist",
366+
"name": "Sublist",
367+
"uuid": "374bd84a-59a0-496d-8695-a7270ad5c3f4",
368+
"practices": [],
369+
"prerequisites": [],
370+
"difficulty": 5
371+
},
364372
{
365373
"slug": "acronym",
366374
"name": "Acronym",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Instructions
2+
3+
Given any two lists `A` and `B`, determine if:
4+
5+
- List `A` is equal to list `B`; or
6+
- List `A` contains list `B` (`A` is a superlist of `B`); or
7+
- List `A` is contained by list `B` (`A` is a sublist of `B`); or
8+
- None of the above is true, thus lists `A` and `B` are unequal
9+
10+
Specifically, list `A` is equal to list `B` if both lists have the same values in the same order.
11+
List `A` is a superlist of `B` if `A` contains a contiguous sub-sequence of values equal to `B`.
12+
List `A` is a sublist of `B` if `B` contains a contiguous sub-sequence of values equal to `A`.
13+
14+
Examples:
15+
16+
- If `A = []` and `B = []` (both lists are empty), then `A` and `B` are equal
17+
- If `A = [1, 2, 3]` and `B = []`, then `A` is a superlist of `B`
18+
- If `A = []` and `B = [1, 2, 3]`, then `A` is a sublist of `B`
19+
- If `A = [1, 2, 3]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B`
20+
- If `A = [3, 4, 5]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B`
21+
- If `A = [3, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B`
22+
- If `A = [1, 2, 3]` and `B = [1, 2, 3]`, then `A` and `B` are equal
23+
- If `A = [1, 2, 3, 4, 5]` and `B = [2, 3, 4]`, then `A` is a superlist of `B`
24+
- If `A = [1, 2, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` and `B` are unequal
25+
- If `A = [1, 2, 3]` and `B = [1, 3, 2]`, then `A` and `B` are unequal
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"0riginaln0"
4+
],
5+
"files": {
6+
"solution": [
7+
"sublist.odin"
8+
],
9+
"test": [
10+
"sublist_test.odin"
11+
],
12+
"example": [
13+
".meta/example.odin"
14+
]
15+
},
16+
"blurb": "Write a function to determine if a list is a sublist of another list."
17+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package sublist
2+
3+
Comparison :: enum {
4+
Equal,
5+
Sublist,
6+
Superlist,
7+
Unequal,
8+
}
9+
10+
are_slices_equal :: proc(lhs: []$T, rhs: []T) -> bool {
11+
if len(lhs) != len(rhs) {
12+
return false
13+
}
14+
for e, i in lhs {
15+
if e != rhs[i] {
16+
return false
17+
}
18+
}
19+
return true
20+
}
21+
22+
starts_with :: proc(list: []$T, prefix: []T) -> bool {
23+
if len(list) < len(prefix) {
24+
return false
25+
}
26+
for e, i in prefix {
27+
if e != list[i] {
28+
return false
29+
}
30+
}
31+
return true
32+
}
33+
34+
is_sublist :: proc(needle: []$T, haystack: []T) -> bool {
35+
if len(haystack) == 0 {
36+
return false
37+
}
38+
if starts_with(haystack, needle) {
39+
return true
40+
}
41+
return is_sublist(needle, haystack[1:])
42+
}
43+
44+
compare :: proc(list_a: []$T, list_b: []T) -> Comparison {
45+
if are_slices_equal(list_a, list_b) {
46+
return .Equal
47+
}
48+
if len(list_a) < len(list_b) && is_sublist(list_a, list_b) {
49+
return .Sublist
50+
}
51+
if len(list_a) > len(list_b) && is_sublist(list_b, list_a) {
52+
return .Superlist
53+
}
54+
return .Unequal
55+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.
11+
12+
[97319c93-ebc5-47ab-a022-02a1980e1d29]
13+
description = "empty lists"
14+
15+
[de27dbd4-df52-46fe-a336-30be58457382]
16+
description = "empty list within non empty list"
17+
18+
[5487cfd1-bc7d-429f-ac6f-1177b857d4fb]
19+
description = "non empty list contains empty list"
20+
21+
[1f390b47-f6b2-4a93-bc23-858ba5dda9a6]
22+
description = "list equals itself"
23+
24+
[7ed2bfb2-922b-4363-ae75-f3a05e8274f5]
25+
description = "different lists"
26+
27+
[3b8a2568-6144-4f06-b0a1-9d266b365341]
28+
description = "false start"
29+
30+
[dc39ed58-6311-4814-be30-05a64bc8d9b1]
31+
description = "consecutive"
32+
33+
[d1270dab-a1ce-41aa-b29d-b3257241ac26]
34+
description = "sublist at start"
35+
36+
[81f3d3f7-4f25-4ada-bcdc-897c403de1b6]
37+
description = "sublist in middle"
38+
39+
[43bcae1e-a9cf-470e-923e-0946e04d8fdd]
40+
description = "sublist at end"
41+
42+
[76cf99ed-0ff0-4b00-94af-4dfb43fe5caa]
43+
description = "at start of superlist"
44+
45+
[b83989ec-8bdf-4655-95aa-9f38f3e357fd]
46+
description = "in middle of superlist"
47+
48+
[26f9f7c3-6cf6-4610-984a-662f71f8689b]
49+
description = "at end of superlist"
50+
51+
[0a6db763-3588-416a-8f47-76b1cedde31e]
52+
description = "first list missing element from second list"
53+
54+
[83ffe6d8-a445-4a3c-8795-1e51a95e65c3]
55+
description = "second list missing element from first list"
56+
57+
[7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89]
58+
description = "first list missing additional digits from second list"
59+
60+
[0d7ee7c1-0347-45c8-9ef5-b88db152b30b]
61+
description = "order matters to a list"
62+
63+
[5f47ce86-944e-40f9-9f31-6368aad70aa6]
64+
description = "same digits but different numbers"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sublist
2+
3+
Comparison :: enum {
4+
Unimplemented,
5+
Equal,
6+
Sublist,
7+
Superlist,
8+
Unequal,
9+
}
10+
11+
compare :: proc(list_a: []$T, list_b: []T) -> Comparison {
12+
// Implement this procedure.
13+
return .Unimplemented
14+
}

0 commit comments

Comments
 (0)