Rearrange numbers in the string
example
hi im 98 and -12 also
output= hi im -12 and 98 also
def IsNum(s):
try:
int(s)
return True
except ValueError:
return False
s = input()
words = s.split()
numbers = []
for x in words:
if IsNum(x):
numbers.append(int(x))
if len(numbers) < 2:
print(s)
numbers.sort()
result = ''
for x in words:
if IsNum(x):
result += str(numbers[0])
numbers = numbers[1:]
else:
result += x
result += ' '
print(result[:-1])
Comments
Leave a comment