Исправь ошибки в коде :def dotexpr(expr: Expr) -> str… · The Chat Archive
Explore / Programming Исправь ошибки в коде :def dotexpr(expr: Expr) -> str: nodelist = [] def addnode(node:… Save Share Download ▾ ReportИсправь ошибки в коде :def dot_expr(expr: Expr) -> str:
node_list = []
code Copy code
def add_node(node: Expr) -> str:
nonlocal node_list
node_id = len(node_list)
if isinstance(node, Num):
node_list.append(f"n{node_id} [label=\"{node.value}\" shape=circle]")
elif isinstance(node, Add):
node_list.append(f"n{node_id} [label=\"+\" shape=circle]")
elif isinstance(node, Mul):
node_list.append(f"n{node_id} [label=\"*\" shape=circle]")
return f"n{node_id}"
def add_edge(start: Expr, end: Expr) -> str:
start_id = node_list.index(add_node(start))
end_id = node_list.index(add_node(end))
return f"n{start_id} -> n{end_id}"
def traverse(curr: Expr):
nonlocal node_list
if isinstance(curr, Num):
add_node(curr)
elif isinstance(curr, Add):
add_node(curr)
traverse(curr.left)
traverse(curr.right)
node_list.append(add_edge(curr.left, curr))
node_list.append(add_edge(curr.right, curr))
node_list.append(add_edge(curr, curr.left))
node_list.append(add_edge(curr, curr.right))
elif isinstance(curr, Mul):
add_node(curr)
traverse(curr.left)
traverse(curr.right)
node_list.append(add_edge(curr.left, curr))
node_list.append(add_edge(curr.right, curr))
node_list.append(add_edge(curr, curr.left))
node_list.append(add_edge(curr, curr.right))
traverse(expr)
return "digraph G {\n" + "\n".join(node_list) + "\n}"
Пример использования
add_expr = Add(Num(7), Mul(Num(3), Num(2)))
result = dot_expr(add_expr)
print(result)
digraph G {
Show full message · 72 lines Assistant2023-05-11 07:24 UTC Copy response
def dot_expr(expr: Expr) -> str:
node_list = []
Copy first prompt Copy first response
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date May 11, 2023
Added to archive Nov 16, 2026
ID 01m7h419m8jcvyxvggrc7t4bwdSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: ceb0148a691a11884da0a8940efcd189
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
n0 [label="7" shape=circle]
n1 [label="2" shape=circle]
n2 [label="3" shape=circle]
n3 [label="*" shape=circle]
n4 [label="+" shape=circle]
n0 -> n4
n1 -> n3
n2 -> n3
n3 -> n4
n4 -> n0
n4 -> n3
} (Список ошибок , которые ты дожен исправить : Traceback (most recent call last): File "C:\Users\bel-i\PycharmProjects\pythonProject7\123.py", line 103, in
result = dot_expr(add_expr)
File "C:\Users\bel-i\PycharmProjects\pythonProject7\123.py", line 97, in dot_expr
traverse(expr)
File "C:\Users\bel-i\PycharmProjects\pythonProject7\123.py", line 83, in traverse
traverse(curr.right)
File "C:\Users\bel-i\PycharmProjects\pythonProject7\123.py", line 92, in traverse
node_list.append(add_edge(curr.left, curr))
File "C:\Users\bel-i\PycharmProjects\pythonProject7\123.py", line 72, in add_edge
start_id = node_list.index(add_node(start))
ValueError: 'n5' is not in list)
def add_node(node: Expr) -> str:
nonlocal node_list
node_id = len(node_list)
if isinstance(node, Num):
node_list.append(f"n{node_id} [label=\"{node.value}\" shape=circle];")
elif isinstance(node, Add):
node_list.append(f"n{node_id} [label=\"+\" shape=circle];")
elif isinstance(node, Mul):
node_list.append(f"n{node_id} [label=\"*\" shape=circle];")
return f"n{node_id}"
def add_edge(start: Expr, end: Expr) -> str:
nonlocal node_list
start_id = node_list.index(add_node(start))
end_id = node_list.index(add_node(end))
return f"n{start_id} -> n{end_id};"
def traverse(curr: Expr):
nonlocal node_list
if isinstance(curr, Num):
add_node(curr)
elif isinstance(curr, Add):
node = add_node(curr)
traverse(curr.left)
traverse(curr.right)
node_list.append(add_edge(curr.left, node))
node_list.append(add_edge(curr.right, node))
elif isinstance(curr, Mul):
node = add_node(curr)
traverse(curr.left)
traverse(curr.right)
node_list.append(add_edge(curr.left, node))
node_list.append(add_edge(curr.right, node))
traverse(expr)
return "digraph G {\n" + "\n".join(node_list) + "\n}"
Пример использования add_expr = Add(Num(7), Mul(Num(3), Num(2)))
result = dot_expr(add_expr)
print(result)
digraph G {
n0 [label="7" shape=circle];
n1 [label="2" shape=circle];
n2 [label="3" shape=circle];
n3 [label="*" shape=circle];
n4 [label="+" shape=circle];
n0 -> n4;
n1 -> n3;
n2 -> n3;
n3 -> n4;
n4 -> n0;
n4 -> n3;
} Not yet included in a published release.