f | import sys | f | import sys |
| import ast | | import ast |
| | | |
n | class X(ast.NodeTransformer): | n | class FloorDivTransformer(ast.NodeTransformer): |
| | | |
t | def visit_BinOp(self, n): | t | def visit_BinOp(self, node): |
| self.generic_visit(n) | | self.generic_visit(node) |
| if isinstance(n.op, ast.FloorDiv): | | if isinstance(node.op, ast.FloorDiv): |
| return ast.copy_location(ast.Call(func=ast.Name(id='y', ctx= | | return ast.copy_location(ast.Call(func=ast.Name(id='floor_di |
| ast.Load()), args=[n.left, n.right], keywords=[]), n) | | v', ctx=ast.Load()), args=[node.left, node.right], keywords=[]), node) |
| return n | | return node |
| i = sys.stdin.read() | | input_code = sys.stdin.read() |
| t = ast.parse(i) | | tree = ast.parse(input_code) |
| x = X() | | transformer = FloorDivTransformer() |
| t = x.visit(t) | | tree = transformer.visit(tree) |
| ast.fix_missing_locations(t) | | ast.fix_missing_locations(tree) |
| y_code = '\ndef y(a, b):\n if isinstance(a, int):\n return a / | | floor_div_code = '\ndef floor_div(a, b):\n if isinstance(a, int):\n |
| / b\n elif isinstance(a, (list, tuple, str)):\n return a[: len | | return a // b\n elif isinstance(a, (list, tuple, str)):\n |
| (a) // b]\n' | | return a[: len(a) // b]\n else:\n raise TypeError("Unsupport |
| | | ed type for floor_div")\n' |
| y_ast = ast.parse(y_code) | | floor_div_ast = ast.parse(floor_div_code) |
| t.body = y_ast.body + t.body | | tree.body = floor_div_ast.body + tree.body |
| exec(compile(t, filename='<ast>', mode='exec')) | | exec(compile(tree, filename='<ast>', mode='exec')) |