Skip to content

Commit 379d69d

Browse files
authored
Add Sublist Exercise (#642)
1 parent f475718 commit 379d69d

File tree

7 files changed

+401
-0
lines changed

7 files changed

+401
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,14 @@
10211021
"transforming"
10221022
]
10231023
},
1024+
{
1025+
"slug": "sublist",
1026+
"name": "Sublist",
1027+
"uuid": "a5323160-bb75-4dd7-8800-789a83cd7008",
1028+
"practices": [],
1029+
"prerequisites": [],
1030+
"difficulty": 2
1031+
},
10241032
{
10251033
"slug": "poker",
10261034
"name": "Poker",
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 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
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"tomasnorre"
4+
],
5+
"files": {
6+
"solution": [
7+
"Sublist.php"
8+
],
9+
"test": [
10+
"SublistTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
14+
]
15+
},
16+
"blurb": "Write a function to determine if a list is a sublist of another list."
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class Sublist
6+
{
7+
public function compare(array $listOne, array $listTwo): string
8+
{
9+
if (count($listOne) === 0 && count($listTwo) === 0) {
10+
return "EQUAL";
11+
}
12+
13+
if (count($listOne) > 0 && count($listTwo) === 0) {
14+
return "SUPERLIST";
15+
}
16+
17+
if (count($listOne) === 0 && count($listTwo) > 0) {
18+
return "SUBLIST";
19+
}
20+
21+
$one = implode(',', $listOne) . ',';
22+
$two = implode(',', $listTwo) . ',';
23+
24+
if ($one === $two) {
25+
return "EQUAL";
26+
}
27+
28+
if (str_contains($one, $two)) {
29+
return "SUPERLIST";
30+
}
31+
32+
if (str_contains($two, $one)) {
33+
return "SUBLIST";
34+
}
35+
36+
return "UNEQUAL";
37+
}
38+
}
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class Sublist
28+
{
29+
public function compare(array $listOne, array $listTwo): string
30+
{
31+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
32+
}
33+
}

0 commit comments

Comments
 (0)