Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "satellite",
"name": "Satellite",
"uuid": "689501f5-1404-4358-b1f2-aea7798352c9",
"practices": [],
"prerequisites": [],
"difficulty": 7
}
]
},
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/satellite/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
27 changes: 27 additions & 0 deletions exercises/practice/satellite/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

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][wiki].

Write the software for the satellite to rebuild the tree from the traversals.

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.

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.

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]

```text
a
/ \
i x
/ \
f r
```

Note: the first item in the pre-order traversal is always the root.

[wiki]: https://en.wikipedia.org/wiki/Tree_traversal
17 changes: 17 additions & 0 deletions exercises/practice/satellite/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"satellite.moon"
],
"test": [
"satellite_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"blurb": "Rebuild binary trees from pre-order and in-order traversals."
}
35 changes: 35 additions & 0 deletions exercises/practice/satellite/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
indexof = (list, elem) ->
for i, v in ipairs list
if v == elem
return i
return -1

uniq = (list) ->
seen = {}
for elem in *list
return false if seen[elem]
seen[elem] = true
true


do_tree = (preorder, inorder) ->
return {} if #preorder == 0

root = preorder[1]
idx = indexof inorder, root
assert idx > 0, "traversals must have the same elements"

return {
v: root,
l: do_tree {table.unpack preorder, 2, idx}, {table.unpack inorder, 1, idx - 1},
r: do_tree {table.unpack preorder, idx + 1, #preorder}, {table.unpack inorder, idx + 1, #inorder}
}


{
tree: (preorder, inorder) ->
assert #preorder == #inorder, "traversals must have the same length"
assert uniq(preorder) and uniq(inorder), "traversals must contain unique items"

do_tree preorder, inorder
}
37 changes: 37 additions & 0 deletions exercises/practice/satellite/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

[8df3fa26-811a-4165-9286-ff9ac0850d19]
description = "Empty tree"

[f945ccfc-05e3-47d7-825b-0270559d43ad]
description = "Tree with one item"

[a0121d5f-37b0-48dd-9c64-cba4c4464135]
description = "Tree with many items"

[6074041f-4891-4d81-a128-401050c2a3b0]
description = "Reject traversals of different length"

[27916ce4-45f3-4d8b-8528-496fedc157ca]
description = "Reject inconsistent traversals of same length"

[d86a3d72-76a9-43b5-9d3a-e64cb1216035]
description = "Reject traversals with repeated items"

[af31ae02-7e5b-4452-a990-bccb3fca9148]
description = "A degenerate binary tree"

[ee54463d-a719-4aae-ade4-190d30ce7320]
description = "Another degenerate binary tree"

[87123c08-c155-4486-90a4-e2f75b0f3e8f]
description = "Tree with many more items"
4 changes: 4 additions & 0 deletions exercises/practice/satellite/satellite.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
tree: (preorder, inorder) ->
error 'Implement me'
}
96 changes: 96 additions & 0 deletions exercises/practice/satellite/satellite_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Satellite = require 'satellite'

describe 'satellite', ->
it "Empty tree", ->
result = Satellite.tree {}, {}
expected = {}
assert.are.same expected, result

pending "Tree with one item", ->
result = Satellite.tree {"a"}, {"a"}
expected = { v: "a", l: {}, r: {} }
assert.are.same expected, result

pending "Tree with many items", ->
result = Satellite.tree {"a", "i", "x", "f", "r"}, {"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: {} }
}
}
assert.are.same expected, result

pending "Reject traversals of different length", ->
f = -> Satellite.tree {"a", "b"}, {"b", "a", "r"}
assert.has.errors f, "traversals must have the same length"

pending "Reject inconsistent traversals of same length", ->
f = -> Satellite.tree {"x", "y", "z"}, {"a", "b", "c"}
assert.has.errors f, "traversals must have the same elements"

pending "Reject traversals with repeated items", ->
f = -> Satellite.tree {"a", "b", "a"}, {"b", "a", "a"}
assert.has.errors f, "traversals must contain unique items"

pending "A degenerate binary tree", ->
result = Satellite.tree {"a", "b", "c", "d"}, {"d", "c", "b", "a"}
expected = {
v: "a",
l: {
v: "b",
l: {
v: "c",
l: { v: "d", l: {}, r: {} },
r: {}
},
r: {}
},
r: {}
}
assert.are.same expected, result

pending "Another degenerate binary tree", ->
result = Satellite.tree {"a", "b", "c", "d"}, {"a", "b", "c", "d"}
expected = {
v: "a",
l: {},
r: {
v: "b",
l: {},
r: {
v: "c",
l: {},
r: { v: "d", l: {}, r: {} }
}
}
}
assert.are.same expected, result

pending "Tree with many more items", ->
result = Satellite.tree {"a", "b", "d", "g", "h", "c", "e", "f", "i"}, {"g", "d", "h", "b", "a", "e", "c", "i", "f"}
expected = {
v: "a",
l: {
v: "b",
l: {
v: "d",
l: { v: "g", l: {}, r: {} },
r: { v: "h", l: {}, r: {} }
},
r: {}
},
r: {
v: "c",
l: { v: "e", l: {}, r: {} },
r: {
v: "f",
l: { v: "i", l: {}, r: {} },
r: {}
}
}
}
assert.are.same expected, result
Loading