Given a string, write a program to move all the numbers in it to its start.
def move_numbers(S):
numbers = ''
not_numbers = ''
in_number = False
for ch in s:
if '0' <= ch <= '9':
numbers += ch
in_number = True
elif ch == ' ' and in_number:
numbers += ch
in_number = False
else:
not_numbers += ch
in_number = False
return numbers + not_numbers
s = '33 cows can graze a field in 12 days. How many cows will graze the same field in 9 days?'
s = move_numbers(s)
print(s)
Comments
Leave a comment