This isn't as much a "challenge" as "a unique problem that there are no built-in functions to solve." Basically, you need to do this:
Read some lines of numbers from
stdin
. These lines may contain trailing or preceding whitespace, and there may be empty lines. No lines will contain "just whitespace" other than a newline. Each non-empty line will have exactly one number. This:123 456\t
(where
\t
is a tab) is considered valid input.Read the numbers in the input in base
N+1
, whereN
is the largest digit in any of the lines. In this case, the largest digit is6
, so the numbers would be read in base7
, resulting in 6610 and 23710.Find the sum of the numbers. The sum of 6610 and 23710 is 30310.
Convert the sum back to base
N+1
again. We'd convert 30310 to base7
, which would be 6127.For each digit, print that many underscores, followed by a space. The last digit should instead be followed by a newline or a space and a newline. In our example, that would be
______ _ __\n
or______ _ __ \n
, where\n
is the newline character.
Here's an example in Python:
import sys
def convert_to_base(n, b):
'Convert `n` to base `b`'
res = []
while n:
res.append(n % b)
n /= b
return res[::-1]
# read the lines, discarding empty ones and stripping any whitespace
lines = map(str.strip, filter(None, list(sys.stdin)))
# find the largest digit, and add 1 to it to make our base
base = max(map(max, (map(int, line) for line in lines)))+1
# read the lines in the base and sum them
result = sum(int(line, base) for line in lines)
# print the result in the original base using underscores, separated by spaces
for number in convert_to_base(result, base):
print '_'*number,
# print a newline
print
This is code golf: the shortest program wins!