JoshDreamland icon

Fixed solution: add braces for spaces

JoshDreamland | PRO | 12/14/14 11:02:30 PM UTC | 0 ⭐ | 412 👁️ | Never ⏰ | []
Python |

710 B

|

None

|

0 👍

/

0 👎

def countSpaces(str):
    for i in range(0, len(str)):
        if str[i] != ' ' and str[i] != '\t':
            return i
    return len(str)
 
def addAll(lines, at, out, lastIndent):
    while at < len(lines):
        indent = countSpaces(lines[at])
        if indent > lastIndent:
            out.append(' ' * lastIndent + "{")
            at = addAll(lines, at, out, indent)
            out.append(' ' * lastIndent + "}")
        elif indent == lastIndent or indent == 0:
            out.append(lines[at])
        else:
            return at - 1
        at = at + 1
    return at
 
lines = open('data.txt').read().split('\n');
output = [];
addAll(lines, 0, output, 0)
for line in output:
    print line

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎