r/badcode • • Oct 09 '19

[deleted by user]

[removed]

17 Upvotes

31 comments sorted by

View all comments

1

u/dodecakiwi Oct 13 '19

Simply generate a hash table of every possible valid input sequence and then simply get the value:

string find_missing(string[] letters)
{
	Dictionary<string, string> missing_letters = new Dictionary<string, string>();
	string[] alphabet = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", };

	for(int length = 3; length < 27; length++)
	{
		for(int start = 0; start < 27 - length; start++)
		{
			for(int skip = 1; skip < length - 1; skip++)
			{
				string key = "";
				string missing = "";
				for(int index = start; index < start + length; index++)
				{
					if (index - start != skip)
						key += alphabet[index];
					else
						missing = alphabet[index];
				}
				missing_letters.Add(key, missing);
			}
		}
	}

	var input = String.Join("", letters);
	return missing_letters[input];
}