Coding Challenge - Lesson Learned

Introduction

Last month, I joined a coding challenge for fun at codeproject.com. The theme for the challenge was “convert integer to a sentence”. For example: 12345 or 12,345 would result in twelve thousand, three hundred and forty five. Of course, I didn’t win, there were way too many smart contestants out there. Anyway, looking back, I think there are several points I can share from the code that I wrote. Here is the link to the code - http://rextester.com/FTS88296

NumberStyles

The code was structured to accept both numeric string with and without number styles. At one point, I was getting the error “Message: "Input string was not in a correct format."” when parsing the input with comma and negative sign into integers. Then, I realized that I need to include NumberStyles in the integer parse method. Basically, the integer parser needs to know that the numeric string can contain certain symbols. To be honest, I forgot the syntax!!! Luckily, I found what I was looking for on the MSDN site to help refresh my memory. Here is the link to NumberStyles Enumeration. The resource lists all the NumberStyles members and provides example on how to include multiple NumberStyles into the int.parse method. I figured if I write this down, it will come in handy once in a while by searching my blog.

Listing 1 shows how to parse a numeric string into 64-bit signed and unassigned integers respectively.

Listing 1

  1. if (Int64.TryParse(input, NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign, provider, out userInput))  
  2.   
  3. {  
  4.   
  5. Console.WriteLine(IntegerToSentence(userInput));  
  6.   
  7. }  
  8.   
  9. else if (UInt64.TryParse(input, NumberStyles.AllowThousands, provider, out userInputUnsigned))  
  10.   
  11. {  
  12.   
  13. Console.WriteLine(IntegerToSentence(userInputUnsigned));  
  14.   

Tuple

I also used 2-tuple or pair in the code. Here is the link to learn more about Tuple Class. Listing 2 shows how to declare a method with return 2-tuple type and how to create 2-tuple object. In this example, the first object holds a Boolean value indicating if the integers are positive or negative. The second object holds a list of integers converted to string.

Listing 2

  1. public static Tuple<bool, IList<string>> GroupInteger<T>(this T input, int groupLength = 3)  
  2. {  
  3. …  
  4. Tuple<bool, IList<string>> listNumbers = new Tuple<bool, IList<string>>(isNegative, groupNumbers);  
  5. …  

Listing 3 shows how to access the 2-tuple elements.

Listing 3

  1. //minus sign  
  2. if (listInteger.Item1)  
  3. {  
  4. output += "Minus ";  
  5. }  
  6.   
  7. //number of group  
  8. j = listInteger.Item2.Count - 1; 

In my opinion, the nice feature about tuple class is that it acts like a container to hold any objects. And every time I think of or come across 6-tuple, it makes me chuckle a little. I always think why can’t we call it sixtuple or hextuple instead of sextuple.

Generic Extension Method

The code also utilized a generic extension method that will take 64-bit signed and unassigned integers as a parameter.

Listing 4 shows how to create a generic extension method with multiple parameters. The first parameter with "this" keyword as a required parameter for extension method. I added the second parameter and set it optional.

Listing 4

  1. public static class Utility  
  2. {  
  3.    public static Tuple<bool, IList<string>> GroupInteger<T>(this T input, int groupLength = 3)  
  4.   { … }  

Listing 5 shows how to invoke the extension method.

Listing 5

  1. static string IntegerToSentence<T>(T input, bool unsigned = false)  
  2. {  
  3.   var listInteger = input.GroupInteger();  
  4.   var subGroup = Convert.ToInt64(listInteger.Item2[j]).GroupInteger(1);  

Group string from right to left

In this section, my goal was to split the given integers into 3 integers per group. Let's say, a user enters 98745612. My expected results are 98, 745 and 612. Usually, we write the code to extract substring from a string, from left to right. The result will end up like 987, 456 and 12. I did some Googling but couldn’t find the code that I was looking for to copy and paste, maybe I didn’t use the correct keyword. So, I ended up writing my own and seriously, it's not that complicated.

  1. int inputLength = userInput.Length;  
  2. //read from right to left  
  3.             //group 3 digit from right to left  
  4.             while (inputLength > 0)  
  5.             {  
  6.                 inputLength = inputLength - groupLength;  
  7.   
  8.                 if (inputLength < 0)  
  9.                 {  
  10.                     startIndex = 0;  
  11.                     length = groupLength + inputLength;  
  12.                 }  
  13.                 else  
  14.                 {  
  15.                     startIndex = inputLength;  
  16.                     length = groupLength;  
  17.                 }  
  18.   
  19.                 inputGroup = userInput.Substring(startIndex, length);  
  20.   
  21.                 groupNumbers.Add(inputGroup);  
  22.             } 

Point of interest

You will be amazed to know that a problem can be solved by using various different programming languages. Some contestants used Python, Perl, C, C#, etc., each having his/her own way to approach the problem.

Let's face it, some of us learned lots of programming skills during our career but do we apply some or all of it to our daily tasks? Or do you remember the syntax when the time comes? I guess not. So, take some time to join one of the challenges when you have time. This is a good opportunity to refresh your memory and learn new programming skills from another contestant/s.

Conclusion

I hope someone will find this information useful and make their programming job easier. If you find any bugs, or disagree with the contents, or want to help improve this article, please drop me a line and I'll work with you to correct it. As always, anonymous/irresponsible down-voting an article without providing a justification will help neither me nor the community.

Up Next
    Ebook Download
    View all
    Learn
    View all