Answer to Question #216390 in Python for manoj

Question #216390

Given a string, write a program to re-arrange all the numbers appearing in the string in decreasing order. Note: There will not be any negative numbers or numbers with decimal part


1
Expert's answer
2021-07-12T17:18:04-0400
def IsNum(s):
    try:
        int(s)
        return True
    except ValueError:
        return False


s = input()
words = s.split()
numbers = []
for x in words:
    if IsNum(x):
        numbers.append(int(x))
if len(numbers) < 2:
    print(s)
numbers.sort(reverse=True)
result = ''
for x in words:
    if IsNum(x):
        result += str(numbers[0])
        numbers = numbers[1:]
    else:
        result += x
    result += ' '
print(result[:-1])

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