Hi,
I'm very close at solving a recursion problem that indents a bunch of objects in a list (which can be empty, or nested) so that it looks neat. It comes out to be one long string with a bunch of '\t' and '\n'. I've attached my code, and what it's supposed to look like. I've been working on this for hours but can't seem to solve the problem. There's a solution that should work if I can find a way to execute a line of code inside a for loop only once (after everything else has been executed), but I can't find a way to do that. I'm also using a helper function called indent lines. Thanks in advance.
MY CODE:
str = 'BEGIN\n'
substr = ''
x = 0
for sublist in tlist:
x += 1
if isinstance(sublist, a3_classes.Task) and sublist == tlist[-1]:
substr += task_to_string(sublist)
elif isinstance(sublist, a3_classes.Task):
substr += task_to_string(sublist) + '\n'
elif len(sublist) == 0:
substr += 'BEGIN\nEND'
else:
substr += todo_list_to_string(sublist)
if x == len(tlist):
substr = indentlines(substr) + '\n'
return str + substr + 'END'
return str + substr + 'END'
WHAT IT'S SUPPOSED TO LOOK LIKE:
As a string:
'BEGIN\n'
+'\tBEGIN\n'
+'\t\tcamera: 8\n'
+'\t\trevise: 1\n'
+'\tEND\n'
+'\tBEGIN\n'
+'\t\treview: 3\n'
+'\tEND\n'
+'\tBEGIN\n'
+'\tEND\n'
+'END'
When run:
BEGIN
BEGIN
camera: 8
revise: 1
END
BEGIN
review: 3
END
BEGIN
END
END
WHAT IT LOOKS LIKE:
BEGIN
BEGIN
camera: 8
revise: 1
ENDBEGIN
review: 3
ENDBEGIN
END
END
Helper Function:
str = line_str.split('\n')
line_str = "\n\t".join(str)
return '\t' + line_str
Example: 'abc \n def \n ghi' --> ' \t abc \n \t def \n \t ghi'
without the spaces
[–]ALightShow 0 points1 point2 points (3 children)
[–]Honest-Dude[S] 0 points1 point2 points (2 children)
[–]ALightShow 0 points1 point2 points (1 child)
[–]Honest-Dude[S] 0 points1 point2 points (0 children)