Skip to content
Open
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 @@ -1173,6 +1173,14 @@
"recursion"
]
},
{
"slug": "satellite",
"name": "Satellite",
"uuid": "da0fa224-6095-4382-b869-f4e3953e09f6",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "sublist",
"name": "Sublist",
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
25 changes: 25 additions & 0 deletions exercises/practice/satellite/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"satellite.ts"
],
"test": [
"satellite.test.ts"
],
"example": [
".meta/proof.ci.ts"
]
},
"custom": {
"version.tests.compatibility": "jest-29",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false,
"flag.tests.jest": true,
"flag.tests.tstyche": false
},
"blurb": "Rebuild binary trees from pre-order and in-order traversals."
}
56 changes: 56 additions & 0 deletions exercises/practice/satellite/.meta/proof.ci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export type Tree = {
v?: string
l?: Tree
r?: Tree
}

const hasUniqueItems = (items: string[]): boolean =>
new Set(items).size === items.length

const hasSameItems = (left: string[], right: string[]): boolean => {
const rightItems = new Set(right)
return left.every((item) => rightItems.has(item))
}

const validateTraversals = (preorder: string[], inorder: string[]): void => {
if (preorder.length !== inorder.length) {
throw new Error('traversals must have the same length')
}

if (!hasUniqueItems(preorder) || !hasUniqueItems(inorder)) {
throw new Error('traversals must contain unique items')
}

if (!hasSameItems(preorder, inorder)) {
throw new Error('traversals must have the same elements')
}
}

const buildTree = (preorder: string[], inorder: string[]): Tree => {
if (preorder.length === 0) {
return {}
}

const [root, ...remainingPreorder] = preorder
const rootIndex = inorder.indexOf(root)
const leftInorder = inorder.slice(0, rootIndex)
const rightInorder = inorder.slice(rootIndex + 1)
const leftSize = leftInorder.length
const leftPreorder = remainingPreorder.slice(0, leftSize)
const rightPreorder = remainingPreorder.slice(leftSize)

return {
v: root,
l: buildTree(leftPreorder, leftInorder),
r: buildTree(rightPreorder, rightInorder),
}
}

export const treeFromTraversals = (
preorder: string[],
inorder: string[]
): Tree => {
validateTraversals(preorder, inorder)

return buildTree(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"
7 changes: 7 additions & 0 deletions exercises/practice/satellite/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"arcanis.vscode-zipfs",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
7 changes: 7 additions & 0 deletions exercises/practice/satellite/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": ["exercism"],
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
}
}
3 changes: 3 additions & 0 deletions exercises/practice/satellite/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
compressionLevel: mixed

enableGlobalCache: true
5 changes: 5 additions & 0 deletions exercises/practice/satellite/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
// eslint-disable-next-line @typescript-eslint/no-require-imports
presets: [[require('@exercism/babel-preset-typescript'), { corejs: '3.38' }]],
plugins: [],
}
26 changes: 26 additions & 0 deletions exercises/practice/satellite/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-check

import tsEslint from 'typescript-eslint'
import config from '@exercism/eslint-config-typescript'
import maintainersConfig from '@exercism/eslint-config-typescript/maintainers.mjs'

export default [
...tsEslint.config(...config, {
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
extends: maintainersConfig,
}),
{
ignores: [
// # Protected or generated
'.git/**/*',
'.vscode/**/*',

//# When using npm
'node_modules/**/*',

// # Configuration files
'babel.config.cjs',
'jest.config.cjs',
],
},
]
22 changes: 22 additions & 0 deletions exercises/practice/satellite/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
moduleNameMapper: {
'^(\\.\\/.+)\\.js$': '$1',
},
}
38 changes: 38 additions & 0 deletions exercises/practice/satellite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@exercism/typescript-satellite",
"version": "1.0.0",
"description": "Exercism exercises in Typescript.",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/typescript"
},
"type": "module",
"engines": {
"node": "^18.16.0 || >=20.0.0"
},
"devDependencies": {
"@exercism/babel-preset-typescript": "^0.6.0",
"@exercism/eslint-config-typescript": "^0.8.0",
"@jest/globals": "^29.7.0",
"@types/node": "~22.7.6",
"babel-jest": "^29.7.0",
"core-js": "~3.38.1",
"eslint": "^9.12.0",
"expect": "^29.7.0",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"tstyche": "^2.1.1",
"typescript": "~5.6.3",
"typescript-eslint": "^8.10.0"
},
"scripts": {
"test": "corepack yarn node test-runner.mjs",
"test:types": "corepack yarn tstyche",
"test:implementation": "corepack yarn jest --no-cache --passWithNoTests",
"lint": "corepack yarn lint:types && corepack yarn lint:ci",
"lint:types": "corepack yarn tsc --noEmit -p .",
"lint:ci": "corepack yarn eslint . --ext .tsx,.ts"
},
"packageManager": "yarn@4.5.1"
}
Loading
Loading