forked from charlesfranciscodev/codingame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgx_formatter.py
61 lines (54 loc) · 1.8 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
56
57
58
59
60
61
class CGXFormatter:
def __init__(self):
self.BASE_INDENT = 4
self.inside_string = False
self.total_indent = 0
self.new_line = True
self.nb_lines = 0
def read_lines(self):
self.nb_lines = int(input())
for _ in range(self.nb_lines):
line = input()
for character in line:
self.read_char(character)
def read_char(self, character: str):
if self.inside_string:
if character == '\'':
self.inside_string = False
self.print_char(character)
else:
self.read_char_outside_string(character)
def read_char_outside_string(self, character: str):
if character == ' ' or character == '\t':
return
if character == '\'':
self.inside_string = True
self.print_char(character)
elif character == '(':
if not self.new_line:
self.print_new_line()
self.print_char(character)
self.print_new_line()
self.total_indent += self.BASE_INDENT
elif character == ')':
self.total_indent -= self.BASE_INDENT
if not self.new_line:
self.print_new_line()
self.print_char(character)
elif character == ';':
self.print_char(character)
self.print_new_line()
else:
self.print_char(character)
def print_char(self, character: str):
if self.new_line:
for _ in range(self.total_indent):
print(' ', end='')
self.new_line = False
print(character, end='')
def print_new_line(self):
print()
self.new_line = True
if __name__ == "__main__":
formatter = CGXFormatter()
formatter.read_lines()