0

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.

enter image description here

The answer was provided by Chetan in the comments, so this is now solved.

Starnes Student
  • 103
  • 1
  • 8
  • 1
    Which call to `UniqueInOrder` method returns null? Which line of code gives you null? – Chetan Jan 20 '20 at 03:01
  • The last one with new List {1, 2, 3}, and everything looks fine with it until it actually returns. – Starnes Student Jan 20 '20 at 03:19
  • 1
    `strList` is not `IEnumerable`... it is `IEnumerbale`... Why do you expect cast to succeed? (Side note: it may be a good idea to explain what is expected from this code - link, or better yet inline the assignment you trying to solve) – Alexei Levenkov Jan 20 '20 at 03:34
  • 1
    `return strList as IEnumerable;` is the issue... `strList` is `List` and `IEnumerable` will be `IEnumerable` in the last method call... so that conversion does not happen and it returns null. – Chetan Jan 20 '20 at 03:35
  • I see what you are both saying, but what would I use to return it as a valid value? I was previously not using any list syntax in the UniqueInOrder method, and it still passed out null using the return strArr as IEnumerable. Here is a link to the problem: https://www.codewars.com/kata/54e6533c92449cc251001667/train/csharp – Starnes Student Jan 20 '20 at 03:50
  • 1
    There are a lot of red flags in this code. I strongly doubt you need a `List` and I doubt you need to use Reflection to solve this problem. – John Wu Jan 20 '20 at 04:01
  • 1
    What I believe is [duplicate](https://stackoverflow.com/questions/5729572/eliminate-consecutive-duplicates-of-list-elements) shows correct solution and hopefully self-explanatory. If not at least it should give you some ideas to clarify what exactly you don't understand... – Alexei Levenkov Jan 20 '20 at 04:01
  • The problem isn't with me being able to deduce the solution to the consecutive elements part, I've already passed 5 of 6 tests. It's only when the argument itself is a list. It has to be returned as an IEnumerable, that is what I do not know how to do without it becoming nullified. – Starnes Student Jan 20 '20 at 04:09
  • If this is a duplicate, then it would mean the duplicate had the answer, which it doesn't not provide. – Starnes Student Jan 20 '20 at 04:11
  • Guess I'll need to re-word this question since nobody has a clue what I'm talking about. – Starnes Student Jan 20 '20 at 04:15
  • @JohnWu's comment's value cannot be overstated. The code you posted is wrong for pretty much every kind of input except a `string` value (which happens to be `IEnumerable`). Your specification is also suspect: lists of integers cannot have adjacent "digits" or "characters"; each element is a whole number unto itself. The 2nd-highest-voted answer in the marked duplicate is a good general solution to what would be a corrected version of your stated specification, so you should look more closely at that. That your method returns `null` is just a side-effect of everything else wrong with it. – Peter Duniho Jan 20 '20 at 04:18
  • _"nobody has a clue what I'm talking about"_ -- Nobody? I don't see anyone that doesn't know what you're talking about, but even if _someone_ doesn't, "nobody" is certainly too broad. Bottom line: you're asking about the `null` return value, which is clearly just because your code is unredeemably broken (you're trying to cast a reference to a type that it's not, using `as`, so of course you get `null`). You need a completely different approach, which the marked duplicate illustrates quite well. – Peter Duniho Jan 20 '20 at 04:20
  • 1
    https://dotnetfiddle.net/zo6HCm here is a simpler implementation of your method. – Chetan Jan 20 '20 at 04:24
  • To solve the issue you are facing @StarnesStudent you need difference approach to solve the problem. With the current version of your method `null` object error can not be resolved for sure....Even if the duplicate answer is not acceptable by you the current approach still need to change. – Chetan Jan 20 '20 at 04:26
  • I get I'm coming off a little hot-headed, so I apologize for that. I have implemented the duplicate's suggestion, and an in the process of rewriting this so it is clearer what the final problem is. – Starnes Student Jan 20 '20 at 04:30
  • Nm, Chetan has the answer. Thank you Chetan. – Starnes Student Jan 20 '20 at 04:32
  • @Chetan's code is not different in any material way from the code in the marked duplicate. So your question is in fact a duplicate of that one. – Peter Duniho Jan 20 '20 at 04:39
  • Peter, my problem was not removing the adjacent duplicates, it was determining how to return the value as an IEnumerable. Chetan's code accounted for that. While I am not doubting you that the other code works, it requires me to solve the problem I was asking to understand how to re-work it into the context of the solution. When I plugged the other solution into my code, it presented with errors on several fronts that I did not know how to fix. I changed "array" to "iterable", but the results.Last() != element was giving me an issue because it says != can't be applied to string and T. – Starnes Student Jan 20 '20 at 04:57

0 Answers0