I need to write a function which gets two numbers in hexadecimal base, and calculates the sum of both of them, I'm not allowed to convert them to decimal base, the code is supposed to calculate it "manually" using loops. for example this is how it should work:
1 1 f 5 (A) + 5 a (B) ------------- = 2 4 f
Here is an input example:
>>> add("a5", "17") 'bc'
I've started building my code but I got stuck, I thought I would divide into three ifs, one that would sum up only numbers, other sums numbers and letters, and the third one sums letters, but I don't know how to continue from here:
def add_hex(A,B): lstA = [int(l) for l in str(A)] lstB = [int(l) for l in str(B)] if len(A)>len(B): A=B B=A A='0'*(len(B)-len(A))+A remainder=False result='' for i in range(len(B)-1) if (A[i]>0 and A[i]<10) and (B[i]>0 and B[i]<10): A[i]+B[i]=result if A[i]+B[i]>10: result+='1'
Any help is greatly appreciated, I have no clue how to start on this!</div