python


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)[0])
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)

Related Links

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

Categories

HOME
jsf
sas
elixir
google-contacts
smartphone
cocos2d-x-3.0
couchdb-2.0
tizen-wearable-sdk
sequelize.js
proguard
eclipse-cdt
metronic
oclint
rfid
uiscrollview
abcpdf
uiview
future
line-api
pyyaml
anychart
atlassian-plugin-sdk
internet-explorer-8
linq-to-sql
device-detection
ip-camera
cython
tweets
rails-activerecord
epicor
profile
jquery-waypoints
bootstrap-typeahead
yosys
iis-10
lync-2013
dss
chrome-native-messaging
dpi
freebase
recurrence-relation
nlb
xamarin.uitest
codesys
linq-to-entities
lexical-analysis
bing-translator-api
constants
mod-fcgid
apiary
onmouseover
ideone
ticker
execl
httplib2
opencpu
emoticons
rsqlite
push-diffusion
xib
picasa
instruments
soda
clob
gemini
faraday
elastix
spinlock
magento-1.4
galaxy
android-snackbar
ffprobe
debian-based
jai
jcr-sql2
lexicographic
scala-2.11
errorprovider
fragment-tab-host
x-ua-compatible
power-law
neoload
unison
healthvault
windows-mobile-6
sttwitter
ruboto
stagefright
qss
pep8
awesomeprint
template-haskell
phpsh
usn
qsqltablemodel
couchdb-lucene
signals2
point-sprites
charts4j
adobe-contribute
squeel
open-graph-beta
asp.net-routing
querypath
quick-search
unreachable-code
clients
openwysiwyg
integer-promotion
visual-studio-2010-beta-2
winsnmp
castle-validators
web-analytics-tools

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App