Answer to Question #176368 in Python for satyasai

Question #176368

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.


1
Expert's answer
2021-03-28T08:47:04-0400
def product(x):
    prod = 1
    while (x):
        prod *= (x % 10)
        x //= 10
    return prod


def findNumber(l, r):
    a = str(l)
    b = str(r)
    ans = r
    for i in range(len(b)):
        if b[i] == '0':
            continue
        curr = list(b)
        curr[i] = str(((ord(curr[i]) - ord('0')) - 1) + ord('0'))
        for j in range(i + 1, len(curr)):
            curr[j] = str(ord('9'))
        num = 0
        for c in curr:
            num = num * 10 + (int(c) - ord('0'))
        if num >= l and product(ans) < product(num):
            ans = num
    return ans


if __name__ == "__main__":
    l, r = int(input()), int(input())
    print(findNumber(l, r))

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