Answer to Question #176330 in Python for phani

Question #176330
Maximum Product of the Digits
Given a range represented by two positive integers L and R. Find the number lying in the range having the maximum product of the digits.
Input

The first line of input will be an integer, denoting the number of test cases T.
The next T lines will contain two space-separated integers, denoting the L and R.
Output

The output should be T lines containing the the number with the maximum product of the digits in the range L and R for each test case.
Explanation

For example, if the given T is 2, read the L and R of the first test case in the next line. If the given L and R are 1 and 10 respectively. As 9 is the number with the maximum product of digits, the output for the first test case should be 9.
If the L and R are 15 to 30 respectively. The product of the digits of number 29 is 18. As 18 is the maximum product in the range, the output should be 29.
Sample Input 1
2
1 10
15 30
Sample Output 1
9
29

Sample Input 2
4
100 200
51 62
10 30
20 80
Sample Output 2
199
59
29
79
1
Expert's answer
2021-03-31T17:07:06-0400
n = int(input())
A = []
for i in range(n):
    A.append([int(i) for i in input().split()])
 
B = []
for i in range(n):
    maxValue = 0
    maxIndex = 0
    for j in range(A[i][0], A[i][1] + 1):
        sum = 1 
        temp = j
        while True:
            sum *= (temp%10)
            temp = temp // 10
            if temp == 0:
                if sum > maxValue:
                    maxValue = sum
                    maxIndex = j
                break
    B.append(maxIndex)
for i in range (len(B)):
    print(B[i])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS