rearrange numbers in a string
given a string write a program to arrange all numbers appearing in the string in decreasing order
line = input()
words = []
numbers = []
word = ''
number = None
munus = False
for ch in line:
if ch.isdigit():
if number is None:
number = int(ch)
if minus:
word = word[:-1]
words.append(word)
word = ''
else:
number = number*10 + int(ch)
else:
if word == '':
word = ch
if number is not None:
if minus:
minus = False
number *= -1
numbers.append(number)
number = None
else:
word += ch
if ch == '-':
minus = True
else:
minus = False
if number is not None:
numbers.append(number)
else:
words.append(word)
numbers.sort(reverse=True)
line = ''
for i in range(len(numbers)):
line += words[i] + str(numbers[i])
if len(words) > len(numbers):
line += words[-1]
print(line)
Comments
Leave a comment