Test Qu: Division and Remainder

Given two numbers n and m, divide n by m without using the division operator. Return both the integer answer as well as the remainder.

This actually can get messy depending on your understanding of “Remainder”. One solution is shown below.

Code Snippet
  1. public static int DivideWithRemainder(int top, int bottom, out int count)
  2.     {
  3.       count = 0;
  4.       if (bottom == 0)
  5.       {
  6.         throw new DataException("cannot divide by zero");
  7.       }
  8.       bool sign = (top > 0 && bottom < 0)
  9.        || (top < 0 && bottom > 0);
  10.  
  11.       top = Math.Abs(top);
  12.       bottom = Math.Abs(bottom);
  13.  
  14.       while (top > bottom)
  15.       {
  16.         count++;
  17.         top -= bottom;
  18.       }
  19.       if (sign )
  20.       {
  21.         count = -count;
  22.       }
  23.       return top;
  24.     }

 

And for unit test:

  • Avoid the know boundary condition in the 10000 loop
  • Test explicitly for boundary condition afterwards.

 

Code Snippet
  1. var rnd = new Random();
  2. var multiple = 0;
  3. for (var i = 0; i < 10000; i++)
  4. {
  5.   var top = rnd.Next() - int.MaxValue / 2;
  6.   var bottom = rnd.Next() - int.MaxValue / 2;
  7.   if (bottom != 0)
  8.   {
  9.     var answer = Questions.DivideWithRemainder(top, bottom, out multiple);
  10.     // Remainder may not be negative, so we need to adjust modulus
  11.     // See http://en.wikipedia.org/wiki/Division_algorithm
  12.     if (Math.Abs(top % bottom) != answer)
  13.       throw new DataException("Unit test failed on remainder");
  14.     if (multiple != top / bottom)
  15.       throw new DataException("Unit test failed on result");
  16.   }
  17. }
  18. try
  19. {
  20.   var test = Questions.DivideWithRemainder(1, 0, out multiple);
  21.   Console.WriteLine("Failed Divide by zero test");
  22. }
  23. catch (Exception exc)
  24. {
  25.   Console.WriteLine("Passed Divide by zero test");
  26. }

Comments

Popular posts from this blog

Yet once more into the breech (of altered programming logic)

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

How to convert SVG data to a Png Image file Using InkScape