1
+ import sys
2
+
3
+ OUTPUT_TYPE = 'Output'
4
+ ANSWER_TYPE = 'Answer'
5
+ GOTO_TYPE = 'Goto'
6
+ CONCLUSION_TYPE = 'Conclusion'
7
+
8
+ HOPPER_MESSAGE_PREFIX = 'Hopper: '
9
+ USER_MESSAGE_PREFIX = 'User: '
10
+ CONCLUSION_MESSAGE_PREFIX = 'Conclusion: '
11
+
12
+ class Output :
13
+ def __init__ (self , text , label = None ):
14
+ self .text = text
15
+ self .label = label
16
+ self .type = OUTPUT_TYPE
17
+ def __str__ (self ):
18
+ return 'Output({}, label={})' .format (self .text , self .label )
19
+
20
+
21
+ class Answer :
22
+ def __init__ (self , text ):
23
+ self .text = text
24
+ self .type = ANSWER_TYPE
25
+ def __str__ (self ):
26
+ return 'Answer({})' .format (self .text )
27
+
28
+
29
+ class Goto :
30
+ def __init__ (self , label ):
31
+ self .label = label
32
+ self .type = GOTO_TYPE
33
+ def __str__ (self ):
34
+ return 'Goto({})' .format (self .label )
35
+
36
+
37
+ class Conclusion :
38
+ def __init__ (self , text ):
39
+ self .text = text
40
+ self .type = CONCLUSION_TYPE
41
+ def __str__ (self ):
42
+ return 'Conclusion({})' .format (self .text )
43
+
44
+
45
+ class IndentationAndInputObject :
46
+ def __init__ (self , indentation , input_object ):
47
+ self .indentation = indentation
48
+ self .input_object = input_object
49
+
50
+ def print_conversation (flat_tree , user_answers ):
51
+ # Write your solution below
52
+ # Some example code is provided to show you how to access our data structures, feel free to modify/delete
53
+ cur_indent , res = 0 , []
54
+ response = 0
55
+ chosen = True
56
+ label_dictionary = {}
57
+ for i , v in enumerate (flat_tree ):
58
+ if v .input_object .type == OUTPUT_TYPE :
59
+ label_dictionary [v .input_object .label ] = i
60
+ i = 0
61
+ answers = []
62
+ while i < len (flat_tree ):
63
+ row = flat_tree [i ]
64
+ i += 1
65
+ if row .input_object .type == OUTPUT_TYPE and row .indentation == cur_indent and chosen :
66
+ res .append (row )
67
+ elif row .input_object .type == ANSWER_TYPE :
68
+ answers .append (row .input_object .text )
69
+ if user_answers [response ] == row .input_object .text :
70
+ res .append (row )
71
+ response += 1
72
+ cur_indent += 2
73
+ chosen = True
74
+ else :
75
+ if i == len (flat_tree )- 1 :
76
+ res .append ("User: " + user_answers [response ])
77
+ response += 1
78
+ res .append ("Hopper: Invalid input" )
79
+ for j in range (response , len (user_answers )):
80
+ res .append ("User: " + user_answers [j ])
81
+ res .append ("Conclusion: Goodbye!" )
82
+ break
83
+ chosen = False
84
+ elif row .input_object .type == GOTO_TYPE and chosen :
85
+ i = label_dictionary [row .input_object .label ]
86
+
87
+ elif row .input_object .type == CONCLUSION_TYPE and chosen :
88
+ res .append (row )
89
+ break
90
+
91
+ for row in res :
92
+ if type (row ) == str :
93
+ print (row )
94
+ elif row .input_object .type == OUTPUT_TYPE :
95
+ print (HOPPER_MESSAGE_PREFIX + row .input_object .text )
96
+ elif row .input_object .type == ANSWER_TYPE :
97
+ print (USER_MESSAGE_PREFIX + row .input_object .text )
98
+ elif row .input_object .type == CONCLUSION_TYPE :
99
+ print (CONCLUSION_MESSAGE_PREFIX + row .input_object .text )
100
+
101
+ # for row in flat_tree:
102
+ # if row.input_object.type == OUTPUT_TYPE:
103
+ # print(HOPPER_MESSAGE_PREFIX + row.input_object.text)
104
+ # elif row.input_object.type == ANSWER_TYPE:
105
+ # print(USER_MESSAGE_PREFIX + row.input_object.text)
106
+ # elif row.input_object.type == CONCLUSION_TYPE:
107
+ # print(CONCLUSION_MESSAGE_PREFIX + row.input_object.text)
108
+
109
+ def parse_line (line ):
110
+ def parse_spaces_and_line (line ):
111
+ for i , c in enumerate (line ):
112
+ if c != ' ' :
113
+ return len (line [:i ]), line [i :]
114
+ raise RuntimeError ("Found all whitespace line" )
115
+
116
+ def parse_label_and_output (line_content ):
117
+ for i , c in enumerate (line_content ):
118
+ if not c .isdigit ():
119
+ if c == ':' and i > 0 :
120
+ return int (line_content [:i ]), line_content [i + 1 :]
121
+ else :
122
+ return None , line_content
123
+ return None , line_content
124
+
125
+ indentation , line_content = parse_spaces_and_line (line )
126
+ if line_content .startswith ('-' ):
127
+ return IndentationAndInputObject (indentation , Answer (line_content [1 :]))
128
+ elif line_content .startswith ('=' ):
129
+ return IndentationAndInputObject (indentation , Conclusion (line_content [1 :]))
130
+ elif line_content .startswith ('>' ):
131
+ return IndentationAndInputObject (indentation , Goto (int (line_content [1 :])))
132
+ else :
133
+ label , output = parse_label_and_output (line_content )
134
+ return IndentationAndInputObject (indentation , Output (output , label ))
135
+
136
+
137
+ def read_input ():
138
+ tree = []
139
+ answers = []
140
+ reading_examples = False
141
+ for line in sys .stdin .readlines ():
142
+ line = line .rstrip ()
143
+ if line == '---' :
144
+ reading_examples = True
145
+ elif not reading_examples :
146
+ tree .append (parse_line (line ))
147
+ else :
148
+ answers .append (line )
149
+ return tree , answers
150
+
151
+
152
+ flat_tree , user_answers = read_input ()
153
+ print_conversation (flat_tree , user_answers )
0 commit comments