Answer to Question #200850 in Python for praveen

Question #200850

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 1

I am 5 years and 11 months old

Sample Output 1

I am 11 years and 5 months old


Sample Input 1

Exam2 will be held on 1st week of august20

Sample Output 1

Exam20 will be held on 2st week of august1


1
Expert's answer
2021-05-30T13:35:33-0400
s = input()
ls = s.split()
sNums = []
neg = -1


num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']


for i in range(len(ls)):
    ss = ''
    for let in ls[i]:
        if let in num:
            ss += let


    
    if len(ss)>0:
        sNums.append(int(ss))
        ls[i] = ls[i].replace(ss, str(neg))
        neg -= 1


neg = -1


sNums.sort(reverse=True)


for i in sNums:
    for j in range(len(ls)):
        if str(neg) in ls[j]:
            ls[j] = ls[j].replace(str(neg), str(i))


    neg -= 1


print(*ls)


Sample Input 1:
I am 5 years and 11 months old

Sample Output 1:
I am 11 years and 5 months old


Sample Input 2:
Exam2 will be held on 1st week of august20

Sample Output 2:
Exam20 will be held on 2st week of august1

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

Ahmed
31.05.21, 06:48

Its working.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS