Sep-20-2019, 06:58 PM
hi everyone
how can i write this code?
input = 3+2+1+2+1
#sort numbers
output = 1+1+2+2+3
how can i write this code?
input = 3+2+1+2+1
#sort numbers
output = 1+1+2+2+3
help for programming
|
Sep-20-2019, 06:58 PM
hi everyone
how can i write this code? input = 3+2+1+2+1 #sort numbers output = 1+1+2+2+3
Sep-20-2019, 07:10 PM
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
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: but i dont know why write dubble + befor number 2 and 3 and i want to dosnt show + after my last number
Sep-20-2019, 09:38 PM
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
Sep-21-2019, 08:52 AM
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.
Sep-21-2019, 06:42 PM
(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. thanks alot |
|