All problems
Transpose Heuristic
Problem
Compute the total cost of transpose-heuristic accesses on a list.
items is the initial list order and accesses is the sequence of requested items, all present in items. Each access costs the item's current 1-based position, and when the item is not first it swaps with the preceding element.
Return the total access cost.
Hint
A requested item moves only one step toward the front.
Test cases
case 1
in: [[1,2,3],[3,2,3]]
expected: 9
case 2
in: [[1,2,3],[1,2,3]]
expected: 6
case 3
in: [[1,2],[2,2]]
expected: 3
case 4
in: [[1,2,3,4],[4,4,3,2]]
expected: 15
Starter codePython
def transpose_cost(items, accesses):
# Your code here
pass4 test cases · run your code against them in the editor