Answer to Question #188761 in Python for J NAGAMANI

Question #188761

Rearrange Numbers in String

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.

Input

The input will be a single line containing a string.

Output

The output should be a single line containing the modified string with all the numbers in string re-ordered in decreasing order.

Explanation

For example, if the given string is "I am 5 years and 11 months old", the numbers are 5, 11. Your code should print the sentence after re-ordering the numbers as "I am 11 years and 5 months old".


Sample Input

I am 5 years and 11 months old

Sample Output

I am 11 years and 5 months old




1
Expert's answer
2021-05-03T17:51:52-0400
def mydigits():
    for w in range(len(string2)):
        if string2[w].isdigit():
            intW=int(string2[w])
            dig.append(intW)
            digW.append(w)
            dig.sort(reverse=True)
    return dig


if __name__ == '__main__':
    string1 = input("Enter a string: ")
    string2 = string1.split()
    dig = []
    digW = []
    digString=[]
    mydigits()
    # convert to string
    for n in dig:
        dw=str(n)
        digString.append(dw)

    for i in range(len(digW)):
        string2[digW[i]] = digString[i]
    print(" ".join(string2))

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