Numbering Hierarchical Outlines
Python 2, 77
S={'0':0}
for w in input().split('\n'):S[w]+=1;S[' '+w]=0;print w[:-1]+`S[w]`
Like Sp3000's answer, but with a dictionary. The dict S
stores the current number for each nesting level '0', ' 0', ' 0'
and so on. For each line in the input, increment the corresponding nesting level, and reset the nesting level one higher to 0.
Python 2, 86 85 81 bytes
S=[]
for r in input().split("\n"):S=([0]+S)[-len(r):];S[0]+=1;print r[:-1]+`S[0]`
(-5 bytes thanks to @xnor)
Takes input as a string via STDIN, e.g.
'0\n 0\n 0\n 0\n 0\n0\n 0\n 0\n 0\n 0\n0\n 0\n 0\n 0'
Alternatively, here's a function for 5 extra bytes:
def f(I,S=[]):
for r in I.split("\n"):S=([0]+S)[-len(r):];S[0]+=1;print r[:-1]+`S[0]`
CJam, 25 bytes
LqN/{0+I,<))_IW@toNo+}fI;
Like my Python answer, this uses an array to store which number each indentation level is up to. One difference, however, is that this uses t
(array set) to replace the 0 on each line with the number we want.
Try it online.