Code Kata 2 Day 3 A LINQ to the Past

So here we are at Code Kata 2 day 3. Honestly, it is fun but I kind of just want to sit down and do them all. Anyway, Zelda jokes aside, it is time to dig into a new implementation of the kata. This time I am going to use LINQ to access criteria that I want. This is an ugly solution. Off the top of my head, I could not remember a way of revealing the index of the data I was selecting. It led to some interesting solutions. Anyways, lets do it.

It’s killing me, lets get to the implementation already of Code Kata 2 Day 3.

So I decided to use LINQ. It worked really good for finding the value. However, it did not do too well when it came time to find the index. I could not figure out a ready way of grabbing that index so I solved the problem using a dictionary.

All this took about 15 minutes to code so I did not dump a huge investment in terms of optimizing it. If I was writing this for a customer, I would have spent up to an hour or two researching the most elegant way to return the index in LINQ. It would be shocking to me if it was not possible. Lets take a look at the code.

Day3Chop.cs

using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeKata2
{
   public class Day3Chop
   {
      public int chop(int numberToSeek, List intList)
      {
         Dictionary dir = new Dictionary();
         int counter = 0;
         if (intList.Count == 0) 
         { 
            return -1; 
         } 
 
         foreach(int i in intList) 
         { 
            dir.Add(counter, i); 
            counter++; 
          } 

          if(dir.Where(i => i.Value == numberToSeek).Select(i => i.Key).Count() == 0) 
          { 
             return -1; 
          } 
 
          return dir.Where(i => i.Value == numberToSeek).Select(i => i.Key).First(); 
      } 
   }
}

Now, I am ok with the length of the function. I do feel like it still a tight function. I would love to streamline it more by begin able to dump the dictionary logic.

Speaking of dictionary logic that is what get started with in the function. Both variables are tied to building the dictionary. The first conditional is the standard, make sure the list is present check. After that we get to the loop that will build the dictionary. The counter gives the dir the appropriate index. I am happy with myself for actually calling it a counter this time.

With the empty array check and dictionary built, we move onto the LINQ. I then check the directory for no value found. If it does not then it returns -1. Otherwise we go to the last line a return of the dictionary key where the value was found.

Looking at the syntax of my LINQ, I think it is pretty clear. I have my dictionary and in it I search the value part of the dictionary for the number we are seeking. Then I select the key value, which is the index value we assigned in the creation step, then return the first row of the dataset.

Now, the constraints have no duplicating number so there is no check for that. I do like how easy it would be to put those checks into this particular code. We can check for over 1 count and throw an exception. We could change the return type and have it deal with a collection rather than a single value.

Ok, we get how in love you are with your code, what do you hate.

For the record, I am not in love with the code. I just appropriate that it could cleanly expand its checks. Actually, I dislike the the dictionary part. In refactoring this code, that would be my target. Can I get the index in a cleaner way using LINQ or do I really have to engineer the list for that becomes possible. If you think of a way to LINQ an index, the send it down in the comments. I would love to learn how to do it.

Next time we will look at the forth implementation of Code Kata 2. Who knew this would be worth half a dozen posts from one kata.

Leave a Reply

Your email address will not be published.