Reverse The Gear

Something triggered this evening and prompted me to write up this post .. and the first thing it came to my mind, what are two difficult things the Driving License folks ask before handing you over for the G/G2 license .. and they are

a. Reverse the Car (of course, the car has to be backed up properly between the two yellow lines)
b. Parallel Parking -- Park this topic for now, some day we will talk about Parallel Programming

Alright, so why the heck I am even talking about it.. well, like I mentioned earlier, something prompted me to ponder over writing up a Reverse routine that must not use a temporary array.. well, sure we can use a temporary variable to (temporarily) store a node value -- and then some one (from my kind of background) may say, well, I don't want to use any temporary variable, whatsoever .. 

The following are the two quick code snippets that I believe should work for now :-)

Snippet 1: Reverse Method (NOT using temporary Array)

Note: This method does use a temporary variable to hold a node value.































Snippet 2: Reverse Method (NOT using any temporary Variable) .. well, so here comes our 
*old*  friend, Recursion



All in all, I'm not sure why would we even need to sweat writing up our own implementation when Microsoft has already invested into a *ready-made*, well-tested API

        static void Main(string[] args)
        {
            int[] vanillaArray = { 11, 2, 6, 8, 56, 3, 21 };

/*
                       (reverses entire 1D array)
                       (reverses range of elements in 1D array)
*/

            Array.Reverse(vanillaArray);

            foreach (int value in vanillaArray)
            {
                Console.WriteLine(value);
            }

            //press any key, stuff..
            Console.ReadLine();
        }

Ok - so moving onto the Unit Test part, there are several things one may need to consider when unit testing your own implementation:

a. Data Type
b. Empty array
c. Odd valued array elements v/s Even valued array elements (to check the /2 rule, sort of)
d. Size, et al.

Basically, leverage Assert methods and you should be good :-)


From the perspective of optimization of code, I have a KISS philosophy, 
Do Not Re-Invent (look for what .Net offers, leverage it, try avoiding your custom implementations unless it is absolutely necessary, rather go for extensions ...)

Comments

Popular posts from this blog

QuadKey - what it is?

Baseless merge!

Binding ENUM to WPF control - ComboBox