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
input1 = input('')
words = input1.split(' ')
numbers = []
num = ['0','1','2','3','4','5','6','7','8','9']
neg = -1
for x in range(len(words)):
s = ''
for let in words[x]:
if let in num:
s += let
if len(s) > 0:
numbers.append(int(s))
numbers[x] = numbers[x].replace(s, str(neg))
neg -= 1
neg = -1
numbers.sort(reverse = True)
for i in numbers:
for j in range(len(words)):
if str(neg) in words:
words[j] = words[j].replace(str(neg), str(i))
neg -= 1
print(*words)
Comments
Leave a comment