Answer to Question #285367 in Python for Ram

Question #285367

Re-order the string


Given a string, write a program to help Eren to modify the string.


sample Input 1

I am 5 years 11 months and 8 days old.


sample output 1

I am 5 years 8 months and 11 days old.




sample Input 2

At 12pm total score is 180 with 8 4s 7 6s in 150 balls.


sample output 2

At 4pm total score is 6 with 7 8s 12 150s in 180 balls.


1
Expert's answer
2022-01-07T05:01:35-0500


import re


def ModifyString(s):
    w = re.split('(\d+)',s)
    numbers = []
    for word in w:
       if word.isdigit():
          numbers.append(int(word))
    numbers = sorted(numbers)
    k=0
    s=""
    for r in range(0,len(w)):
       if w[r].isdigit():
          w[r] = numbers[k]
          k=k+1;
       s = s+str(w[r]   )
    return(s)
    
s = "At 12pm total score is 180 with 8 4s 7 6s in 150 balls."
w = ModifyString(s)
print("Original String: %s"%s)
print("Modified String: %s"%w)


s = "I am 5 years 11 months and 8 days old."
w = ModifyString(s)
print("\nOriginal String: %s"%s)
print("Modified String: %s"%w)

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