Part 1
We are given a file with several lines of input. Each line consists of a number in a certain base and the base. We should extract only the bases and return the sum.
We start by parsing the input file into a list of tuples. For the first part, we sum over the bases and are done.
Part 2
In part 2, we should use the bases to convert each number into base 10 and sum the result.
We use the int function of python to convert strings into numbers. The second, optional argument of int can specify a base, so we supply the base. Summing up the results gets us the answer.
Part 3
In part 3, we should convert the previous sum into a base 65 string. In base 65, the first 10 digits are normal, followed by upper case and lower case letters. The last 3 digits are special chars !, # and @.
The easiest way to convert into a base is by building the string in reverse. We start by calculating the number modulo 65 and convert the result into a digit. We then divide the number by 65 and repeat to get the rest of the base-65 string. While building the string, we just have to remember to append the current digit to the left.
No comments:
Post a Comment