Python Forum

Full Version: help for programming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi everyone
how can i write this code?
input = 3+2+1+2+1
#sort numbers

output = 1+1+2+2+3
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.

I will tell you that you will need the split method of strings and the sort method of lists. If you are supposed to handle integers over 9 you will also need the int built-in function.
ok tnx for your reply

i want to write a progrram that give nubers with sum and sort them and return the list
nums = input('enter your numbers:')
nums = nums.replace('+', '')
nums = sorted(nums)
#print(nums)
def convert(s):
    st=''
    return(st.join(s))
nums = (convert(nums))
print(nums)
for i in nums:
    print(i)
    nums = nums.replace(i,i+'+')
print(nums)
and my output is:
Output:
1+2++2++3+
but i dont know why write dubble + befor number 2 and 3 and i want to dosnt show + after my last number
You are getting double +'s because you have two 2's. You find the first one, and replace with 2+, but that replaces both of them. Then you find the second one, and replace both of them again, giving you the double +'s.

Try replacing line 6 with st = '+'. Then you won't need that last for loop.
If I understand the goal correctly then one can do:

>>> s = '3+2+1+2+1'
>>> '+'.join(sorted(s.split('+')))  
'1+1+2+2+3'
However one should do conversion(s) otherwise this will happen:

>>> s = '10+1+2+1'                                        
>>> '+'.join(sorted(s.split('+')))                        
'1+1+10+2'
(Sep-20-2019, 09:38 PM)ichabod801 Wrote: [ -> ]You are getting double +'s because you have two 2's. You find the first one, and replace with 2+, but that replaces both of them. Then you find the second one, and replace both of them again, giving you the double +'s.

Try replacing line 6 with st = '+'. Then you won't need that last for loop.

thanks alot