Skip to content
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
10 changes: 4 additions & 6 deletions CodingChallenge.FamilyTree.Tests/FamilyTreeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CodingChallenge.FamilyTree;
using System.Linq;
using FizzWare.NBuilder;

namespace CodingChallenge.FamilyTree.Tests
Expand All @@ -13,10 +10,11 @@ public static Person Make()
var hierarchySpec = Builder<HierarchySpec<Person>>.CreateNew()
.With(h => h.AddMethod, (p1, p2) => p1.Descendants.Add(p2))
.With(h => h.Depth = 7)
.With(h => h.MaximumChildren = 3)
.With(h => h.MinimumChildren = 2)
.With(h => h.MaximumChildren = 4)
.With(h => h.NumberOfRoots = 1).Build();

var person = Builder<Person>.CreateListOfSize(1100).BuildHierarchy(hierarchySpec).First();
var person = Builder<Person>.CreateListOfSize(5461).BuildHierarchy(hierarchySpec).First();

return person;
}
Expand Down
32 changes: 30 additions & 2 deletions CodingChallenge.FamilyTree/Solution.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace CodingChallenge.FamilyTree
{
public class Solution
{
//queue based BFS implementation for traversing a family tree to find a descendants birth month
//I chose BFS over DFS because it's a little more effecient at scale and it's implemenation is more readable in my opinion
//Most BFS implementations ensure graphs can be traversed by maintianing a "visited" structure
//I have omitted that here since this is a limited example and our use case probably shouldn't allow that
public string GetBirthMonth(Person person, string descendantName)
{
throw new NotImplementedException();
if(person == null
|| person.Descendants.Count == 0
|| string.IsNullOrEmpty(descendantName))
{
return string.Empty;
}

var treeTraversalQueue = new Queue<Person>();

treeTraversalQueue.Enqueue(person);

while(treeTraversalQueue.Any())
{
var currentFamilyMember = treeTraversalQueue.Dequeue();

if (currentFamilyMember.Name == descendantName)
{
return currentFamilyMember.Birthday.ToString("MMMM");
}

currentFamilyMember.Descendants.ForEach(x => treeTraversalQueue.Enqueue(x));
}

return string.Empty;
}
}
}
3 changes: 3 additions & 0 deletions CodingChallenge.PirateSpeak.Tests/PirateSpeakTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class PirateSpeakTests
[TestCase("ainstuomn", new[] { "mountains", "hills", "mesa" }, new[] { "mountains" })]
[TestCase("oopl", new[] { "donkey", "pool", "horse", "loop" }, new[] { "pool", "loop" })]
[TestCase("oprst", new[] {"sport", "ports", "ball", "bat", "port"}, new[] {"sport", "ports"})]
[TestCase("", new[] {"empty", "jumble", "test"}, new string[0])]
[TestCase("emptydictionarytest", new string[0], new string[0])]
[TestCase("nulldicitonarytest", null, new string[0])]
public void TestPirateVocabulary(string jumble, string[] dictionary, object expectedResult)
{
var actualResult = new Solution().GetPossibleWords(jumble, dictionary);
Expand Down
30 changes: 29 additions & 1 deletion CodingChallenge.PirateSpeak/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,35 @@ public class Solution
{
public string[] GetPossibleWords(string jumble, string[] dictionary)
{
throw new NotImplementedException();
if(string.IsNullOrEmpty(jumble)
|| dictionary == null
|| dictionary.Length == 0)
{
return new string[0];
}

var possibleMatches = dictionary.ToList();
var jumbleCharacterArray = StringToSortedCharacterArray(jumble);

for (int i = possibleMatches.Count - 1; i >= 0; i--)
{
var possibleMatchArray = StringToSortedCharacterArray(possibleMatches[i]);

if(!possibleMatchArray.SequenceEqual(jumbleCharacterArray))
{
possibleMatches.Remove(possibleMatches[i]);
}
}

return possibleMatches.ToArray();
}

private char[] StringToSortedCharacterArray(string inputString)
{
char[] retVal = inputString.ToLower().ToCharArray();
Array.Sort(retVal);

return retVal;
}
}
}