Question: I am using PYTHON to solve this problem. Currently, I learned if-else statements, for and while loops, def, and list, and I can only use

 I am using "PYTHON" to solve this problem. Currently, I learnedif-else statements, for and while loops, def, and list, and I canonly use these. This is the code that I wrote, please fix:

I am using "PYTHON" to solve this problem. Currently, I learned if-else statements, for and while loops, def, and list, and I can only use these.

This is the code that I wrote, please fix:

C = int(input()) list_int = list(map(int, input().split(","))) list_val = list(map(str, input().split(","))) list_new = [] list_check = []

if len(list_int) != len(list_val): print("Warning: number of keys and values are not the same") elif C != -1 and C

def get(key): found = False for i in range(len(list_new)): if list_new[i][0] == key: found = True val = list_new[i][1] #list_new.pop(i) #list_new.append([key, val]) for j in range(len(list_check)): if list_check[j][0] == key: val_check = list_check[j][1] list_check.pop(j) list_check.append([key, val_check]) j = len(list_check)-1 print(val) break if not found: print('NULL')

def put(key, value): list_temp = [key, value] found = False found_check = False if C == -1: for i in range(len(list_check)): if list_check[i][0] == key: pop_check = list_check.pop(list_check[i][0]) #Not sure list_check.insert(i, pop_check) else: list_check.append(list_temp) for i in range(len(list_new)): if list_new[i][0] == key: pop_new = list_new.pop(list_new[i][0]) #Not sure list_new.insert(i, pop_new) else: list_new.append(list_temp) else: for i in range(len(list_check)): if list_check[i][0] == key: val_check = list_check[i][0] list_check.pop(i) list_check.insert(i, [key, val_check]) for j in range(len(list_new)): if list_new[j][0] == val_check: val_new = list_new.pop(j) list_new.insert(j, val_new) else: if len(list_new) >= C: val_check_removed = list_check.pop(0) for i in range(len(list_new)): if list_new[i][0] == val_check_removed: val_new2 = list_new.pop(i) list_new.insert(i, val_new2) list_check.append(list_temp) list_new.append(list_temp)

while True: point = input().strip() if point[0] == 'g': A, B = map(str, point.split(",")) get(int(B)) elif point[0] == 'p': c, d, e = map(str, point.split(",")) put(int(d), e) elif point[0] == 'e': break

print(list_new)

After passing all the tests in Level 2, the interviewers proceed to ask a follow-up question to truly test your ability in programming and problem solving. They tell you that it is computationally expensive to move the most recently used item to the end of the list in order to conform to the description in the Introduction, i.e., the 1st (leftmost) item is the least recently used while the last (rightmost) item is the most recently used. It is because all the subsequent items need to be moved to the left by 1 . For example, the steps of moving 2 in [1,2,3,4] to the end of the list is [1,2,3,4][1,3,3,4][1,3,4,4][1,3,4,2]. As a result, moving item to the end of a list should be avoided if possible. The follow-up question is whether you can implement a solution without the need of moving items in the cache once they are inserted. Specifically, when accessing an item, NO movement should be made to any item but you are allowed to modify the item. When adding a new item to the list, it should be added to the end of the list if the cache is not full. Otherwise, the least recently used item is REPLACED by the new item and NO movement of other items are allowed. For example, given a cache of capacity 4:[[1,100],[2,200],[3,300]], the cache contents will be updated as follows. 1. get(2)[[1,100],[2,200],[3,300]] 2. put(4,,400)[[1,100],[2,200],[3,300],[4,400]] 3. get(1)[[1,100],[2,200],[3,300],[4,400]] 4. put(5,500)[[1,100],[2,200],[5,500],[4,400]] As a result, there is NO limitation on how you are going to store the data. However, ONLY one list can be created and used for the cache and no other auxiliary list is allowed. Note: The test cases on VPL will be time limited. If you see timeout on VPL, it means your program is too slow and need further optimization. Nested loops are usually the most expensive operations and it is a good starting point for optimization. Inputs: 1. Capacity of the cache C (integer). C=1 means that the cache has unlimited capacity. 2. A string of integral keys K1,K2,,KNk separated by "," where Nk is number of keys. 3. A string of values V1,V2,,VNv separated by "," where Nv is number of values. 4. A series of commands consisting of either "get, KEY" or "put, KEY,VALUE" separated by a newline character. 5. The input must be ended with "end". Outputs: 1. Warning message if Nk=Nv. 2. For every get operation, print the value of the key-value pair if the key exists, otherwise print "NULL". 3. Cache as a list with format [[K1,V1],[K2,V2],[Kn,Vn]] where the ordering is no longer based on recent usage but follows the specs listed in the question. Assumptions: - 1C10000 or C=1 - 1Nk,Nv10000 - Input keys can be converted to integers - Input keys are unique, no duplicates - Input values are non-null string - None of the values will be equal to "NULL" - Time constraint will be imposed on VPL and timeout is possible Hints: - You may want to change the structure of how you keep the data. For example, you can store extra information alongside with each data item if you find it useful. - It is acceptable if put and/or other operations take more time to run as long as the total time is within the time constraint. Your program should be well within the time constraint unless it has multiple layers of loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!