How can I compare the difference between digits of two numbers?
For eg I have two numbers:4532 and 5489.I want to calculate the least difference between first digit of 4532 which is 4 and all the digits of 5489 ie 4-5, 4-4, 4-8 and 4-9.How can I do it? num = "4532" num2 = "5489" for j in num2: if int(i)-int(j)<int(i)-int(j+1): print(int(i)-int(j)) #I am getting list index out of bound exception
It's not a nice code, but it's working. You can adjust this to what you want. Keep in mind that if you have two digits with the same result you wouldn't know that (you'll be left with the first who matched, in that case) In that example you'll get the digit that will give you the least difference. You can change it to save the result instead. num1 = 4532 num2 = 5489 ln2 = len(str(num2)) firstDigit = int(str(num1)) lowest = None temp = num2 for i in range(ln2): last = temp % 10 temp //= 10 diff = abs(firstDigit - last) if lowest is None or diff < abs(lowest - firstDigit): lowest = last print(lowest)
Identifying largest connected component in a matrix
VBA shell script set environment variable before execution
Embed matplotlib in PyQt
Odoo - overriding old api method with new api?
Pylint warning - W5304(Missing-Test-Class)
In terms of efficiency/quickly rejecting things, does it matter if your if statements are on the same line?
QComboBox drop-down list adding unnecessary scroll bar
Specify color type for bar graph pylab
prettifying a part of the html doc using beautifulsoup
Django concatenate two querysets for same model
How can I validate input to accept only binary numbers?
Custom route predicates in Pyramid
Why is my query for a structured property failing with BadFilterError?
Simple Audio Units Host to drive an Audio Units Instrument
kivy language cumbersomeness and rationale behind it
detect if variable is of sympy type