Subtracting two numbers of any base between 2 and 10

Question:

For two numbers x and y that are base b, does this work for subtracting them? The numbers are given in string format and 2 <= b <= 10.

def n2b(n, b):         # function to convert number n from base 10 to base b
    if n == 0:
        return 0
    d = []
    while n:
        d.append(int(n % b))
        n /= b
    return ''.join(map(str,d[::-1]))

x = int(x,b) # convert to integers in base 10 
y = int(y,b)
z = x - y
z = n2b(z,b) # convert back to base b, still in integer form
Asked By: kfish15

||

Answers:

You have some confusion about how integers work in python. As the comments above say: python always stores integers in binary form and only converts them to a base when you print them. Depending on how you get x and y and how you need to give back z the code needs to be different

Situation 1: x, y and z are all integers

In this case you only need to do

z = x - y

And you’re done.

Situation 2: x, y and z are all strings

In this case you first need to convert the strings into integers with the right base. I think that’s your case, since you already deal with int(x, b) which is correct to transform a string into an integer (e.g. int("11", 2) gives 3 (integer represented in base 10). I would advice you to reform your code into something like this:

x_int = int(x, b)
y_int = int(y, b)
z_str = n2b(x_int - y_int, b)

In your code x is first a string and then an integer, which is bad practice. So e.g. use x_int instead of x.

Now it comes down to if your n2b function is correct. It looks good from the distance, although you’re not handling signs and bases bigger than 10. There is a broadly accepted convert integer to base b answer so you might take this to be sure.

Answered By: hansaplast

This is exactly the problem I just ran into in the google foobar challenge (which I suspect is the same source of ops problem). Granted its years later and op has no use for my answer but someone else might.

The issue

The function op used looked a lot like a copy and paste of this provided by the accepted answer but slightly modified (although I didn’t look to closely).
I used the same function and quickly realized that I needed my output to be a string. Op appears to have realized the same thing based on the return statement at the end of the function.
This is why most of the test cases passed. Op did almost everything right.
See, the function begins with

if n==0:
    return 0

One of the test cases in the foobar challenge uses 0 as the input. Its an easy line to miss but a very important one.

Solution

When I was presented this problem, I thought about the possible outlier cases. 0 was my first idea (which turned out to be right). I ran the program in vscode and would ya look at that – it failed.
This let me see the error message (in my case it was a list rather than int but op would have received a similar error).
The solution is simply changing return 0 to return '0' (a string rather than int)

I wasn’t super excited to write out this answer since it feels a bit like cheating but its such a small detail that can be so infuriating to deal with. It doesn’t solve the challenge that foobar gives you, just tells you how to get past a speed bump along the way.

Good luck infiltrating commander lambda’s castle and hopefully this helps.

Answered By: Ryan Richter
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.