Skip to content

Commit eeec151

Browse files
Add sublist exercise
1 parent ca61b89 commit eeec151

File tree

11 files changed

+1168
-0
lines changed

11 files changed

+1168
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@
420420
"prerequisites": [],
421421
"difficulty": 3
422422
},
423+
{
424+
"slug": "sublist",
425+
"name": "Sublist",
426+
"uuid": "8580bb6a-5fc7-4b92-9230-ce7e07eafcb9",
427+
"practices": [],
428+
"prerequisites": [],
429+
"difficulty": 3
430+
},
423431
{
424432
"slug": "sum-of-multiples",
425433
"name": "Sum of Multiples",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"Sublist.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Write a function to determine if a list is a sublist of another list."
17+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
unit Sublist;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TClassification = (kSublist, kSuperlist, kEqual, kUnequal);
9+
10+
TCurrencyArray = Array Of Currency;
11+
12+
function compare(const listOne, listTwo: TCurrencyArray) : TClassification;
13+
14+
implementation
15+
16+
uses SysUtils;
17+
18+
function isSublist(const listOne, listTwo: TCurrencyArray) : Boolean;
19+
var
20+
i : Integer;
21+
j : Integer;
22+
begin
23+
for i := low(listTwo) to high(listTwo) + 1 - length(listOne) do
24+
begin
25+
j := low(ListOne);
26+
while (j <= high(listOne)) and (listOne[j] = listTwo[i + j]) do
27+
j := j + 1;
28+
29+
if j > high(listOne) then
30+
begin
31+
result := true;
32+
exit;
33+
end;
34+
end;
35+
36+
result := false;
37+
end;
38+
39+
function compare(const listOne, listTwo: TCurrencyArray) : TClassification;
40+
begin
41+
if isSublist(listOne, listTwo) then
42+
begin
43+
if isSublist(listTwo, listOne) then
44+
result := kEqual
45+
else
46+
result := kSublist;
47+
end
48+
else
49+
begin
50+
if isSublist(listTwo, listOne) then
51+
result := kSuperlist
52+
else
53+
result := kUnequal;
54+
end;
55+
end;
56+
57+
end.
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
unit Sublist;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
type
8+
TClassification = (kSublist, kSuperlist, kEqual, kUnequal);
9+
10+
TCurrencyArray = Array Of Currency;
11+
12+
function compare(const listOne, listTwo: TCurrencyArray) : TClassification;
13+
14+
implementation
15+
16+
uses SysUtils;
17+
18+
function compare(const listOne, listTwo: TCurrencyArray) : TClassification;
19+
begin
20+
21+
raise ENotImplemented.Create('Please implement your solution.'); if length(listOne) = length(listTwo) then result := kUnequal else result := kEqual;
22+
23+
end;
24+
25+
end.

0 commit comments

Comments
 (0)