I guess I didn’t search for the right keyword so ended not founding an existing answer…. I’m trying to get the best way of getting the max or min of a 2 lists with same length, taking the max or min of each position.
I got to a point as below
Input A = 725324
Input B = 9341
Expected Max calculation of A & B would be 729344 (taking the max number on that position)
I got to the point only aligning 2 numbers to have same digits, and zipped the 2 strings, then got stuck with below list… the way I’m going towards will be Max(L[1])
, Max(L[2])
…all the way to Max(L[digits - 1])
,but not sure what’s the best way of expressing it in Python and if that’s the right approach.
List = [(7, 0), (2, 0), (5, 9), (3, 3), (2, 4), (4, 1)]
CodePudding user response:
You can use string padding str.rjust()
to properly pad the "shorter" number:
a = 725324
b = 9341
a, b = map(str, (a, b))
w = max(map(len, (a, b)))
result = ""
for t in zip(a.rjust(w, "0"), b.rjust(w, "0")):
result = max(t)
result = int(result)
Output:
729344
CodePudding user response:
One approach we can use for this problem is to use math (division and remainders):
a = 725324
b = 9341
digits = 0
num = 0
while a != 0 or b != 0:
if a != 0 and b != 0:
num = 10 ** digits * max(a % 10, b % 10)
a //= 10
b //= 10
elif a != 0:
num = 10 ** digits * (a % 10)
a //= 10
else:
num = 10 ** digits * (b % 10)
b //= 10
digits = 1
print(num)
With this code, we will enter a while loop while either a
or b
is nonzero.
From there, if both a
and b
are nonzero, then we find which number has the greatest last digit, which we calculate by doing % 10
. We then scale it up so that we can add it to a total variable, num
by storing the number of digits we have added, digits
, and raising 10
to the power of digits
and multiplying that value by the remainder.
We then do this same process for if only a
or if only b
are nonzero, but without taking the other variable into account.
I hope this helped! Please let me know if you need any further clarification or details :)
CodePudding user response:
Here is another pure numerical solution using divmod
and powers of 10:
def max_digit(A,B):
out = 0
exp = 0
if A < B: # ensure looping on the largest number
B,A = A,B
while A>0:
A,a = divmod(A,10) # get unit and shift
B,b = divmod(B,10)
out = (10**exp)*max(a,b)
exp = 1
return out
A = 725324
B = 9341
max_digit(A,B)
# 729344
CodePudding user response:
If I understand your problem well so here is my solution
def maximum_digits():
a = input("nummber a :")
b = input("nummber b :")
# finding the difference lenght between two number
c = len(a) - len(b)
# making a list of each digits in a number so 725324 >>> [7,2,5,3,2,4]
list_a = []
a=int(a)
while a > 0:
list_a.append(a % 10)
a //= 10
list_b = []
b=int(b)
while b > 0:
list_b.append(b % 10)
b //= 10
# making the two number have same lenght by adding zero
if a >= b:
for k in range(c):
list_b.append(0)
else:
for k in range(c):
list_a.append(0)
final_list = []
for i, j in zip(list_a, list_b):
if i > j:
final_list.append(i)
else:
final_list.append(j)
number = ""
for i in final_list[::-1]:
number = str(i)
return number
print(maximum_digits())
CodePudding user response:
The solution by @ddejohn written even shorter:
a = 725324
b = 9341
a, b = map(str, (a, b))
w = max(map(len, (a, b)))
result = int("".join(max(t) for t in zip(a.rjust(w, "0"), b.rjust(w, "0"))))