← 返回 meta 的题目列表Compiler Optimization Cost Estimator (Time and Memory)
类型:online_judge
Problem: Compiler Optimization Cost Estimator (Time and Memory)
You are given an instruction file (e.g., test/instruction1.txt). Each line contains a three-address-style assignment. Implement extract_time_and_mem_cost(instruction_path) to read the file and compute the program's time cost and memory cost (or a single required cost metric, depending on the test harness).
Example instruction files:
instruction1.txt
res1 = var1 + var2
res2 = var3 - var4
res3 = res2 + var5
res = res1 + res3
instruction2.txt
res1 = var1 * var2
res2 = var3 - var4
res3 = res2 / var5
res = res1 + res3
instruction3.txt
res1 = 10 * var2
res2 = var3 * 100 - var4
res3 = res1 / 2
res = res2 + res3
Using the cost rules provided in the prompt (e.g., different operators having different time/memory costs, temporary storage usage, etc.), compute and return the required cost so that all unit tests pass (including hidden tests like test4–test7).
Requirements
Parse each assignment statement and identify destination variable, operands, and operators.
Handle constants and variables (e.g., 10 * var2).
Handle expressions with multiple operators in one line (e.g., var3 * 100 - var4) following the specified precedence/accounting rules.
Output must match the hidden tests.
I/O
Input: instruction_path: str (path to the instruction file).
Output: return the cost as required by the tests (commonly (time, mem) as two integers, or a single aggregated value).
Sample Tests (form)
Because the original post does not include the full rule set or expected values, only the test shape is known:
extract_time_and_mem_cost('test/instruction1.txt') equals the expected assertion value.
Same for instruction2.txt, instruction3.txt.
Note: The post indicates the operator costs may not be as simple as +/-/= => 1 and *// => 5, and hidden tests likely target more complex expressions and rule nuances.