Bad Code Coding Challenge #21 - Find the missing letter

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

namespace BadCodeChallenges

{

class Program

{

static void Main(string[] args)

{

MissingLetterFinder.FindMissingLetter("abcdefghijklmnopqrstuvwyz");

}

}

static class MissingLetterFinder

{

public static char FindMissingLetter(string letters)

{

AllWrongCombinatinos wrongCombos = new AllWrongCombinatinos(letters.Length);

// We will compare our ordered set of letters to any possible wrong

// set of letters with one missing letter between them

foreach(var wrongCombo in wrongCombos)

{

if(letters == wrongCombo)

{

Console.WriteLine($"{wrongCombos.MissingLetter} is missing! from {letters}");

Console.ReadKey();

return wrongCombos.MissingLetter;

}

}

return ' ';

}

}

class AllWrongCombinatinos : IEnumerable<string>

{

private string things = "abcdefghijklmnopqrstuvwxyz";

private readonly int l;

public AllWrongCombinatinos(int l = 26)

{

this.l = l;

}

IEnumerator IEnumerable.GetEnumerator()

{

return this.GetEnumerator();

}

public IEnumerator<string> GetEnumerator()

{

while (true)

{

// Create a random wrong combination and yield return it

for(int i = 0; i < 26; i++)

{

int x = i;

for(int j = i + 1; j <26; j++)

{

int y = j;

if (x > y)

{

var t = x;

x = y;

y = t;

}

if (y - x + 1 > l + 1) continue; // the plus one is because we cut a letter

for (int k = i + 1; k < j; k++)

{

var c = things.ElementAt(k);

var w = things.Substring(x, y + 1 - x).Replace(things.ElementAt(k).ToString(), string.Empty);

MissingLetter = c;

yield return w;

}

}

}

}

}

public char MissingLetter { get; set; }

}

}

That's one way to solve it

/r/badcode Thread