-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e9b1f6
commit ac71b43
Showing
12 changed files
with
244 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"fantomas-tool": { | ||
"version": "4.7.9", | ||
"commands": [ | ||
"fantomas" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Satellite | ||
|
||
type Tree = | ||
| Empty | ||
| Node of value: string * left: Tree * right: Tree | ||
|
||
let rec createTree inorder preorder = | ||
match preorder with | ||
| [] -> Empty | ||
| hd :: tail -> | ||
let hdIdx = inorder |> List.findIndex (fun x -> x = hd) | ||
let leftInorder, rightInorder = inorder[0 .. hdIdx - 1], inorder[hdIdx + 1 ..] | ||
let leftPreorder, rightPreorder = tail[0 .. leftInorder.Length - 1], tail[leftInorder.Length ..] | ||
Node(hd, createTree leftInorder leftPreorder, createTree rightInorder rightPreorder) | ||
|
||
let treeFromTraversals preorder inorder = | ||
if List.length preorder <> List.length inorder then | ||
Error "traversals must have the same length" | ||
elif List.sort preorder <> List.sort inorder then | ||
Error "traversals must have the same elements" | ||
elif List.distinct preorder <> preorder then | ||
Error "traversals must contain unique items" | ||
else | ||
Ok(createTree preorder inorder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Satellite.fs" | ||
], | ||
"test": [ | ||
"SatelliteTests.fs" | ||
], | ||
"example": [ | ||
".meta/Example.fs" | ||
] | ||
}, | ||
"blurb": "Rebuild binary trees from pre-order and in-order traversals." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Satellite | ||
|
||
type Tree = | ||
| Empty | ||
| Node of value: string * left: Tree * right: Tree | ||
|
||
let treeFromTraversals preorder inorder = | ||
failwith "Please implement the 'treeFromTraversals' function" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<GenerateProgramFile>false</GenerateProgramFile> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="Satellite.fs" /> | ||
<Compile Include="SatelliteTests.fs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" /> | ||
<PackageReference Include="FsUnit.xUnit" Version="4.0.4" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module SatelliteTests | ||
|
||
open FsUnit.Xunit | ||
open Xunit | ||
|
||
open Satellite | ||
|
||
[<Fact>] | ||
let ``Empty tree`` () = | ||
let expected: Result<Tree,string> = Ok ( | ||
Empty | ||
) | ||
treeFromTraversals [] [] |> should equal expected | ||
|
||
[<Fact>] | ||
let ``Tree with one item`` () = | ||
let expected: Result<Tree,string> = Ok ( | ||
Node("a", Empty, Empty) | ||
) | ||
treeFromTraversals ["a"] ["a"] |> should equal expected | ||
|
||
[<Fact>] | ||
let ``Tree with many items`` () = | ||
let expected: Result<Tree,string> = Ok ( | ||
Node( | ||
"a", | ||
Node("i", Empty, Empty), | ||
Node( | ||
"x", | ||
Node("f", Empty, Empty), | ||
Node("r", Empty, Empty) | ||
) | ||
) | ||
) | ||
treeFromTraversals ["i"; "a"; "f"; "x"; "r"] ["a"; "i"; "x"; "f"; "r"] |> should equal expected | ||
|
||
[<Fact>] | ||
let ``Reject traversals of different length`` () = | ||
let expected: Result<Tree,string> = Error "traversals must have the same length" | ||
treeFromTraversals ["b"; "a"; "r"] ["a"; "b"] |> should equal expected | ||
|
||
[<Fact>] | ||
let ``Reject inconsistent traversals of same length`` () = | ||
let expected: Result<Tree,string> = Error "traversals must have the same elements" | ||
treeFromTraversals ["a"; "b"; "c"] ["x"; "y"; "z"] |> should equal expected | ||
|
||
[<Fact>] | ||
let ``Reject traversals with repeated items`` () = | ||
let expected: Result<Tree,string> = Error "traversals must contain unique items" | ||
treeFromTraversals ["b"; "a"; "a"] ["a"; "b"; "a"] |> should equal expected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.