Answer to Question #179145 in Python for jayanth

Question #179145

Shift Numbers - 2

Given a string, write a program to move all the numbers in it to its start.Input


The input will contain a string A.Output


The output should contain a string after moving all the numbers in it to its start.Explanation


For example, if the given string A is "1good23morning456", the output should be "123456goodmorning", as it contains numbers at the start.

Sample Input 1

1good23morning456

Sample Output 1

123456goodmorning

Sample Input 2

com876binat25ion

Sample Output 2

87625combination




1
Expert's answer
2021-04-07T10:11:53-0400
import sys


if len(sys.argv) > 1:
    s = sys.argv[1]
else:
    try:
        s = raw_input()
    except:
        s = input()


num_idx = 0
str_idx = 0


chars = [c for c in str(s)]
while str_idx < len(s):
    c = s[str_idx]
    if c.isdigit():
        chars.pop(str_idx)
        chars.insert(num_idx, c)
        num_idx += 1
    str_idx += 1
print(''.join(chars))

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