#Please help (python)

3 messages · Page 1 of 1 (latest)

azure nova
#

I am struggling with the All Your Base (https://exercism.org/tracks/python/exercises/all-your-base) exercise. So far I have managed to convert the given digits into a base-10 number, but am unsure of how to convert this into the desired base. My code is below:

def rebase(input_base, digits, output_base):
    if input_base < 2:
        raise ValueError("input base must be >= 2")
    if output_base < 2:
        raise ValueError("output base must be >= 2")
    if not all(0 <= d < input_base for d in digits):
            raise ValueError("all digits must satisfy 0 <= d < input base")

    valueOfDigits = 0
    for d in digits:
        valueOfDigits += d * input_base ** (len(digits) - d)

Exercism

Can you solve All Your Base in Python? Improve your Python skills with support from our world-class team of mentors.

frail verge
#

The process to convert a number (in decimal) to any other base is quite similar. If you can figure out how to do it for one, you can do it for the rest.
Let pick a familiar base : base 2, and figure out how you can convert a number, let say 10, to binary (base 2)

#

Afterward you can apply the same technique for all bases