Tech Qu: Data structure to store and add arbitrarily large number
Data structure to store and add arbitrarily large number |
Hi folks, I decided to play around with some of the silliness known as technical questions. At one time obtaining a University Degree was sufficient proof – until grade standards started diving to 20,000 leagues under the sea. In general, there are many right answers.
a) Design a data structure to store an arbitrarily large number
b) How will you use it to store two such number and add them.
c) Write down the class (code) for the data structure
We assume integers and not negative.
If it is arbitrary real then use:
Code Snippet
- struct ArbitraryReal
- {
- public Stack<byte> LeftOf;
- public Stack<byte> RightOf;
- public bool Sign;
- }
This is an all in one function that could be decomposed..
- Take the string, break into characters, then convert to integer.
- Conversion was done with an old fashion C approach
- Add the integers (doing a carryover)
- push the result into a new stack
- convert the stack to a character string
- return the string
Code Snippet
- public static string Add(string a, string b)
- {
- // Assume both are positive and integers
- var aStack = new Stack<int>();
- var bStack = new Stack<int>();
- var cStack = new Stack<int>();
- char zero = '0';
- foreach (var a1 in a.ToCharArray())
- {
- int diff = a1 - zero;
- if (diff > -1 && diff < 10)
- {
- aStack.Push(diff);
- }
- else
- {
- throw new DataException("Not 0 - 9");
- }
- }
- foreach (char a1 in b.ToCharArray())
- {
- int diff = a1 - zero;
- if (diff > -1 && diff < 10)
- {
- bStack.Push(diff);
- }
- else
- {
- throw new DataException("Not 0 - 9");
- }
- }
- int carryover = 0;
- while (aStack.Count > 0 && bStack.Count > 0)
- {
- int sum = aStack.Pop() + bStack.Pop() + carryover;
- if (sum > 9)
- {
- carryover = 1;
- sum -= 10;
- }
- else
- carryover = 0;
- cStack.Push(sum);
- }
- while (aStack.Count > 0)
- {
- int sum = aStack.Pop() + carryover;
- if (sum > 9)
- {
- carryover = 1;
- sum -= 10;
- }
- else
- {
- carryover = 0;
- }
- cStack.Push(sum);
- }
- while (bStack.Count > 0)
- {
- int sum = bStack.Pop() + carryover;
- if (sum > 9)
- {
- carryover = 1;
- sum -= 10;
- }
- else
- {
- carryover = 0;
- }
- cStack.Push(sum);
- }
- if (carryover > 0)
- cStack.Push(carryover);
- var results = new StringBuilder();
- while (cStack.Count > 0)
- results.Append(cStack.Pop());
- return results.ToString();
- }
To test the code:
Code Snippet
- var rnd = new Random();
- for (int i = 0; i < 10000; i++)
- {
- var a = rnd.Next().ToString();
- var b = rnd.Next().ToString();
- var test = Questions.Add(a, b);
- Console.WriteLine(String.Format("{0}+{1}={2}",a,b,test));
- if (long.Parse(test) != long.Parse(a) + long.Parse(b))
- {
- throw new Exception("Unit Test failed");
- }
- }
Variations:
- You can check (and discard) comma(,) and other symbols ($) that have no numeric consequences.
- With the +/- real case, you can do multiple tests for negative (i.e. (1234), or 1234- or –1234) giving robustness.
- You can even work with complex (real and imaginaries)
Code Snippet
- struct ArbitraryComplex
- {
- public Stack<byte> LeftOf;
- public Stack<byte> RightOf;
- public bool Sign;
- public Stack<byte> iLeftOf;
- public Stack<byte> iRightOf;
- public bool iSign;
- }
Comments
Post a Comment