Answer to Question #241855 in Python for Pratyusha

Question #241855

Rearrange numbers in the string

example

hi im 98 and -12 also

output= hi im -12 and 98 also


1
Expert's answer
2021-09-24T14:24:56-0400
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])

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