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.