-
-
Notifications
You must be signed in to change notification settings - Fork 379
Add zipper exercise #179
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
Merged
Merged
Add zipper exercise #179
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,137 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public class BinTree<T> : IEquatable<BinTree<T>> | ||
| { | ||
| public BinTree(T value, BinTree<T> left, BinTree<T> right) | ||
| { | ||
| Value = value; | ||
| Left = left; | ||
| Right = right; | ||
| } | ||
|
|
||
| public BinTree(BinTree<T> tree) : this(tree.Value, tree.Left, tree.Right) | ||
| { | ||
| } | ||
|
|
||
| public T Value { get; } | ||
| public BinTree<T> Left { get; } | ||
| public BinTree<T> Right { get; } | ||
|
|
||
| public bool Equals(BinTree<T> other) | ||
| { | ||
| if (other == null || !Equals(Value, other.Value)) | ||
| return false; | ||
|
|
||
| if (!ReferenceEquals(Left, other.Left) && (!Left?.Equals(other.Left) ?? false)) | ||
| return false; | ||
|
|
||
| if (!ReferenceEquals(Right, other.Right) && (!Right?.Equals(other.Right) ?? false)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| public abstract class BinTreeCrumb<T> | ||
| { | ||
| public BinTreeCrumb(T value, BinTree<T> tree) | ||
| { | ||
| Value = value; | ||
| Tree = tree; | ||
| } | ||
|
|
||
| public T Value { get; } | ||
| public BinTree<T> Tree { get; } | ||
| } | ||
|
|
||
| public class BinTreeLeftCrumb<T> : BinTreeCrumb<T> | ||
| { | ||
| public BinTreeLeftCrumb(T value, BinTree<T> tree) : base(value, tree) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| public class BinTreeRightCrumb<T> : BinTreeCrumb<T> | ||
| { | ||
| public BinTreeRightCrumb(T value, BinTree<T> tree) : base(value, tree) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| public class Zipper<T> | ||
| { | ||
| private readonly T value; | ||
| private readonly BinTree<T> left; | ||
| private readonly BinTree<T> right; | ||
| private readonly List<BinTreeCrumb<T>> crumbs; | ||
|
|
||
| public Zipper(T value, BinTree<T> left, BinTree<T> right, List<BinTreeCrumb<T>> crumbs) | ||
| { | ||
| this.value = value; | ||
| this.left = left; | ||
| this.right = right; | ||
| this.crumbs = crumbs; | ||
| } | ||
|
|
||
| public T Value => value; | ||
|
|
||
| public Zipper<T> SetValue(T newValue) => new Zipper<T>(newValue, left, right, crumbs); | ||
|
|
||
| public Zipper<T> SetLeft(BinTree<T> binTree) => new Zipper<T>(value, binTree, right, crumbs); | ||
|
|
||
| public Zipper<T> SetRight(BinTree<T> binTree) => new Zipper<T>(value, left, binTree, crumbs); | ||
|
|
||
| public Zipper<T> Left() | ||
| { | ||
| if (left == null) | ||
| return null; | ||
|
|
||
| var newCrumbs = new[] { new BinTreeLeftCrumb<T>(value, right) }.Concat(crumbs).ToList(); | ||
| return new Zipper<T>(left.Value, left.Left, left.Right, newCrumbs); | ||
| } | ||
|
|
||
| public Zipper<T> Right() | ||
| { | ||
| if (right == null) | ||
| return null; | ||
|
|
||
| var newCrumbs = new[] { new BinTreeRightCrumb<T>(value, left) }.Concat(crumbs).ToList(); | ||
| return new Zipper<T>(right.Value, right.Left, right.Right, newCrumbs); | ||
| } | ||
|
|
||
| public Zipper<T> Up() | ||
| { | ||
| if (crumbs.Count == 0) | ||
| return null; | ||
|
|
||
| var firstCrumb = crumbs[0]; | ||
| var remainingCrumbs = crumbs.Skip(1).ToList(); | ||
|
|
||
| if (firstCrumb is BinTreeLeftCrumb<T>) | ||
| return new Zipper<T>(firstCrumb.Value, new BinTree<T>(value, left, right), firstCrumb.Tree, remainingCrumbs); | ||
|
|
||
| if (firstCrumb is BinTreeRightCrumb<T>) | ||
| return new Zipper<T>(firstCrumb.Value, firstCrumb.Tree, new BinTree<T>(value, left, right), remainingCrumbs); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public BinTree<T> ToTree() | ||
| { | ||
| var tree = new BinTree<T>(value, left, right); | ||
|
|
||
| foreach (var crumb in crumbs) | ||
| { | ||
| if (crumb is BinTreeLeftCrumb<T>) | ||
| tree = new BinTree<T>(crumb.Value, new BinTree<T>(tree), crumb.Tree); | ||
| if (crumb is BinTreeRightCrumb<T>) | ||
| tree = new BinTree<T>(crumb.Value, crumb.Tree, new BinTree<T>(tree)); | ||
| } | ||
|
|
||
| return tree; | ||
| } | ||
|
|
||
| public static Zipper<T> FromTree(BinTree<T> tree) => new Zipper<T>(tree.Value, tree.Left, tree.Right, new List<BinTreeCrumb<T>>()); | ||
| } | ||
This file contains hidden or 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,83 @@ | ||
| using NUnit.Framework; | ||
|
|
||
| public class ZipperTest | ||
| { | ||
| private static BinTree<int> bt(int v, BinTree<int> l, BinTree<int> r) => new BinTree<int>(v, l, r); | ||
| private static BinTree<int> leaf(int v) => bt(v, null, null); | ||
|
|
||
| private static readonly BinTree<int> empty = null; | ||
| private static readonly BinTree<int> t1 = new BinTree<int>(1, bt(2, empty, leaf(3)), leaf(4)); | ||
| private static readonly BinTree<int> t2 = new BinTree<int>(1, bt(5, empty, leaf(3)), leaf(4)); | ||
| private static readonly BinTree<int> t3 = new BinTree<int>(1, bt(2, leaf(5), leaf(3)), leaf(4)); | ||
| private static readonly BinTree<int> t4 = new BinTree<int>(1, leaf(2), leaf(4)); | ||
|
|
||
| [Test] | ||
| public void Data_is_retained() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| var tree = zipper.ToTree(); | ||
| Assert.That(tree, Is.EqualTo(t1)); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Left_right_and_value() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| Assert.That(zipper.Left().Right().Value, Is.EqualTo(3)); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Dead_end() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| Assert.That(zipper.Left().Left(), Is.Null); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Tree_from_deep_focus() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| Assert.That(zipper.Left().Right().ToTree(), Is.EqualTo(t1)); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Set_value() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| var updatedZipper = zipper.Left().SetValue(5); | ||
| var tree = updatedZipper.ToTree(); | ||
| Assert.That(tree, Is.EqualTo(t2)); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Set_left_with_value() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| var updatedZipper = zipper.Left().SetLeft(new BinTree<int>(5, null, null)); | ||
| var tree = updatedZipper.ToTree(); | ||
| Assert.That(tree, Is.EqualTo(t3)); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Set_right_to_null() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| var updatedZipper = zipper.Left().SetRight(null); | ||
| var tree = updatedZipper.ToTree(); | ||
| Assert.That(tree, Is.EqualTo(t4)); | ||
| } | ||
|
|
||
| [Ignore("Remove to run test")] | ||
| [Test] | ||
| public void Different_paths_to_same_zipper() | ||
| { | ||
| var zipper = Zipper<int>.FromTree(t1); | ||
| Assert.That(zipper.Left().Up().Right().ToTree(), Is.EqualTo(zipper.Right().ToTree())); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're giving into the dark side @ErikSchierboom :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did :)