16
3
Write a program that visualizes long division with ASCII art. Input consists of two integers, a numerator and a denominator, using the input format of your choice.
Examples:
1234 ÷ 56:
22
----
56|1234
112
---
114
112
---
2
1002012 ÷ 12:
83501
-------
12|1002012
96
---
42
36
--
60
60
--
12
12
--
0 ÷ 35
0
-
35|0
Rules:
- Use of the programming language's division operator is allowed.
- Use of big integer support is also allowed.
- For consistency:
- If the quotient is zero, print a single zero at the end of the diving board.
- If the remainder is zero, do not print it.
- Do not print leading zeros on any numbers.
- Excess newlines at the end and trailing spaces to the right are allowed.
- Solution with the fewest characters wins.
Limits:
- 0 <= numerator <= 1072 - 1
- 1 <= denominator <= 9999999
This implies that the output will never be wider than 80 columns.
Test suite and sample implementation:
You can use long-division.c (gist) to test your program. It is actually a bash script with a C program inside. Tweak it to invoke your program in the test suite. Look at the C code at the bottom to see the reference implementation. Please let me know if there are any problems with the sample program or test suite.
$ ./long-division.c 10 7
1
--
7|10
7
--
3
$ ./long-division.c
PASS 1234 ÷ 56
PASS 1002012 ÷ 12
PASS 1 ÷ 1
--- snip ---
Score: 35 / 35
All tests passed!
Edit: By request, I put the test suite input and expected output into text files (gist). Sample usage (bash):
cat input | while read nd; do
./program $nd |
sed 's/\s*$//' | sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
done > output
diff -u expected output
The weird sed commands filter out trailing newlines and spaces from the program output.
I discovered a minor flaw in the reference implementation, namely in the case of 123000123000123 ÷ 123. The subtraction lines were spanning blank space, but they should only be spanning the length of the visible digits of the minuend. It's fixed now. – Joey Adams – 2011-03-15T01:22:22.833
2
I think the output is a bit biased towards the English-speaking golfing audience: http://en.wikipedia.org/wiki/Long_division#Notation_in_non-English-speaking_parts_of_the_world
– hallvabo – 2011-03-15T11:23:28.097Can you just create a file that shows the expected output of all the tests and link to it? – mellamokb – 2011-03-15T14:43:38.830
@mellamokb: Added, thanks! – Joey Adams – 2011-03-15T15:28:18.367
What about accepting? The question is old enough... – Oleh Prypin – 2011-04-06T16:57:27.093