Given two numbers a
and b
, definition of decimal zip is as explained below:
• the first (i.e. the most significant) digit of C is the first digit of a
;
• the second digit of C is the first digit of b
;
• the third digit of C is the second digit of a
;
• the fourth digit of C is the second digit of b
;
• etc.
If one of the integers a
and b
runs out of digits, the remaining digits of
the other integer are appended to the result.
Example:
Input: a = 12, b = 59 Output should be: c = 1529
Input: a = 1094, b = 12 Output: c = 110294
I have this code:
a = 1234534543
b = 3525523342
a = str(a)
b = str(b)
aLen = len(a)
bLen = len(b)
c = "".join([x+y for (x,y) in zip(a,b)]) + a[min(aLen,bLen):] + b[min(aLen,bLen):]
print(int(c))
It works, but is there a more pythonic way to achieve the same?
Problem with the above code is:
- I am trying to append the remaining digits of
a
andb
although one or them or neither of them will be unnecessary in certain cases. - It takes some time when the input is too big.