1. Integer extraction
Title description
Input an integer, separate the odd and even numbers in its digits to form a new integer, and output the absolute value of the difference between the two integers. The odd number of 120395: 1395, the even number: 20, and the absolute value of the output difference
: 1375120395 The odd number: 1395, the even number: 20, the absolute value of the output difference: 1375.
def fun(s):
even=0 #偶数
odd=0 #奇数
for i in s:
if int(i)%2==0:
even=even*10+int(i)
else:
odd=odd*10+int(i)
return even-odd if even>odd else odd-even
fun('120395')
result:
1375