Answer to Question #305933 in Python for Sneha

Question #305933

Number Arrangement


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 is a string containing the actual sentence.


Output

The output is a string containing the modified sentence as mentioned above.


Explanation

For example, if the actual sentence is It is a 3days 4nights holiday trip.

The numbers in the sentence are 3 and 4.After arranging them in the decreasing order, the output looks like It is a 4days 3nights holiday trip.


Sample Input1

It is a 3days 4nights holiday trip

Sample Output1

It is a 4days 3nights holiday trip


1
Expert's answer
2022-03-04T02:15:44-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,reverse=True)
    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 = "It is a 3days 4nights holiday trip"
w = ModifyString(s)
print("Original 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