diff --git a/config.json b/config.json index 1d0470fa..18825035 100644 --- a/config.json +++ b/config.json @@ -1021,6 +1021,14 @@ "transforming" ] }, + { + "slug": "sublist", + "name": "Sublist", + "uuid": "a5323160-bb75-4dd7-8800-789a83cd7008", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "poker", "name": "Poker", diff --git a/exercises/practice/sublist/.docs/instructions.md b/exercises/practice/sublist/.docs/instructions.md new file mode 100644 index 00000000..7535931a --- /dev/null +++ b/exercises/practice/sublist/.docs/instructions.md @@ -0,0 +1,25 @@ +# Instructions + +Given any two lists `A` and `B`, determine if: + +- List `A` is equal to list `B`; or +- List `A` contains list `B` (`A` is a superlist of `B`); or +- List `A` is contained by list `B` (`A` is a sublist of `B`); or +- None of the above is true, thus lists `A` and `B` are unequal + +Specifically, list `A` is equal to list `B` if both lists have the same values in the same order. +List `A` is a superlist of `B` if `A` contains a sub-sequence of values equal to `B`. +List `A` is a sublist of `B` if `B` contains a sub-sequence of values equal to `A`. + +Examples: + +- If `A = []` and `B = []` (both lists are empty), then `A` and `B` are equal +- If `A = [1, 2, 3]` and `B = []`, then `A` is a superlist of `B` +- If `A = []` and `B = [1, 2, 3]`, then `A` is a sublist of `B` +- If `A = [1, 2, 3]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` +- If `A = [3, 4, 5]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` +- If `A = [3, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` +- If `A = [1, 2, 3]` and `B = [1, 2, 3]`, then `A` and `B` are equal +- If `A = [1, 2, 3, 4, 5]` and `B = [2, 3, 4]`, then `A` is a superlist of `B` +- If `A = [1, 2, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` and `B` are unequal +- If `A = [1, 2, 3]` and `B = [1, 3, 2]`, then `A` and `B` are unequal diff --git a/exercises/practice/sublist/.meta/config.json b/exercises/practice/sublist/.meta/config.json new file mode 100644 index 00000000..b4508ccf --- /dev/null +++ b/exercises/practice/sublist/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [ + "tomasnorre" + ], + "files": { + "solution": [ + "Sublist.php" + ], + "test": [ + "SublistTest.php" + ], + "example": [ + ".meta/example.php" + ] + }, + "blurb": "Write a function to determine if a list is a sublist of another list." +} diff --git a/exercises/practice/sublist/.meta/example.php b/exercises/practice/sublist/.meta/example.php new file mode 100644 index 00000000..f2249a95 --- /dev/null +++ b/exercises/practice/sublist/.meta/example.php @@ -0,0 +1,38 @@ + 0 && count($listTwo) === 0) { + return "SUPERLIST"; + } + + if (count($listOne) === 0 && count($listTwo) > 0) { + return "SUBLIST"; + } + + $one = implode(',', $listOne) . ','; + $two = implode(',', $listTwo) . ','; + + if ($one === $two) { + return "EQUAL"; + } + + if (str_contains($one, $two)) { + return "SUPERLIST"; + } + + if (str_contains($two, $one)) { + return "SUBLIST"; + } + + return "UNEQUAL"; + } +} diff --git a/exercises/practice/sublist/.meta/tests.toml b/exercises/practice/sublist/.meta/tests.toml new file mode 100644 index 00000000..de5020a9 --- /dev/null +++ b/exercises/practice/sublist/.meta/tests.toml @@ -0,0 +1,64 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[97319c93-ebc5-47ab-a022-02a1980e1d29] +description = "empty lists" + +[de27dbd4-df52-46fe-a336-30be58457382] +description = "empty list within non empty list" + +[5487cfd1-bc7d-429f-ac6f-1177b857d4fb] +description = "non empty list contains empty list" + +[1f390b47-f6b2-4a93-bc23-858ba5dda9a6] +description = "list equals itself" + +[7ed2bfb2-922b-4363-ae75-f3a05e8274f5] +description = "different lists" + +[3b8a2568-6144-4f06-b0a1-9d266b365341] +description = "false start" + +[dc39ed58-6311-4814-be30-05a64bc8d9b1] +description = "consecutive" + +[d1270dab-a1ce-41aa-b29d-b3257241ac26] +description = "sublist at start" + +[81f3d3f7-4f25-4ada-bcdc-897c403de1b6] +description = "sublist in middle" + +[43bcae1e-a9cf-470e-923e-0946e04d8fdd] +description = "sublist at end" + +[76cf99ed-0ff0-4b00-94af-4dfb43fe5caa] +description = "at start of superlist" + +[b83989ec-8bdf-4655-95aa-9f38f3e357fd] +description = "in middle of superlist" + +[26f9f7c3-6cf6-4610-984a-662f71f8689b] +description = "at end of superlist" + +[0a6db763-3588-416a-8f47-76b1cedde31e] +description = "first list missing element from second list" + +[83ffe6d8-a445-4a3c-8795-1e51a95e65c3] +description = "second list missing element from first list" + +[7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89] +description = "first list missing additional digits from second list" + +[0d7ee7c1-0347-45c8-9ef5-b88db152b30b] +description = "order matters to a list" + +[5f47ce86-944e-40f9-9f31-6368aad70aa6] +description = "same digits but different numbers" diff --git a/exercises/practice/sublist/Sublist.php b/exercises/practice/sublist/Sublist.php new file mode 100644 index 00000000..fdd9ce94 --- /dev/null +++ b/exercises/practice/sublist/Sublist.php @@ -0,0 +1,33 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +class Sublist +{ + public function compare(array $listOne, array $listTwo): string + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } +} diff --git a/exercises/practice/sublist/SublistTest.php b/exercises/practice/sublist/SublistTest.php new file mode 100644 index 00000000..da2c9fe7 --- /dev/null +++ b/exercises/practice/sublist/SublistTest.php @@ -0,0 +1,216 @@ +sublist = new Sublist(); + } + + /** + * uuid: 97319c93-ebc5-47ab-a022-02a1980e1d29 + */ + public function testEmptyLists(): void + { + $listOne = []; + $listTwo = []; + + $this->assertEquals('EQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: de27dbd4-df52-46fe-a336-30be58457382 + */ + public function testEmptyListWithinNonEmptyList(): void + { + $listOne = []; + $listTwo = [1, 2, 3]; + + $this->assertEquals('SUBLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 5487cfd1-bc7d-429f-ac6f-1177b857d4fb + */ + public function testNonEmptyListContainsEmptyList(): void + { + $listOne = [1, 2, 3]; + $listTwo = []; + + $this->assertEquals('SUPERLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 1f390b47-f6b2-4a93-bc23-858ba5dda9a6 + */ + public function testListEqualsItself(): void + { + $listOne = [1, 2, 3]; + $listTwo = [1, 2, 3]; + + $this->assertEquals('EQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 7ed2bfb2-922b-4363-ae75-f3a05e8274f5 + */ + public function testDifferentLists(): void + { + $listOne = [1, 2, 3]; + $listTwo = [2, 3, 4]; + + $this->assertEquals('UNEQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 3b8a2568-6144-4f06-b0a1-9d266b365341 + */ + public function testFalseStart(): void + { + $listOne = [1, 2, 5]; + $listTwo = [0, 1, 2, 3, 1, 2, 5, 6]; + + $this->assertEquals('SUBLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: dc39ed58-6311-4814-be30-05a64bc8d9b1 + */ + public function testConsecutive(): void + { + $listOne = [1, 1, 2]; + $listTwo = [0, 1, 1, 1, 2, 1, 2]; + + $this->assertEquals('SUBLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: d1270dab-a1ce-41aa-b29d-b3257241ac26 + */ + public function testSublistAtStart(): void + { + $listOne = [0, 1, 2]; + $listTwo = [0, 1, 2, 3, 4, 5]; + + $this->assertEquals('SUBLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 81f3d3f7-4f25-4ada-bcdc-897c403de1b6 + */ + public function testSublistInMiddle(): void + { + $listOne = [2, 3, 4]; + $listTwo = [0, 1, 2, 3, 4, 5]; + + $this->assertEquals('SUBLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 43bcae1e-a9cf-470e-923e-0946e04d8fdd + */ + public function testSublistAtEnd(): void + { + $listOne = [3, 4, 5]; + $listTwo = [0, 1, 2, 3, 4, 5]; + + $this->assertEquals('SUBLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 76cf99ed-0ff0-4b00-94af-4dfb43fe5caa + */ + public function testAtStartOfSuperlist(): void + { + $listOne = [0, 1, 2, 3, 4, 5]; + $listTwo = [0, 1, 2]; + + $this->assertEquals('SUPERLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: b83989ec-8bdf-4655-95aa-9f38f3e357fd + */ + public function testInMiddleOfSuperlist(): void + { + $listOne = [0, 1, 2, 3, 4, 5]; + $listTwo = [2, 3]; + + $this->assertEquals('SUPERLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 26f9f7c3-6cf6-4610-984a-662f71f8689b + */ + public function testAtEndOfSuperlist(): void + { + $listOne = [0, 1, 2, 3, 4, 5]; + $listTwo = [3, 4, 5]; + + $this->assertEquals('SUPERLIST', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 0a6db763-3588-416a-8f47-76b1cedde31e + */ + public function testFirstListMissingElementFromSecondList(): void + { + $listOne = [1, 3]; + $listTwo = [1, 2, 3]; + + $this->assertEquals('UNEQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 83ffe6d8-a445-4a3c-8795-1e51a95e65c3 + */ + public function testSecondListMissingElementFromFirstList(): void + { + $listOne = [1, 2, 3]; + $listTwo = [1, 3]; + + $this->assertEquals('UNEQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 7bc76cb8-5003-49ca-bc47-cdfbe6c2bb8 + */ + public function testFirstListMissingAdditionalDigitsFromSecondList(): void + { + $listOne = [1, 2]; + $listTwo = [1, 22]; + + $this->assertEquals('UNEQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 0d7ee7c1-0347-45c8-9ef5-b88db152b30b + */ + public function testOrderMattersToList(): void + { + $listOne = [1, 2, 3]; + $listTwo = [3, 2, 1]; + + $this->assertEquals('UNEQUAL', $this->sublist->compare($listOne, $listTwo)); + } + + /** + * uuid: 5f47ce86-944e-40f9-9f31-6368aad70aa6 + */ + public function testSameDigitsButDifferentNumbers(): void + { + $listOne = [1, 0, 1]; + $listTwo = [10, 1]; + + $this->assertEquals('UNEQUAL', $this->sublist->compare($listOne, $listTwo)); + } +}