 # Python program - Number arrangement

Hi everyone,

I need help in understanding the way to approach for the below question.

well i am not sure about the efficiency of the logic but i think
we can iterate from first character to last charecter of the string and store the numirical value ,
position of the value in two diffrent arrays(array_value , array_loc) .
now sort the array which have all the numirical values (array_value)
now assign the values of sorted array to the orginal array at the locations stored in array_loc.

1 Like

Hello guys, earlier you people helped with few doubts that’s why iam tagging you here.

kindly help me with this code.

As we see from the example, the numbers are not attached to the following word. The remaining uncertainty is whether we are catering for multi-digit numbers (10 and up).

So we need to find the location of the numbers in the string, separate them out, sort a list of the numbers, and re-assemble the string. There are no doubt some clever ways to do the splitting with regex but that’s not usually a beginner’s tool. So the general structure for me would be:

``````# Divide the string into text sections and numbers (edge case: check there are some numbers)
# ending with the following:
# - text_parts is a list of strings; initial/final empty string implies an initial/final number
# - num_parts is a list of the interspersed numbers, one shorter than above

# Sort num_parts, make into strings

# Reassemble the string
final_string = "".join(a+n for a,n in zip(text_parts, num_parts)) + text_parts[-1]
``````

The first part is perhaps hardest, but a simple traverse of the string, keeping track of whether you’re currently working through text or numerics should be easy enough.

1 Like

Thanks brother.

s = str(input())

number =[]

position =[]

k=0

l=0

for i in range(len(s)):

``````if s[i]== '1' or s[i]== '2'or s[i]== '3' or s[i]== '4' or s[i]== '5' or s[i]== '6' or s[i]== '7' or s[i]== '8' or s[i]== '9' or s[i]== '0':

number.append(s[i])

position.append(i)
``````

number =sorted(number)

s1 = list(s)

for i in range(len(position)):

``````s1[position[i]] =number[i]
``````

for j in s1:

``print(j,end="")``
1 Like

It worked, Thanks a lot. i thought it was the complex one but the way you prepared the code was brilliant. i made a small change, just wanted to try.

and can you please explain the last line? i never worked with end function. Kindly explain it.

``````for j in s1:
print(j,end="")
``````
``````given_string = input()
numbers = ['1','2','3','4','5','6','7','8','9','0']
number_list = []
positions = []
for i in range(len(given_string)):
if given_string[i] in numbers:
number_list.append(given_string[i])
positions.append(i)

number_list.sort()
string = list(given_string)
for i in range(len(positions)):
string[positions[i]] = number_list[i]

for j in string:
print(j,end="")
``````

The print built-in function takes four optional named parameters, the two most commonly used being `sep` and `end`. `sep` (separator) specifies what goes in between items in a print list, defaulting to a space, and `end` specifies what character is sent at the end of the list, defaulting to newline `"\n"`, but used here with an empty string to suppress the newline. Really there should be another `print()` after the loop.

To achieve the same result as here, instead of the explicit loop you could send the unpacked list (prefix an asterisk) to the `print` function with `sep=""`:

``````print (*string, sep="")
``````

which has the advantage of only calling `print` once and keeps the final newline.

(Also I would never call a variable `string`, but that’s another discussion)

1 Like