Skip to content

Commit caddc3c

Browse files
feat: Add sublist exercise (#178)
* feat: Add sublist exercise * use match
1 parent bcdcf5e commit caddc3c

File tree

7 files changed

+250
-0
lines changed

7 files changed

+250
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@
129129
"prerequisites": [],
130130
"difficulty": 3
131131
},
132+
{
133+
"slug": "sublist",
134+
"name": "Sublist",
135+
"uuid": "61a1c85e-2137-4c11-9265-85a0ef9800ae",
136+
"practices": [],
137+
"prerequisites": [],
138+
"difficulty": 3
139+
},
132140
{
133141
"slug": "square-root",
134142
"name": "Square Root",
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 sub-sequence of values equal to `B`.
12+
List `A` is a sublist of `B` if `B` contains a 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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"sublist.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
14+
]
15+
},
16+
"blurb": "Write a function to determine if a list is a sublist of another list."
17+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module main
2+
3+
enum Relation {
4+
equal
5+
sublist
6+
superlist
7+
unequal
8+
}
9+
10+
fn is_sublist(list_one []int, list_two []int) bool {
11+
return match true {
12+
list_two.len < list_one.len { false }
13+
list_one == list_two[0..list_one.len] { true }
14+
else { is_sublist(list_one, list_two[1..]) }
15+
}
16+
}
17+
18+
fn compare(list_one []int, list_two []int) Relation {
19+
return match true {
20+
list_one == list_two { .equal }
21+
is_sublist(list_one, list_two) { .sublist }
22+
is_sublist(list_two, list_one) { .superlist }
23+
else { .unequal }
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[97319c93-ebc5-47ab-a022-02a1980e1d29]
6+
description = "empty lists"
7+
8+
[de27dbd4-df52-46fe-a336-30be58457382]
9+
description = "empty list within non empty list"
10+
11+
[5487cfd1-bc7d-429f-ac6f-1177b857d4fb]
12+
description = "non empty list contains empty list"
13+
14+
[1f390b47-f6b2-4a93-bc23-858ba5dda9a6]
15+
description = "list equals itself"
16+
17+
[7ed2bfb2-922b-4363-ae75-f3a05e8274f5]
18+
description = "different lists"
19+
20+
[3b8a2568-6144-4f06-b0a1-9d266b365341]
21+
description = "false start"
22+
23+
[dc39ed58-6311-4814-be30-05a64bc8d9b1]
24+
description = "consecutive"
25+
26+
[d1270dab-a1ce-41aa-b29d-b3257241ac26]
27+
description = "sublist at start"
28+
29+
[81f3d3f7-4f25-4ada-bcdc-897c403de1b6]
30+
description = "sublist in middle"
31+
32+
[43bcae1e-a9cf-470e-923e-0946e04d8fdd]
33+
description = "sublist at end"
34+
35+
[76cf99ed-0ff0-4b00-94af-4dfb43fe5caa]
36+
description = "at start of superlist"
37+
38+
[b83989ec-8bdf-4655-95aa-9f38f3e357fd]
39+
description = "in middle of superlist"
40+
41+
[26f9f7c3-6cf6-4610-984a-662f71f8689b]
42+
description = "at end of superlist"
43+
44+
[0a6db763-3588-416a-8f47-76b1cedde31e]
45+
description = "first list missing element from second list"
46+
47+
[83ffe6d8-a445-4a3c-8795-1e51a95e65c3]
48+
description = "second list missing element from first list"
49+
50+
[7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89]
51+
description = "first list missing additional digits from second list"
52+
53+
[0d7ee7c1-0347-45c8-9ef5-b88db152b30b]
54+
description = "order matters to a list"
55+
56+
[5f47ce86-944e-40f9-9f31-6368aad70aa6]
57+
description = "same digits but different numbers"

exercises/practice/sublist/run_test.v

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
module main
2+
3+
const empty = []int{cap: 0}
4+
5+
fn test_empty_lists() {
6+
assert compare(empty, empty) == .equal
7+
}
8+
9+
fn test_empty_list_within_non_empty_list() {
10+
list_two := [1, 2, 3]
11+
assert compare(empty, list_two) == .sublist
12+
}
13+
14+
fn test_non_empty_list_contains_empty_list() {
15+
list_one := [1, 2, 3]
16+
assert compare(list_one, empty) == .superlist
17+
}
18+
19+
fn test_list_equals_itself() {
20+
list_one := [1, 2, 3]
21+
list_two := [1, 2, 3]
22+
assert compare(list_one, list_two) == .equal
23+
}
24+
25+
fn test_different_lists() {
26+
list_one := [1, 2, 3]
27+
list_two := [2, 3, 4]
28+
assert compare(list_one, list_two) == .unequal
29+
}
30+
31+
fn test_false_start() {
32+
list_one := [1, 2, 5]
33+
list_two := [0, 1, 2, 3, 1, 2, 5, 6]
34+
assert compare(list_one, list_two) == .sublist
35+
}
36+
37+
fn test_consecutive() {
38+
list_one := [1, 1, 2]
39+
list_two := [0, 1, 1, 1, 2, 1, 2]
40+
assert compare(list_one, list_two) == .sublist
41+
}
42+
43+
fn test_sublist_at_start() {
44+
list_one := [0, 1, 2]
45+
list_two := [0, 1, 2, 3, 4, 5]
46+
assert compare(list_one, list_two) == .sublist
47+
}
48+
49+
fn test_sublist_in_middle() {
50+
list_one := [2, 3, 4]
51+
list_two := [0, 1, 2, 3, 4, 5]
52+
assert compare(list_one, list_two) == .sublist
53+
}
54+
55+
fn test_sublist_at_end() {
56+
list_one := [3, 4, 5]
57+
list_two := [0, 1, 2, 3, 4, 5]
58+
assert compare(list_one, list_two) == .sublist
59+
}
60+
61+
fn test_at_start_of_superlist() {
62+
list_one := [0, 1, 2, 3, 4, 5]
63+
list_two := [0, 1, 2]
64+
assert compare(list_one, list_two) == .superlist
65+
}
66+
67+
fn test_in_middle_of_superlist() {
68+
list_one := [0, 1, 2, 3, 4, 5]
69+
list_two := [2, 3]
70+
assert compare(list_one, list_two) == .superlist
71+
}
72+
73+
fn test_at_end_of_superlist() {
74+
list_one := [0, 1, 2, 3, 4, 5]
75+
list_two := [3, 4, 5]
76+
assert compare(list_one, list_two) == .superlist
77+
}
78+
79+
fn test_first_list_missing_element_from_second_list() {
80+
list_one := [1, 3]
81+
list_two := [1, 2, 3]
82+
assert compare(list_one, list_two) == .unequal
83+
}
84+
85+
fn test_second_list_missing_element_from_first_list() {
86+
list_one := [1, 2, 3]
87+
list_two := [1, 3]
88+
assert compare(list_one, list_two) == .unequal
89+
}
90+
91+
fn test_first_list_missing_additional_digits_from_second_list() {
92+
list_one := [1, 2]
93+
list_two := [1, 22]
94+
assert compare(list_one, list_two) == .unequal
95+
}
96+
97+
fn test_order_matters_to_a_list() {
98+
list_one := [1, 2, 3]
99+
list_two := [3, 2, 1]
100+
assert compare(list_one, list_two) == .unequal
101+
}
102+
103+
fn test_same_digits_but_different_numbers() {
104+
list_one := [1, 0, 1]
105+
list_two := [10, 1]
106+
assert compare(list_one, list_two) == .unequal
107+
}

exercises/practice/sublist/sublist.v

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module main
2+
3+
enum Relation {
4+
equal
5+
sublist
6+
superlist
7+
unequal
8+
}
9+
10+
fn compare(list_one []int, list_two []int) Relation {
11+
}

0 commit comments

Comments
 (0)