Ordered sequence of ascending integer combinations

Write a program or function to produce the following output in the correct order.

EDIT: The symbols are not mathematical! The numbers just represent unique data and the + and - could be any two arbitrary symbols.

Take an non-negative integer input n. The first line is always -, even for n=0.

• If the current line is -, the next line is 1+2+ ... (n-1)+n-
• n=4: - => 1+2+3+4-
• If the last integer is equal to n, remove all integers from the end that are immediately followed by a -, then change the last + to a -
• n=4: 1-2+3-4- => 1-2-
• EDIT: When the string is full (all the integers from 1 to n are included), remove all integers from the end that are followed by a -, until you reach an integer followed by a +. Leave that integer but change its following + to a -
• Using the same example as immediately above (which does not follow -), remove 4-, remove 3-, change 2+ to 2-. 1- doesn't change since we stop at 2. Result: 1-2-
• If the last integer is less than n, append the remaining integers with a + after each one, except the final integer which should have a - appended
• n=4: 1+2- => 1+2-3+4-
• EDIT: If the current string is not full (does not contain all the integers from 1 to n), add each integer not already included in ascending order up to n-1 with a + after each one, and then append the last integer n followed by a -
• If the current line is 1-, append 2+, append 3+ which is n-1 if n=4. Then append 4-. Result: 1-2+3+4-
• If the current line contains all the integers and each one is immediately followed by a -, exit the code
• n=4: 1-2-3-4- => END

There must be no leading or trailing spaces on any line. There must be a line break between each line. There may or may not be a line break on the last line.

EDIT: You should test your code up to at least n=10 (over 1000 lines of output so I can't include it here). Any number that doesn't cause your code to run out of resources should (eventually!) produce the correct output but you don't have to wait for the universe to end!

This is , so shortest code in bytes wins!

Input n=0:

-

Input n=1:

-
1-

Input n=2:

-
1+2-
1-
1-2-

Input n=4:

-
1+2+3+4-
1+2+3-
1+2+3-4-
1+2-
1+2-3+4-
1+2-3-
1+2-3-4-
1-
1-2+3+4-
1-2+3-
1-2+3-4-
1-2-
1-2-3+4-
1-2-3-
1-2-3-4-

g takes an integer and returns a string.

g n=unlines$max"-".foldr(\(s,i)r->id=<<[show i++s:r|s:r>"+"])""<$>mapM(mapM(,)"+-")[1..n]

Try it online!

How it works

• Loosely speaking, the algorithm constructs a list of all 2^n combinations 1+2+...n+, 1+2+...n- up to 1-2-...n-, strips away the final +-terminated numbers, and if the result is empty, replaces it with -.

• mapM(,)"+-" is a shorter way (using the function monad) to write \i->[('+',i),('-',i)].

• mapM(mapM(,)"+-")[1..n] generates (using the list monad for the outer mapM) a list with all combinations as lists of tuples e.g. [(1,'+'),(2,'-'),...,(n,'+')].

Try it online!

Python 3, 305 bytes

x=int(input())
o=lambda:list(range(1,x+1))or
q=o()
q[-1]*=-1
print('-')
def f(a):print(''.join(str(abs(x))+'-+'[x>0]for x in a))
while any([k>0 for k in q])or[-k for k in q]!=o():
f(q)
if-q[-1]==x:
while q[-1]<0:q=q[:-1]
q[-1]*=-1
elif-q[-1]<x:
while q[-1]<x:q+=[abs(q[-1])+1]
q[-1]*=-1
f(q)

Try it online!

• I'm a bit confused by your tab/space mixing. First I didn't even think you COULD in Python 3, and second it seems to serve little purpose? Why bother doing <tab><space> when <space><space> would be the same # of bytes? I guess perhaps if you did the small indents with <space> and the larger with <tab> it would be saving a byte... Jul 14 '17 at 1:51
• @nmjcman101 erm. I must be really tired, or stupid, or both. I was trying to save bytes by doing that space/tab indent not tab/tabspace >< thanks! Jul 14 '17 at 1:55
• This is 17 bytes shorter, but it does exit on an error. Still trying to shorten that pesky q[-1] to q. BTW: mixing tabs and spaces doesn't work in Python 3, so the current code yields an error. Jul 14 '17 at 1:58
• @notjagan oh hm. never mind then, it must have been because earlier I was being dumb :P but okay, thanks for the suggestion; i'll try to work from there Jul 14 '17 at 2:18