Rearrange numbers in string
given a string, write a program to re-arrange all the numbers appearing on the string in decreasing order.
Note: There will not be any negative numbers or numbers with decimal part
def Getdigits():
for w in range(len(string2)):
if string2[w].isdigit():
intW=int(string2[w])
dig.append(intW)
digW.append(w)
dig.sort(reverse=True)
return dig
if __name__ == '__main__':
string1 = input("Enter a string: ")
string2 = string1.split()
dig = []
digW = []
digString=[]
Getdigits()
# convert to string
for n in dig:
dw=str(n)
digString.append(dw)
for i in range(len(digW)):
string2[digW[i]] = digString[i]
print(" ".join(string2))
Comments
Leave a comment