Answer to Question #211195 in Python for pandu

Question #211195

Rearrange numbers in string ?


input = I am 5 years and 11 months 20old6

output = I am 20 years and 11 months 6old5


1
Expert's answer
2021-06-27T14:10:05-0400
import re
#get string from the user
inputString = input()
#get all integer from the string
numbers=[int(n) for n in re.findall(r'\d+', inputString)]
#sort all numbers
numbers.sort(reverse=True)
#Replace all numbers with the start "*" 
inputString=re.sub("\d+", "*", inputString)
outputString=""
for letter in inputString:
    #check if the letter in the inputed string is the star "*"
    if letter=="*":
        #add a number to a new string
        outputString+=str(numbers.pop(0))
    else:
        #add a letter to a new string
        outputString+=letter
#display output string
print(outputString)




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