Last active
December 21, 2015 05:18
-
-
Save Buildstarted/6255558 to your computer and use it in GitHub Desktop.
Trying out a question posed by David Fowler
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
| public class ArrayStore : IStore | |
| { | |
| private List<int[]> _arrays; | |
| private int _rowIndex = -1; | |
| private int _index = -1; | |
| private int _capacity = DefaultCapacityPerLine; | |
| private const int DefaultCapacityPerLine = 20; | |
| public ArrayStore() : this(DefaultCapacityPerLine) { } | |
| public ArrayStore(int capacityPerLine) | |
| { | |
| _capacity = capacityPerLine; | |
| _arrays = new List<int[]>(); | |
| CreateArray(); | |
| } | |
| public IEnumerable<int> Get(int @from) | |
| { | |
| bool startReturn = false; | |
| for (int i = 0; i <= _rowIndex; i++) | |
| { | |
| if (!startReturn && _arrays[i][_capacity - 1] > @from) | |
| { | |
| //we've got our starting row | |
| for (int column = 0; column < _capacity; column++) | |
| { | |
| if (!startReturn && _arrays[i][column] > @from) | |
| { | |
| //start here | |
| startReturn = true; | |
| } | |
| if (startReturn) | |
| { | |
| yield return _arrays[i][column]; | |
| } | |
| } | |
| } | |
| else if (startReturn) | |
| { | |
| int columnMax = i == _arrays.Count - 1 ? _index : _capacity; | |
| for (int column = 0; column < columnMax; column++) | |
| { | |
| //if we're on the last row we need to ensure we don't go over the index | |
| yield return _arrays[i][column]; | |
| } | |
| } | |
| } | |
| } | |
| public void Add(int value) | |
| { | |
| if (_index >= _capacity) | |
| { | |
| //create a new array | |
| CreateArray(); | |
| } | |
| _arrays[_rowIndex][_index++] = value; | |
| } | |
| private void CreateArray() | |
| { | |
| var newArray = new int[_capacity]; | |
| _arrays.Add(newArray); | |
| _rowIndex++; | |
| _index = 0; | |
| } | |
| } |
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
| public class BstStore : IStore | |
| { | |
| private Node _root; | |
| public BstStore() | |
| { | |
| } | |
| public void Add(int value) | |
| { | |
| if (_root == null) | |
| { | |
| _root = new Node() | |
| { | |
| Value = value | |
| }; | |
| } | |
| else | |
| { | |
| AddTo(_root, value); | |
| } | |
| } | |
| private void AddTo(Node node, int value) | |
| { | |
| if (node == null) | |
| { | |
| node = new Node() { Value = value }; | |
| } | |
| else | |
| { | |
| if (value.CompareTo(node.Value) < 0) | |
| { | |
| if (node.Left == null) | |
| { | |
| node.Left = new Node() { Value = value }; | |
| } | |
| else | |
| { | |
| AddTo(node.Left, value); | |
| } | |
| } | |
| else | |
| { | |
| if (node.Right == null) | |
| { | |
| node.Right = new Node() { Value = value }; | |
| } | |
| else | |
| { | |
| AddTo(node.Right, value); | |
| } | |
| } | |
| } | |
| } | |
| private Node Find(Node node, int value) | |
| { | |
| if (node == null) | |
| { | |
| return null; | |
| } | |
| int comparison = value.CompareTo(node.Value); | |
| if (comparison >= 0) | |
| { | |
| return Find(node.Right, value); | |
| } | |
| return node; | |
| } | |
| private Node Find(int value) | |
| { | |
| return Find(_root, value); | |
| } | |
| public IEnumerable<int> Get(int @from) | |
| { | |
| var root = Find(@from); | |
| //since this is the root node we're looking for | |
| //all children should be > n | |
| //this is horrible as the tree should be self balanced | |
| //binary search tree seems rather pointless here | |
| return GetNodeValues(root); | |
| } | |
| private IEnumerable<int> GetNodeValues(Node node) | |
| { | |
| if (node == null) | |
| { | |
| yield break; | |
| } | |
| yield return node.Value; | |
| //this should never execute as all values are on the right since | |
| //every child node value is > than the search value | |
| if (node.Left != null) | |
| { | |
| foreach (var value in GetNodeValues(node.Left)) | |
| { | |
| yield return value; | |
| } | |
| } | |
| if (node.Right != null) | |
| { | |
| foreach (var value in GetNodeValues(node.Right)) | |
| { | |
| yield return value; | |
| } | |
| } | |
| } | |
| private class Node : IComparable<int> | |
| { | |
| public int Value { get; set; } | |
| public Node Left { get; set; } | |
| public Node Right { get; set; } | |
| public int CompareTo(int other) | |
| { | |
| return Value.CompareTo(other); | |
| } | |
| } | |
| } |
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
| interface IStore | |
| { | |
| IEnumerable<int> Get(int from); | |
| void Add(int value); | |
| } |
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
| class StoreTests | |
| { | |
| private Random random = new Random(0); | |
| [Test] | |
| public void TotalOf95() | |
| { | |
| const int start = 5; | |
| const int firstId = 1; | |
| const int lastId = 100; | |
| const int expected = 95; | |
| var store = CreateStore(); | |
| var range = CreateRange(firstId, lastId, false).ToList(); | |
| AddAll(store, range); | |
| var total = store.Get(start); | |
| Assert.AreEqual(expected, total.Count()); | |
| } | |
| [Test] | |
| public void TotalOf95WithGaps() | |
| { | |
| const int start = 5; | |
| const int firstId = 1; | |
| const int lastId = 100; | |
| var store = CreateStore(); | |
| var range = CreateRange(firstId, lastId, true).ToList(); | |
| int expected = range.Count(c => c > 5); | |
| AddAll(store, range); | |
| var total = store.Get(start); | |
| Assert.AreEqual(expected, total.Select(c => c > start).Count()); | |
| } | |
| private void AddAll(IStore bstStore, List<int> range) | |
| { | |
| foreach (var i in range) | |
| { | |
| bstStore.Add(i); | |
| } | |
| } | |
| private IStore CreateStore() | |
| { | |
| return new BstStore(); | |
| } | |
| private IEnumerable<int> CreateRange(int start, int end, bool introduceGaps = false) | |
| { | |
| for (int i = start; i <= end; i++) | |
| { | |
| if (introduceGaps && random.Next(1, 3) == 1) | |
| { | |
| continue; | |
| } | |
| yield return i; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment