Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help for programming
#1
hi everyone
how can i write this code?
input = 3+2+1+2+1
#sort numbers

output = 1+1+2+2+3
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020