-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCGX-Formatter.py
55 lines (50 loc) · 1.46 KB
/
CGX-Formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import math
# https://www.codingame.com/training/hard/cgx-formatter
def check_next_line(c, last_char):
global temp_string
global tabs
if last_char == '=':
print(ord(c), file=sys.stderr)
if last_char == ')' and c != ';' and c != ')' and c != '(':
temp_string += '\n' + (tabs * 4) * " "
elif last_char == '(' and c != ")":
temp_string += '\n' + (tabs * 4) * " "
elif last_char == ';':
temp_string += '\n' + (tabs * 4) * " "
elif last_char == '=' and c == '(':
temp_string += '\n' + (tabs * 4) * " "
cgxlines = []
n = int(input())
for i in range(n):
cgxline = input()
cgxlines.append(cgxline)
tabs = 0
temp_string = ''
string_started = False
last_char = ''
for line in cgxlines:
for c in line:
if c == '\'' and not (string_started):
check_next_line(c, last_char)
string_started = True
temp_string += c
elif string_started and c == '\'':
string_started = False
temp_string += c
elif string_started:
temp_string += c
elif (c.isspace()):
continue
else:
check_next_line(c, last_char)
if c == '(':
temp_string += "("
tabs += 1
elif c == ')':
tabs -= 1
temp_string += '\n' + (tabs * 4) * " " + ")"
else:
temp_string += c
last_char = c
print(temp_string)