I'm just trying to solve various code katas, and have been stuck on the final piece of this problem for hours. The purpose is to return a result that doesn't have a repeating digit or character adjacent to it, while remaining in order. I've passed all the tests except one called ListTest, so I've tried to determine how to make a List argument return a value. Well, it always returns null thus far. I could use a push or hint in the right direction.
I can't remember everything I've tried, but the basic setup I have is an if statement checking for generic lists, and then running a separate code branch if that's the case. I'm not even sure that is necessary.
public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable)
{
string strArr = String.Empty;
string strLast = String.Empty;
List<dynamic> strList = new List<dynamic>();
var enumerator = iterable.GetEnumerator();
for (int i = 0; i < iterable.Count(); i++)
{
enumerator.MoveNext();
if (enumerator.Current.ToString() == strLast)
continue;
strArr += enumerator.Current.ToString();
strLast = enumerator.Current.ToString();
}
if (iterable is IList && iterable.GetType().IsGenericType)
{
foreach (var l in strArr)
{
strList.Add(l);
}
return strList as IEnumerable<T>;
}
return strArr as IEnumerable<T>;
}
static void Main()
{
Console.WriteLine(UniqueInOrder("AABBCDEE"));
Console.WriteLine(UniqueInOrder("112334557"));
Console.WriteLine(UniqueInOrder(new List<int> { 1, 2, 3 }));
}
This is what my original code looked like before I tried throwing the kitchen sink at it to fix it.
The answer was provided by Chetan in the comments, so this is now solved.