All problems
Write-Through vs Write-Back Policy Simulation
Problem
Count the memory writes performed under two cache policies.
policy is either "through" or "back". operations contains "write" (dirty the block in addresses[i]) or "flush" (write all dirty blocks to memory and clear them). A write-through cache writes to memory on every write, while a write-back cache writes only on flush.
Return the total number of memory writes.
Hint
A set of dirty block addresses is enough; repeating a write to the same block stays one dirty block.
Test cases
case 1
in: ["through",["write","write","flush"],[1,2,0]]
expected: 2
case 2
in: ["back",["write","write","flush"],[1,2,0]]
expected: 2
case 3
in: ["back",["write","write","write"],[1,1,2]]
expected: 0
case 4
in: ["back",["write","flush","write","flush"],[1,0,1,0]]
expected: 2
case 5
in: ["through",["flush"],[0]]
expected: 0
Starter codePython
def memory_write_count(policy, operations, addresses):
# Your code here
pass5 test cases · run your code against them in the editor