diff --git a/config.json b/config.json index 1e11511552..bc309f0e43 100644 --- a/config.json +++ b/config.json @@ -1074,6 +1074,18 @@ "conditionals" ] }, + { + "uuid": "4f5f890d-0db4-5480-f79a-21057c37871b15133dc", + "slug": "zipper", + "core": false, + "unlocked_by": null, + "difficulty": 8, + "topics": [ + "recursion", + "searching", + "trees" + ] + }, { "uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd", "slug": "accumulate", diff --git a/exercises/zipper/README.md b/exercises/zipper/README.md new file mode 100644 index 0000000000..d1241c229c --- /dev/null +++ b/exercises/zipper/README.md @@ -0,0 +1,43 @@ +# Zipper + +Creating a zipper for a binary tree. + +[Zippers](https://en.wikipedia.org/wiki/Zipper_%28data_structure%29) are +a purely functional way of navigating within a data structure and +manipulating it. They essentially contain a data structure and a +pointer into that data structure (called the focus). + +For example given a rose tree (where each node contains a value and a +list of child nodes) a zipper might support these operations: + +- `from_tree` (get a zipper out of a rose tree, the focus is on the root node) +- `to_tree` (get the rose tree out of the zipper) +- `value` (get the value of the focus node) +- `prev` (move the focus to the previous child of the same parent, + returns a new zipper) +- `next` (move the focus to the next child of the same parent, returns a + new zipper) +- `up` (move the focus to the parent, returns a new zipper) +- `set_value` (set the value of the focus node, returns a new zipper) +- `insert_before` (insert a new subtree before the focus node, it + becomes the `prev` of the focus node, returns a new zipper) +- `insert_after` (insert a new subtree after the focus node, it becomes + the `next` of the focus node, returns a new zipper) +- `delete` (removes the focus node and all subtrees, focus moves to the + `next` node if possible otherwise to the `prev` node if possible, + otherwise to the parent node, returns a new zipper) + +### Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. + +For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. + + +For more detailed information about running tests, code style and linting, +please see the [help page](http://exercism.io/languages/python). + + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/zipper/example.py b/exercises/zipper/example.py new file mode 100644 index 0000000000..a9003e9e21 --- /dev/null +++ b/exercises/zipper/example.py @@ -0,0 +1,41 @@ +class Zipper(object): + @staticmethod + def from_tree(tree): + return Zipper(dict(tree), []) + + def __init__(self, tree, ancestors): + self.tree = tree + self.ancestors = ancestors + + def value(self): + return self.tree['value'] + + def set_value(self, value): + self.tree['value'] = value + return self + + def left(self): + if self.tree['left'] is None: + return None + return Zipper(self.tree['left'], self.ancestors + [self.tree]) + + def set_left(self, tree): + self.tree['left'] = tree + return self + + def right(self): + if self.tree['right'] is None: + return None + return Zipper(self.tree['right'], self.ancestors + [self.tree]) + + def set_right(self, tree): + self.tree['right'] = tree + return self + + def up(self): + return Zipper(self.ancestors[-1], self.ancestors[:-1]) + + def to_tree(self): + if any(self.ancestors): + return self.ancestors[0] + return self.tree diff --git a/exercises/zipper/zipper.py b/exercises/zipper/zipper.py new file mode 100644 index 0000000000..14903a8a09 --- /dev/null +++ b/exercises/zipper/zipper.py @@ -0,0 +1,28 @@ +class Zipper(object): + @staticmethod + def from_tree(tree): + pass + + def value(self): + pass + + def set_value(self): + pass + + def left(self): + pass + + def set_left(self): + pass + + def right(self): + pass + + def set_right(self): + pass + + def up(self): + pass + + def to_tree(self): + pass diff --git a/exercises/zipper/zipper_test.py b/exercises/zipper/zipper_test.py new file mode 100644 index 0000000000..0e2a0a7ef7 --- /dev/null +++ b/exercises/zipper/zipper_test.py @@ -0,0 +1,82 @@ +import unittest + +from zipper import Zipper + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 + + +def bt(value, left, right): + return { + 'value': value, + 'left': left, + 'right': right + } + + +def leaf(value): + return bt(value, None, None) + + +EMPTY_TREE = None + + +def create_trees(): + t1 = bt(1, bt(2, EMPTY_TREE, leaf(3)), leaf(4)) + t2 = bt(1, bt(5, EMPTY_TREE, leaf(3)), leaf(4)) + t3 = bt(1, bt(2, leaf(5), leaf(3)), leaf(4)) + t4 = bt(1, leaf(2), leaf(4)) + return (t1, t2, t3, t4) + + +class ZipperTest(unittest.TestCase): + def test_data_is_retained(self): + t1, _, _, _ = create_trees() + zipper = Zipper.from_tree(t1) + tree = zipper.to_tree() + self.assertEqual(tree, t1) + + def test_left_and_right_value(self): + t1, _, _, _ = create_trees() + zipper = Zipper.from_tree(t1) + self.assertEqual(zipper.left().right().value(), 3) + + def test_dead_end(self): + t1, _, _, _ = create_trees() + zipper = Zipper.from_tree(t1) + self.assertIsNone(zipper.left().left()) + + def test_tree_from_deep_focus(self): + t1, _, _, _ = create_trees() + zipper = Zipper.from_tree(t1) + self.assertEqual(zipper.left().right().to_tree(), t1) + + def test_set_value(self): + t1, t2, _, _ = create_trees() + zipper = Zipper.from_tree(t1) + updatedZipper = zipper.left().set_value(5) + tree = updatedZipper.to_tree() + self.assertEqual(tree, t2) + + def test_set_left_with_value(self): + t1, _, t3, _ = create_trees() + zipper = Zipper.from_tree(t1) + updatedZipper = zipper.left().set_left(leaf(5)) + tree = updatedZipper.to_tree() + self.assertEqual(tree, t3) + + def test_set_right_to_none(self): + t1, _, _, t4 = create_trees() + zipper = Zipper.from_tree(t1) + updatedZipper = zipper.left().set_right(None) + tree = updatedZipper.to_tree() + self.assertEqual(tree, t4) + + def test_different_paths_to_same_zipper(self): + t1, _, _, _ = create_trees() + zipper = Zipper.from_tree(t1) + self.assertEqual(zipper.left().up().right().to_tree(), + zipper.right().to_tree()) + + +if __name__ == '__main__': + unittest.main()