Add numbers in hexadecimal base without converting bases? - python

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!

You can have a sub-function that adds two single-digit hex numbers and returns their single-digit sum and a carry (either 0 or 1). This function will take three inputs: two numbers you want to add and a carry-in. You can then loop through the digits of the two numbers you want to add from least significant to most significant, and apply this function for every pair of digits while taking into account the carry at each stage.
So let's try your example:
A 5
1 7 +
We start at the least significant digits, 5 and 7, and perform the 1-digit addition. 516 + 716 = 1210. 1210 is less than 1610, so the output of our 1-digit add is 1210 = C16 with a carry of 0.
Now we add A and 1 (our carry-in is 0 so we can just add them normally). A16 + 116 = 1110. 1110 is less than 1610, so the output of our 1-digit add is 1110 = B16 with a carry of 0. (If we had a non-zero carry-in, we would just add 1 to this value.)
Hence, our overall result is:
A 5
1 7 +
-----
B C

I think we just remember the pattern of addition. Like following.
"0" + "0" = "0"
"0" + "1" = "1"
"0" + "2" = "2"
.
.
.
"f" + "d" = "1b"
"f" + "e" = "1c"
"f" + "f" = "1e"
We have dictionary of all of the pattern because we've learned it in school or somewhere. And we've also learned carry.
So I think this seems like manual addition algorithm.
Remembering the pattern include carry.
Calculating
Translate two digit to one digit(a+b->c).
Treat carry correctly.
And here is my code for that. But it may be a bit tricky.
import itertools
def add_hex(A,B):
A = "0"+A
B = "0"+B
#Remember all pattern include carry in variable d.
i2h = dict(zip(range(16), "0123456789abcdef"))
a = [(i,j) for i in "0123456789abcdef" for j in "0123456789abcdef"]
b = list(map(lambda t: int(t[0],16)+int(t[1],16), a))
c = ["0"+i2h[i] if i<16 else "1"+i2h[i-16] for i in b]#list of digit include carry
d = dict(zip(a,c))#d={(digit,digit):digit,,,}
#Calculate with variable d.
result = ""
cur = "0"
nex = "0"
for i in itertools.izip_longest(A[::-1], B[::-1], fillvalue = "0"):
cur = d[(nex, d[i][1])][1] #cur = carry + digit + digit
if d[i][0]=='1' or d[(nex, d[i][1])][0]=='1':#nex = carry = carry + digit + digit
nex = "1"
else:
nex = "0"
result += cur
return result[::-1]
#Test
A = "fedcba"
B = "012346"
print add_hex(A,B)
print hex(int(A,16)+int(B,16))#For validation
I hope it helps. :)

Related

My Function To Count The Largest Binary Gap Doesn't Work But I Can't Figure Out Why

I'm working through the Codility problems and I have gotten the first one almost correct. The task is to write a function which returns the longest binary gap (a sequence of 0s in between 1s) in a binary number. I have gotten all of the test numbers correct apart from 9, which should be 2 (its binary representation is 1001) but which my function returns as 0. I can't seem to figure out why.
My function is as follows:
def Solution(N):
x = bin(N)[2:]
x_string = str(x)
y = (len(x_string))
count = 0
max = 0
for index, item in enumerate(x_string):
if item == "1":
count = 0
elif item == "0" and x_string[index + 1:y-1] != "0"*(y -1 - (index + 1)):
count = count + 1
if count > max:
max = count
print(max)
The complicated indexing and second condition in the elif statement is so that when a 0 is not contained between two 1s then it isn't recognised as the beginning of a binary gap e.g. when the for loop looks at the second character in bin(16) = 10000, it doesn't set count to 1 because all of the remaining characters in the string are also zero.
Simple solution
x_string[index + 1:y-1] != "0"
this bit wants to take a look at the whole string that's left, but the end argument isn't inclusive,it's exluded, so if string length = 4; string[0:4] is the whole string.
source: https://docs.python.org/3/tutorial/introduction.html
-Sam

Extract the n-th digit of a number in Sagemath

How is it possible to extract the n-th digit of a number in Sagemath? We have to compute the 13787th digit from pi + e in Sagemath. My approach was the following:
sage: xi = e + pi
....: var1 = xi.n(digits=13786+1)
....: var2 = xi.n(digits=13787+1)
....: var3 = ((var2-var1) * 10^13787).trunc()
....: var3
0
Which gave me 0 back, however it should be 9.
The digit is indeed 9. But the subsequent digits are also 9: this part of decimal expansion goes ...9999237... Rounding rolls these 9s to 0s, carrying 1 to a higher digit.
So you need some extra digits in order to avoid the digit you are interested in from being affected by rounding. How many depends on the number; we don't know in advance if there isn't a sequence of one billion of 9s starting from that position. Here I use 10 extra digits
xi = e + pi
n = 13787
offset = 1 + floor(log(xi, 10)) # the first significant figure is not always the first digit after decimal dot, so we account for that
extra = 10
digit = int(str(xi.n(digits = n + offset + extra))[-1 - extra])
This returns 9. I think extracting with str is more reliable than subtracting two nearly-equal numbers and hoping there won't be additional loss of precition there.
Of course, including a magic number like 10 isn't reliable. Here is a better version, which starts with 10 extra digits but then increases the number until we no longer have 00000000... as a result.
xi = e + pi
n = 13787
offset = 1 + floor(log(xi, 10))
extra = 10
while True:
digits = str(xi.n(digits = n + offset + extra))[-1 - extra:]
if digits == "0" * len(digits):
extra *= 2
else:
digit = int(digits[0])
break
print(digit)
This will loop forever if the digits keep coming as 0, and rightly so: without actually knowing what the number is we can never be sure if the ...0000000... we get is not really ...999999999942... rounded up.

I have an int 123. How to produce a string "100+20+3" using python?

I have a int 123. I need to convert it to a string "100 + 20 + 3"
How can I achieve it using Python?
I am trying to divide the number first (with 100) and then multiple the quotient again with 100. This seems to be pretty inefficient. Is there another way which I can use?
a = 123
quot = 123//100
a1 = quot*100
I am repeating the above process for all the digits.
Another option would be to do it by the index of the digit:
def int_str(i):
digits = len(str(i))
result = []
for digit in range(digits):
result.append(str(i)[digit] + '0' * (digits - digit - 1))
print ' + '.join(result)
which gives:
>>> int_str(123)
100 + 20 + 3
This works by taking each digit and adding a number of zeroes equal to how many digits are after the current digit. (at index 0, and a length of 3, you have 3 - 0 - 1 remaining digits, so the first digit should have 2 zeroes after it.)
When the loop is done, I have a list ["100", "20", "3"] which I then use join to add the connecting " + "s.
(Ab)using list comprehension:
>>> num = 123
>>> ' + '.join([x + '0' * (len(str(num)) - i - 1) for i, x in enumerate(str(num))])
'100 + 20 + 3'
How it works:
iteration 0
Digit at index 0: '1'
+ ('0' * (num_digits - 1 - iter_count) = 2) = '100'
iteration 1
Digit at index 1: '2'
+ ('0' * 1) = '20'
iteration 2
Digit at index 2: '3'
+
('0' * 0) = '3'
Once you've created all the "numbers" and put them in the list, call join and combine them with the string predicate +.
Another way of achieving what you intended to do:
def pretty_print(a):
aa = str(a)
base = len(aa) - 1
for v in aa:
yield v + '0' * base
base -= 1
>>> ' + '.join(pretty_print(123))
'100 + 20 + 3'
Here's my approach:
numInput= 123
strNums= str(numInput)
numberList= []
for i in range(0,len(strNums)):
digit= (10**i)*int(strNums[-(i+1)])
numberList.append(str(digit))
final= "+".join(numberList)
print(final)
It's the mathematical approach for what you want.
In number system every digit can be denoted as the 10 to the power of the actual place plus number(counting from zero from right to left)
So we took a number and converted into a string. Then in a loop we decided the range of the iteration which is equal to the length of our number.
range: 0 to length of number
and we give that number of power to the 10, so we would get:
10^0, 10^1, 10^2...
Now we need this value to multiply with the digits right to left. So we used negative index. Then we appended the string value of the digit to an empty list because we need the result in a form as you said.
Hope it will be helpful to you.

Lucky Name Number Challenge In Python

i have to create a lucky name number programme in python but i keep coming across many errors.
if you don't know what a lucky name number is, it is basically where each letter of the alphabet has a value and you add the values toether in your first name and second name so for example
john doe
165 465
1+6+5 = 12
4+6+5 = 15
15 + 12 = 27
2+7 = 8
then 8 = has diplomatic skills
Here is what i have done so far:
#this will go all the way to z
charDict = { 'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4}
# example names - while loop will go here
firstName = 'AAB'
lastName = 'DCDD'
# split the strings into a list of chars
firstNameChars = list(firstName)
lastNameChars = list(lastName)
# sum up values
firstNameSum = 0
lastNameSum = 0
for chr in firstNameChars:
firstNameSum += charDict[chr]
for chr in lastNameChars:
lastNameSum += charDict[chr]
# cast sums to strings. In this example, this would be '2024'
combinedNames = str(firstNameSum) + str(lastNameSum)
# split the string into a list of chars
combinedNameDigits = list(combinedNames)
# sum them up
finalSum = 0
for dgt in combinedNames:
finalSum += int(dgt)
# print the lucky number
print finalSum
So my question is, is where do i go from here, as the numbers don't add up correctly and the values of the letters aren't correct, so basically how do i do the calculations correctly
I really don't undersand how: john doe gives 165 465 and how: AAB DCDD gives 2024.
However, the standard way to convert letters in numbers and to sum the digits of a number is the following:
def letters_to_numbers(name):
sum_ = 0
for letter in name:
sum_ += ord(letter.upper())-64 #ord("A")=65 minus 64 -> 1
return sum_
def sum_digits(number):
sum_ = 0
while number:
sum_ += number%10
number //=10
return sum_
sum_digits(
sum_digits(letters_to_numbers("john"))
+sum_digits(letters_to_numbers("doe")))
This works considering you always split your numbers to the digit level, I let you wrap it in a function and modify the dictionary to add other letters
first = 'aaa'
last = 'bbb'
name = first + last
dicti = {'a':1, 'b':2, '1':1, '2':2 , '3':3 , '4':4 , '5':5 , '6':6 ,
'7':7 , '8':8 , '9':9 , '0':0}
while len(name) >1:
sum = 0
for letter in name:
sum = sum + dicti[letter]
name = str(sum)
final_sum = int(name)

Kaprekar numbers in Python

I aas solving a problem on HackerRank and the problem is as follows:
A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).
Alternatively, a modified Kaprekar number is a positive whole number n with 2d digits (if its number of digits is even) or 2d + 1 digits (if its number of digits is odd), such that when we split its square into two pieces, a right hand piece r containing d or d + 1 digits, and a left piece l containing the remaining d digits, the sum of the two pieces is equal to the original number.
We need to find Kaprekar numbers within a given range. So I wrote the following piece of code:
def checkIsKaprekar( num ):
string_num = str(num**2)
if num == int(string_num[:len(string_num)//2]) + int(string_num[len(string_num)//2:]):
return True
kaprekars = [ str(i) for i in range(int(input()),int(input())) if checkIsKaprekar(i) == True ]
print (' '.join(kaprekars))
The problem with the above solution is when we pass single digit numbers, they are converted into a string of which the right half is the number itself and the left half is ''. Now I am converting these strings to int(), which throws an exception.
To avoid this situation, I changed my code to this:
def checkIsKaprekar( num ):
string_num = str(num**2)
left_string = string_num[:len(string_num)//2]
right_string = string_num[len(string_num)//2:]
left_num = int(left_string) if left_string != '' else 0
right_num = int(right_string) if right_string != '' else 0
if num == left_num + right_num:
return True
kaprekars = [ str(i) for i in range(int(input()),int(input())) if checkIsKaprekar(i) == True ]
print (' '.join(kaprekars))
But I don't like this code even though it does solve the purpose. How can I tweak my original code to produce the right output?
Link to the question: https://www.hackerrank.com/challenges/kaprekar-numbers
Is there any way I can specify a default value of 0 if the string is ''?
As answered by B.M. int('0'+string_num[:len(string_num)//2]) works perfectly. int('0'+'') produces 0.
Maybe the cleanest way would be to define a function like this:
def to_int(s)
return int(s) if s else 0
But it may not be worth the overhead in this case.
It's often convenient to solve problems on integers ... with integers:
def kaprekar(n):
l=1
while l<=n : l*=10
return n== (n*n)//l + (n*n)%l
[print(x,end=' ') for x in range(1,100) if kaprekar(x)]
# 1 9 45 55 99
def checkIsKaprekar( num ):
if(num==1):
return True
elif((num**2)<15):
return False
string_num = str(num**2)
if num == int(string_num[:len(string_num)//2]) + int(string_num[len(string_num)//2:]):
return True
You can find Kaprekar numbers within a given range by testing each number for the given condition in the given range. For example,
def print_Kaprekar_nums(start, end):
for i in range(start, end + 1):
# Get the digits from the square in a list:
sqr = i ** 2
digits = str(sqr)
# Now loop from 1 to length of the number - 1, sum both sides and check
length = len(digits)
for x in range(1, length):
left = int("".join(digits[:x]))
right = int("".join(digits[x:]))
if (left + right) == i:
print("Number: " + str(i) + "Left: " + str(left) + " Right: " + str(right))
print_Kaprekar_nums(150, 8000)
This will give the output:
Number: 297Left: 88 Right: 209
Number: 703Left: 494 Right: 209
Number: 999Left: 998 Right: 1
Number: 1000Left: 1000 Right: 0
Number: 2223Left: 494 Right: 1729
Number: 2728Left: 744 Right: 1984
Number: 4879Left: 238 Right: 4641
Number: 4950Left: 2450 Right: 2500
Number: 5050Left: 2550 Right: 2500
Number: 5292Left: 28 Right: 5264
Number: 7272Left: 5288 Right: 1984
Number: 7777Left: 6048 Right: 1729

Categories

Resources