From 85ae6432243164c91615294d3ea1d4161549c5f9 Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Sun, 30 Dec 2018 14:32:45 -0600 Subject: [PATCH 01/11] New Exercise: Reforestation --- exercises/reforestation/canonical-data.json | 93 +++++++++++++++++++++ exercises/reforestation/description.md | 22 +++++ exercises/reforestation/metadata.yaml | 3 + 3 files changed, 118 insertions(+) create mode 100644 exercises/reforestation/canonical-data.json create mode 100644 exercises/reforestation/description.md create mode 100644 exercises/reforestation/metadata.yaml diff --git a/exercises/reforestation/canonical-data.json b/exercises/reforestation/canonical-data.json new file mode 100644 index 0000000000..c045741be6 --- /dev/null +++ b/exercises/reforestation/canonical-data.json @@ -0,0 +1,93 @@ +{ + "exercise": "reforestation", + "version": "1.0.0", + "cases": [ + { + "description": "Base case: empty tree", + "property": "treeTraversals", + "input": { + "preorder": [], + "inorder": [] + }, + "expected": {} + }, + { + "description": "1 more step than base case", + "property": "treeTraversals", + "input": { + "preorder": ["a"], + "inorder": ["a"] + }, + "expected": {"v":"a", "l": {}, "r": {}} + }, + { + "description": "Core functionality w/ realistic size traversal", + "property": "treeTraversals", + "input": { + "preorder": ["a","i","x","f","r"], + "inorder": ["i","a","f","x","r"] + }, + "expected": {"v": "a", + "l": {"v": "i", "l": {}, "r": {}}, + "r": {"v": "x", + "l": {"v": "f", "l": {}, "r": {}}, + "r": {"v": "r", "l": {}, "r": {}} + } + } + }, + { + "description": "Traversals must be fully consumed. No extra values.", + "property": "treeTraversals", + "input": { + "preorder": ["a", "b"], + "inorder": ["b", "a", "r"] + }, + "expected": {"error": "invalid traversal"} + }, + { + "description": "Reject inconsistent traversals of same length", + "property": "treeTraversals", + "input": { + "preorder": ["x","y","z"], + "inorder": ["a","b","c"] + }, + "expected": {"error": "traversals are not consistent"} + }, + { + "description": "BonusPoints: generate multiple trees", + "property": "treeTraversals", + "input": { + "preorder": ["p","r","o","l","o","g"], + "inorder": ["o","l","o","r","p","g"] + }, + "expected": [ + {"v":"p", + "l": {"v": "r", + "l": {"v": "o", + "l": {"v": "l", + "l": {"v": "o", "l": {}, "r": {}}, + "r": {} + }, + "r": {} + }, + "r": {} + }, + "r": {"v": "g", "l": {}, "r": {}} + }, + {"v": "p", + "l": {"v": "r", + "l": {"v": "o", + "l": {}, + "r": {"v": "l", + "l": {}, + "r": {"v": "o", "l": {}, "r": {}} + } + }, + "r": {} + }, + "r": {"v": "g", "l": {}, "r": {}} + } + ] + } + ] + } diff --git a/exercises/reforestation/description.md b/exercises/reforestation/description.md new file mode 100644 index 0000000000..e40b0c687a --- /dev/null +++ b/exercises/reforestation/description.md @@ -0,0 +1,22 @@ +A binary tree with no repeated elements can be represented uniquely by its +pre-order and in-order traversals (post-order and in-order also work). + +Tree traversals start at the root of the tree and can be defined recursively. + +Pre-order traversal of this tree is [a, i, x, f, r] | + take the node value | a + then pre-order left subtree (recursive) | / \ + and finally pre-order right subtree (recursive) | i x +In-order traversal of this tree is [i, a, f, x, r] | / \ + in-order left subtree (recursive) | f r + then take the node value | + and finally in-order right subtree (recursive) | + +Implement an algorithm to rebuild a tree from its pre-order and in-order +traversals. + +Bonus Points: +Consider the case when the traversals contain repeated elements. There may be +multiple possible trees. Find all of them. + +Save the trees! diff --git a/exercises/reforestation/metadata.yaml b/exercises/reforestation/metadata.yaml new file mode 100644 index 0000000000..b02689e468 --- /dev/null +++ b/exercises/reforestation/metadata.yaml @@ -0,0 +1,3 @@ +--- +title: "Reforestation" +blurb: "Rebuild binary trees from pre-order and in-order traversals." From 8b9c506fa0b48b51ab95319377ef383ed940e287 Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Sun, 30 Dec 2018 15:01:21 -0600 Subject: [PATCH 02/11] reforestation: renamed metadata.yaml => metadata.yml --- exercises/reforestation/{metadata.yaml => metadata.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename exercises/reforestation/{metadata.yaml => metadata.yml} (100%) diff --git a/exercises/reforestation/metadata.yaml b/exercises/reforestation/metadata.yml similarity index 100% rename from exercises/reforestation/metadata.yaml rename to exercises/reforestation/metadata.yml From ac679a47e6df203028391d3ccd49488042ec954e Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Sun, 30 Dec 2018 21:02:08 -0600 Subject: [PATCH 03/11] Fix description.md formatting --- exercises/reforestation/description.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/exercises/reforestation/description.md b/exercises/reforestation/description.md index e40b0c687a..289f01ef18 100644 --- a/exercises/reforestation/description.md +++ b/exercises/reforestation/description.md @@ -3,19 +3,20 @@ pre-order and in-order traversals (post-order and in-order also work). Tree traversals start at the root of the tree and can be defined recursively. -Pre-order traversal of this tree is [a, i, x, f, r] | - take the node value | a - then pre-order left subtree (recursive) | / \ - and finally pre-order right subtree (recursive) | i x -In-order traversal of this tree is [i, a, f, x, r] | / \ - in-order left subtree (recursive) | f r - then take the node value | - and finally in-order right subtree (recursive) | + Pre-order traversal of this tree is [a, i, x, f, r] | + take the node value | a + then pre-order left subtree (recursive) | / \ + and finally pre-order right subtree (recursive) | i x + In-order traversal of this tree is [i, a, f, x, r] | / \ + in-order left subtree (recursive) | f r + then take the node value | + and finally in-order right subtree (recursive) | Implement an algorithm to rebuild a tree from its pre-order and in-order traversals. -Bonus Points: +Bonus Points: + Consider the case when the traversals contain repeated elements. There may be multiple possible trees. Find all of them. From 772dd6e9617a72bb5d3281a10a6bee0ac2019f68 Mon Sep 17 00:00:00 2001 From: Ryan Potts Date: Tue, 1 Jan 2019 10:44:40 -0600 Subject: [PATCH 04/11] Remove abbreviation from canonical-data.json Co-Authored-By: fidelcoria <1coriaF@gmail.com> --- exercises/reforestation/canonical-data.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/reforestation/canonical-data.json b/exercises/reforestation/canonical-data.json index c045741be6..830ebf9ace 100644 --- a/exercises/reforestation/canonical-data.json +++ b/exercises/reforestation/canonical-data.json @@ -21,7 +21,7 @@ "expected": {"v":"a", "l": {}, "r": {}} }, { - "description": "Core functionality w/ realistic size traversal", + "description": "Core functionality with realistic size traversal", "property": "treeTraversals", "input": { "preorder": ["a","i","x","f","r"], From 8032e2451fa67818aa42142a8a2d9fd06ecc5016 Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Tue, 1 Jan 2019 11:02:49 -0600 Subject: [PATCH 05/11] Rename exercise: retree --- exercises/{reforestation => retree}/canonical-data.json | 2 +- exercises/{reforestation => retree}/description.md | 0 exercises/{reforestation => retree}/metadata.yml | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename exercises/{reforestation => retree}/canonical-data.json (98%) rename exercises/{reforestation => retree}/description.md (100%) rename exercises/{reforestation => retree}/metadata.yml (76%) diff --git a/exercises/reforestation/canonical-data.json b/exercises/retree/canonical-data.json similarity index 98% rename from exercises/reforestation/canonical-data.json rename to exercises/retree/canonical-data.json index 830ebf9ace..b908e3c39e 100644 --- a/exercises/reforestation/canonical-data.json +++ b/exercises/retree/canonical-data.json @@ -1,5 +1,5 @@ { - "exercise": "reforestation", + "exercise": "retree", "version": "1.0.0", "cases": [ { diff --git a/exercises/reforestation/description.md b/exercises/retree/description.md similarity index 100% rename from exercises/reforestation/description.md rename to exercises/retree/description.md diff --git a/exercises/reforestation/metadata.yml b/exercises/retree/metadata.yml similarity index 76% rename from exercises/reforestation/metadata.yml rename to exercises/retree/metadata.yml index b02689e468..88b5694d0f 100644 --- a/exercises/reforestation/metadata.yml +++ b/exercises/retree/metadata.yml @@ -1,3 +1,3 @@ --- -title: "Reforestation" +title: "Retree" blurb: "Rebuild binary trees from pre-order and in-order traversals." From d40926023d28f17fa9d9371c358cff59040f112b Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Thu, 3 Jan 2019 21:42:12 -0600 Subject: [PATCH 06/11] Retree: Clean canonical-data.json --- exercises/retree/canonical-data.json | 60 ++++++---------------------- 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/exercises/retree/canonical-data.json b/exercises/retree/canonical-data.json index b908e3c39e..eb8a3967ff 100644 --- a/exercises/retree/canonical-data.json +++ b/exercises/retree/canonical-data.json @@ -3,8 +3,8 @@ "version": "1.0.0", "cases": [ { - "description": "Base case: empty tree", - "property": "treeTraversals", + "description": "Empty tree", + "property": "treeFromTraversals", "input": { "preorder": [], "inorder": [] @@ -12,8 +12,8 @@ "expected": {} }, { - "description": "1 more step than base case", - "property": "treeTraversals", + "description": "Tree with one item", + "property": "treeFromTraversals", "input": { "preorder": ["a"], "inorder": ["a"] @@ -22,10 +22,10 @@ }, { "description": "Core functionality with realistic size traversal", - "property": "treeTraversals", + "property": "treeFromTraversals", "input": { - "preorder": ["a","i","x","f","r"], - "inorder": ["i","a","f","x","r"] + "preorder": ["a", "i", "x", "f", "r"], + "inorder": ["i", "a", "f", "x", "r"] }, "expected": {"v": "a", "l": {"v": "i", "l": {}, "r": {}}, @@ -37,57 +37,21 @@ }, { "description": "Traversals must be fully consumed. No extra values.", - "property": "treeTraversals", + "property": "treeFromTraversals", "input": { "preorder": ["a", "b"], "inorder": ["b", "a", "r"] }, - "expected": {"error": "invalid traversal"} + "expected": {"error": "traversals should have the same length"} }, { "description": "Reject inconsistent traversals of same length", - "property": "treeTraversals", + "property": "treeFromTraversals", "input": { - "preorder": ["x","y","z"], - "inorder": ["a","b","c"] + "preorder": ["x", "y", "z"], + "inorder": ["a", "b", "c"] }, "expected": {"error": "traversals are not consistent"} - }, - { - "description": "BonusPoints: generate multiple trees", - "property": "treeTraversals", - "input": { - "preorder": ["p","r","o","l","o","g"], - "inorder": ["o","l","o","r","p","g"] - }, - "expected": [ - {"v":"p", - "l": {"v": "r", - "l": {"v": "o", - "l": {"v": "l", - "l": {"v": "o", "l": {}, "r": {}}, - "r": {} - }, - "r": {} - }, - "r": {} - }, - "r": {"v": "g", "l": {}, "r": {}} - }, - {"v": "p", - "l": {"v": "r", - "l": {"v": "o", - "l": {}, - "r": {"v": "l", - "l": {}, - "r": {"v": "o", "l": {}, "r": {}} - } - }, - "r": {} - }, - "r": {"v": "g", "l": {}, "r": {}} - } - ] } ] } From e1bfb5f8e81569d5248c4ec22f84481053532981 Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Thu, 3 Jan 2019 22:19:49 -0600 Subject: [PATCH 07/11] Retree: rewrite description.md --- exercises/retree/description.md | 36 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/exercises/retree/description.md b/exercises/retree/description.md index 289f01ef18..a475767502 100644 --- a/exercises/retree/description.md +++ b/exercises/retree/description.md @@ -1,23 +1,25 @@ -A binary tree with no repeated elements can be represented uniquely by its -pre-order and in-order traversals (post-order and in-order also work). +Imagine you need to transmit a binary tree to a satellite approaching Alpha +Centauri and you have limited bandwidth. Since the tree has no repeating +items it can be uniquely represented by its pre-order and in-order traversals. -Tree traversals start at the root of the tree and can be defined recursively. +Write the software for the satellite to rebuild the tree from the traversals. - Pre-order traversal of this tree is [a, i, x, f, r] | - take the node value | a - then pre-order left subtree (recursive) | / \ - and finally pre-order right subtree (recursive) | i x - In-order traversal of this tree is [i, a, f, x, r] | / \ - in-order left subtree (recursive) | f r - then take the node value | - and finally in-order right subtree (recursive) | +A pre-order traversal reads the value of the current node before (hence "pre") +reading the left subtree in pre-order. Afterwards the right subtree is read +in pre-order. -Implement an algorithm to rebuild a tree from its pre-order and in-order -traversals. +An in-order traversal reads the left subtree in-order then the current node and +finally the right subtree in-order. So in order from left to right. -Bonus Points: +For example the pre-order traversal of this tree is [a, i, x, f, r]. +The in-order traversal of this tree is [i, a, f, x, r] -Consider the case when the traversals contain repeated elements. There may be -multiple possible trees. Find all of them. +``` + a + / \ +i x + / \ + f r +``` -Save the trees! +(note: the first item in the pre-order traversal is always the root) From dc8b9082e65c41f40514f9fe258bdb9d220b1e90 Mon Sep 17 00:00:00 2001 From: Fidel Coria <1coriaF@gmail.com> Date: Fri, 4 Jan 2019 07:47:10 -0600 Subject: [PATCH 08/11] Retree: Apply description.md suggestions from code review Co-Authored-By: fidelcoria <1coriaF@gmail.com> --- exercises/retree/canonical-data.json | 8 ++++---- exercises/retree/description.md | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/exercises/retree/canonical-data.json b/exercises/retree/canonical-data.json index eb8a3967ff..c4321cb129 100644 --- a/exercises/retree/canonical-data.json +++ b/exercises/retree/canonical-data.json @@ -21,7 +21,7 @@ "expected": {"v":"a", "l": {}, "r": {}} }, { - "description": "Core functionality with realistic size traversal", + "description": "Tree with many items", "property": "treeFromTraversals", "input": { "preorder": ["a", "i", "x", "f", "r"], @@ -36,13 +36,13 @@ } }, { - "description": "Traversals must be fully consumed. No extra values.", + "description": "Reject traversals of different length", "property": "treeFromTraversals", "input": { "preorder": ["a", "b"], "inorder": ["b", "a", "r"] }, - "expected": {"error": "traversals should have the same length"} + "expected": {"error": "traversals must have the same length"} }, { "description": "Reject inconsistent traversals of same length", @@ -51,7 +51,7 @@ "preorder": ["x", "y", "z"], "inorder": ["a", "b", "c"] }, - "expected": {"error": "traversals are not consistent"} + "expected": {"error": "traversals must have the same elements"} } ] } diff --git a/exercises/retree/description.md b/exercises/retree/description.md index a475767502..0aa30c37c1 100644 --- a/exercises/retree/description.md +++ b/exercises/retree/description.md @@ -1,6 +1,6 @@ Imagine you need to transmit a binary tree to a satellite approaching Alpha Centauri and you have limited bandwidth. Since the tree has no repeating -items it can be uniquely represented by its pre-order and in-order traversals. +items it can be uniquely represented by its [pre-order and in-order traversals][wiki]. Write the software for the satellite to rebuild the tree from the traversals. @@ -22,4 +22,6 @@ i x f r ``` -(note: the first item in the pre-order traversal is always the root) +Note: the first item in the pre-order traversal is always the root. + +[wiki]: https://en.wikipedia.org/wiki/Tree_traversal From 85836c7cda8bc1d88120a5d665c9bae2b424fc8d Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Fri, 4 Jan 2019 08:07:33 -0600 Subject: [PATCH 09/11] Retree: Add test for unique items --- exercises/retree/canonical-data.json | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/exercises/retree/canonical-data.json b/exercises/retree/canonical-data.json index c4321cb129..7743cb1f74 100644 --- a/exercises/retree/canonical-data.json +++ b/exercises/retree/canonical-data.json @@ -52,6 +52,15 @@ "inorder": ["a", "b", "c"] }, "expected": {"error": "traversals must have the same elements"} + }, + { + "description": "Reject traversals with repeated items", + "property": "treeFromTraversals", + "input": { + "preorder": ["a", "b", "a"], + "inorder": ["b", "a", "a"] + }, + "expected": {"error": "traversals must contain unique items"} } ] } From af674e7973f54d36d6401ef5768087171b11e414 Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Sun, 6 Jan 2019 21:51:36 -0600 Subject: [PATCH 10/11] Retree: fix indentation of canonical-data --- exercises/retree/canonical-data.json | 120 +++++++++++++-------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/exercises/retree/canonical-data.json b/exercises/retree/canonical-data.json index 7743cb1f74..0676a6c119 100644 --- a/exercises/retree/canonical-data.json +++ b/exercises/retree/canonical-data.json @@ -1,66 +1,66 @@ { - "exercise": "retree", - "version": "1.0.0", - "cases": [ - { - "description": "Empty tree", - "property": "treeFromTraversals", - "input": { - "preorder": [], - "inorder": [] - }, - "expected": {} + "exercise": "retree", + "version": "1.0.0", + "cases": [ + { + "description": "Empty tree", + "property": "treeFromTraversals", + "input": { + "preorder": [], + "inorder": [] }, - { - "description": "Tree with one item", - "property": "treeFromTraversals", - "input": { - "preorder": ["a"], - "inorder": ["a"] - }, - "expected": {"v":"a", "l": {}, "r": {}} + "expected": {} + }, + { + "description": "Tree with one item", + "property": "treeFromTraversals", + "input": { + "preorder": ["a"], + "inorder": ["a"] }, - { - "description": "Tree with many items", - "property": "treeFromTraversals", - "input": { - "preorder": ["a", "i", "x", "f", "r"], - "inorder": ["i", "a", "f", "x", "r"] - }, - "expected": {"v": "a", - "l": {"v": "i", "l": {}, "r": {}}, - "r": {"v": "x", - "l": {"v": "f", "l": {}, "r": {}}, - "r": {"v": "r", "l": {}, "r": {}} - } - } + "expected": {"v":"a", "l": {}, "r": {}} + }, + { + "description": "Tree with many items", + "property": "treeFromTraversals", + "input": { + "preorder": ["a", "i", "x", "f", "r"], + "inorder": ["i", "a", "f", "x", "r"] }, - { - "description": "Reject traversals of different length", - "property": "treeFromTraversals", - "input": { - "preorder": ["a", "b"], - "inorder": ["b", "a", "r"] - }, - "expected": {"error": "traversals must have the same length"} + "expected": {"v": "a", + "l": {"v": "i", "l": {}, "r": {}}, + "r": {"v": "x", + "l": {"v": "f", "l": {}, "r": {}}, + "r": {"v": "r", "l": {}, "r": {}} + } + } + }, + { + "description": "Reject traversals of different length", + "property": "treeFromTraversals", + "input": { + "preorder": ["a", "b"], + "inorder": ["b", "a", "r"] }, - { - "description": "Reject inconsistent traversals of same length", - "property": "treeFromTraversals", - "input": { - "preorder": ["x", "y", "z"], - "inorder": ["a", "b", "c"] - }, - "expected": {"error": "traversals must have the same elements"} + "expected": {"error": "traversals must have the same length"} + }, + { + "description": "Reject inconsistent traversals of same length", + "property": "treeFromTraversals", + "input": { + "preorder": ["x", "y", "z"], + "inorder": ["a", "b", "c"] }, - { - "description": "Reject traversals with repeated items", - "property": "treeFromTraversals", - "input": { - "preorder": ["a", "b", "a"], - "inorder": ["b", "a", "a"] - }, - "expected": {"error": "traversals must contain unique items"} - } - ] - } + "expected": {"error": "traversals must have the same elements"} + }, + { + "description": "Reject traversals with repeated items", + "property": "treeFromTraversals", + "input": { + "preorder": ["a", "b", "a"], + "inorder": ["b", "a", "a"] + }, + "expected": {"error": "traversals must contain unique items"} + } + ] +} From cb0cddedd02e776f1585e0a028163c25397cd54a Mon Sep 17 00:00:00 2001 From: Fidel Coria Date: Mon, 7 Jan 2019 17:46:47 -0600 Subject: [PATCH 11/11] Retree: Fix colon spacing --- exercises/retree/canonical-data.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/retree/canonical-data.json b/exercises/retree/canonical-data.json index 0676a6c119..d7153b6606 100644 --- a/exercises/retree/canonical-data.json +++ b/exercises/retree/canonical-data.json @@ -18,7 +18,7 @@ "preorder": ["a"], "inorder": ["a"] }, - "expected": {"v":"a", "l": {}, "r": {}} + "expected": {"v": "a", "l": {}, "r": {}} }, { "description": "Tree with many items",