[2016-05-30] Challenge #269 [Easy] BASIC Formatting

Solution in Python3, implements bonus. Took some for "G33kDude"s code.

challengeinput = """12 ···· VAR I ·FOR I=1 TO 31 »»»»IF !(I MOD 3) THEN ··PRINT "FIZZ" ··»»ENDIF »»»»····IF !(I MOD 5) THEN »»»»··PRINT "BUZZ" ··»»»»»»ENDIF »»»»IF (I MOD 3) && (I MOD 5) THEN ······PRINT "FIZZBUZZ" ··»»ENDIF »»»»·NEXT """

linecount, indent, codes = challengeinput.splitlines() stack=[] edited="" error=False linecount=int(linecount) lineNumber=0 for line in codes: lineNumber+=1 line = line.lstrip('·» \t') if line.startswith('ENDIF'): if (stack.pop()!="IF"): print("-------------------------------") print("Line {}: {}".format(lineNumber,line)) print("Error: expected NEXT") print("-------------------------------") error=True break if line.startswith('NEXT'): if (stack.pop()!="FOR"): print("-------------------------------") print("Line {}: {}".format(lineNumber,line)) print("Error: expected ENDIF") print("-------------------------------") error=True break edited+=(indentlen(stack) + line)+"\n" if line.startswith('IF'): stack.append("IF") if line.startswith('FOR'): stack.append("FOR") if (not(error) and lineNumber==linecount): if (len(stack)>0): if (stack.pop()=="IF"): print("-------------------------------") print("Line {}: {}".format(lineNumber,line)) print("Error: expected ENDIF") print("-------------------------------") elif (stack.pop()=="FOR"): print("-------------------------------") print("Line {}: {}".format(lineNumber,line)) print("Error: expected NEXT") print("-------------------------------") else: print(edited)

/r/dailyprogrammer Thread