Skip to content

Instantly share code, notes, and snippets.

@DavoBR
Last active November 29, 2023 03:08
Show Gist options
  • Save DavoBR/1339bf59b1fe67d8cef11d8959c0d413 to your computer and use it in GitHub Desktop.
Save DavoBR/1339bf59b1fe67d8cef11d8959c0d413 to your computer and use it in GitHub Desktop.
Developer Challenge - Word Finder in C#
string[] matrix = [
"a b c d c",
"i g w i o",
"c h i l l",
"p q n s d",
"u v d x y"
];
string[] wordstream = [
"cold",
"wind",
"snow",
"chill"
];
var wordFinder = new WordFinder(matrix);
var founds = wordFinder.Find(wordstream);
Console.WriteLine("Finding words...");
foreach (var word in founds)
{
Console.WriteLine($"- {word}");
}
Console.WriteLine("Done!");
public class WordFinder(IEnumerable<string> matrix)
{
private string[][] Matrix
{
get
{
var matrix2 = new string[matrix.Count()][];
for (int i = 0; i < matrix2.Length; i++)
{
matrix2[i] = [.. matrix.ElementAt(i).Split(" ")];
}
return matrix2;
}
}
public IEnumerable<string> Find(IEnumerable<string> wordstream)
{
return FindWords(wordstream).Distinct();
}
private IEnumerable<string> FindWords(IEnumerable<string> wordstream)
{
for (int x = 0; x < Matrix.Length; x++)
{
var hLetters = new List<string>();
var vLetters = new List<string>();
for (int y = 0; y < Matrix[x].Length; y++)
{
hLetters.Add(Matrix[x][y]);
vLetters.Add(Matrix[y][x]);
}
var hWord = string.Join(string.Empty, hLetters);
var vWord = string.Join(string.Empty, vLetters);
foreach (var word in wordstream)
{
if (hWord.Contains(word) || vWord.Contains(word))
{
yield return word;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment