Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add satellite exercise #398

Merged
merged 1 commit into from
Jun 4, 2024
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 @@ -780,6 +780,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "satellite",
"name": "Satellite",
"uuid": "02d23d01-c4c6-4fd1-9e0b-f6a457c402ed",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "knapsack",
"name": "Knapsack",
Expand Down
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": [
"keiravillekode"
],
"files": {
"solution": [
"satellite.el"
],
"test": [
"satellite-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "Rebuild binary trees from pre-order and in-order traversals."
}
31 changes: 31 additions & 0 deletions exercises/practice/satellite/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
;;; satellite.el --- Satellite (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)
(require 'seq)

(defun tree-from-traversals (preorder inorder)
(when (/= (length preorder) (length inorder))
(error "traversals must have the same length"))
(when (or (/= (length preorder) (length (seq-uniq preorder)))
(/= (length inorder) (length (seq-uniq inorder))))
(error "traversals must contain unique items"))
(cl-labels
((traverse (successor)
(cond
((and successor (equal (car inorder) successor)) nil)
((null preorder) (error "traversals must have the same elements"))
(t (let ((v (car preorder)))
(setq preorder (cdr preorder))
(let ((l (funcall #'traverse v)))
(setq inorder (cdr inorder))
(let ((r (and inorder (funcall #'traverse successor))))
(list (cons :v v) (cons :l l) (cons :r r)))))))))
(and preorder (funcall #'traverse nil))))

(provide 'satellite)
;;; satellite.el ends here

28 changes: 28 additions & 0 deletions exercises/practice/satellite/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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"
52 changes: 52 additions & 0 deletions exercises/practice/satellite/satellite-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
;;; satellite-test.el --- Tests for Satellite (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "satellite.el")
(declare-function tree-from-traversals "satellite.el" (preorder inorder))


(ert-deftest empty-tree ()
(should (equal nil
(tree-from-traversals '() '()))))


(ert-deftest tree-with-one-item ()
(should (equal '((:v . "a")
(:l . nil)
(:r . nil))
(tree-from-traversals '("a") '("a")))))


(ert-deftest tree-with-many-items ()
(should (equal '((:v . "a")
(:l . ((:v . "i")
(:l . nil)
(:r . nil)))
(:r . ((:v . "x")
(:l . ((:v . "f")
(:l . nil)
(:r . nil)))
(:r . ((:v . "r")
(:l . nil)
(:r . nil))))))
(tree-from-traversals '("a" "i" "x" "f" "r") '("i" "a" "f" "x" "r")))))


(ert-deftest reject-traversals-of-different-length ()
(should-error (tree-from-traversals '("a" "b") '("b" "a" "r"))))


(ert-deftest reject-inconsistent-traversals-of-same-length ()
(should-error (tree-from-traversals '("x" "y" "z") '("a" "b" "c"))))


(ert-deftest reject-traversals-with-repeated-items ()
(should-error (tree-from-traversals '("a" "b" "a") '("b" "a" "a"))))


(provide 'satellite-test)
;;; satellite-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/satellite/satellite.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; satellite.el --- Satellite (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun tree-from-traversals (preorder inorder)
(error "Delete this S-Expression and write your own implementation"))


(provide 'satellite)
;;; satellite.el ends here