All problems
Weak Reference Promotion
Problem
Simulate object lifetimes with strong references and weak reference checks.
operations contains "create" (register a new object with one strong reference), "ref" (add a strong reference when the object is alive), "release" (drop a strong reference and destroy the object at zero), "weak" (record whether a weak reference could be promoted, meaning the object is still alive), or "alive".
Return the list of recorded results (None for create, ref, and release).
Hint
A weak reference becomes invalid the moment the strong count reaches zero.
Test cases
case 1
in: [["create","weak","release","weak"],[1,1,1,1]]
expected: [null,true,null,false]
case 2
in: [["create","ref","release","alive"],[2,2,2,2]]
expected: [null,null,null,true]
case 3
in: [["weak"],[3]]
expected: [false]
case 4
in: [["create","create","release","alive"],[1,1,1,1]]
expected: [null,null,null,true]
Starter codePython
def weak_ref_sim(operations, values):
# Your code here
pass4 test cases · run your code against them in the editor