Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use PYTHON to solve this question. Currently, I learned if-else statements, for and while loops, def, and list, and I can only use these.

image text in transcribedimage text in transcribedimage text in transcribed

Please use "PYTHON" to solve this question.

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:

# 1. Keys must be stored as integers # 2. Values must be stored as strings # 3. If the numbers of keys and values are different, # prints "Warning: number of keys and values are not the same" and outputs an empty cache

#What I need to do: #1. Make definite size of list #2. Make sure changed values are printed out when get()

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

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

else: if C != -1 and C

else: for i in range(len(list_int)): list_new.append([list_int[i], list_val[i]])

# get done def get(key): for i in range(len(list_new)): if list_int[i] == key: print(list_val[i]) a = list_new.pop(i) b = list_val.pop(i) D = list_int.pop(i) list_val.append(b) list_int.append(D) list_new.append(a) break if list_int[i] != key and i == len(list_new)-1: print('NULL') if len(list_new) == 0: print('NULL')

def put(key, value): list_temp = [key, value] if len(list_new) == 0: list_new.append(list_temp)

#size = 1 else: for i in range(0, len(list_new)): if list_new[i][0] == key: list_new.remove(list_new[i]) list_new.append(list_temp) break else: if C != -1 and len(list_new)

while C > -2: point = input() if point[0] == 'g': A, B = map(str, point.split(",")) get(int(B)) if point[0] == 'p': c, d, e = map(str, point.split(",")) put(int(d), e) if point[0] == 'e': C -= 9999

print(list_new)

Next, you are going to implement two main functions, get(key) and put(key, value) to access a data item in the cache. The get(key) function retrieves the value of the data item given by the key parameter if the key exists in the cache. The put(key, value) function modifies the value of the data item, if the key exists, or inserts a new data item, if the key does not exist. The ordering of data items in the cache must 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. Every time a data item is accessed, whether using get or put, the data item has to be moved to the end (rightmost) of the cache to become the most recently used item. When a new data item is inserted to a full cache, the least recently used (leftmost) item needs to be evicted. For example, given a cache of capacity 5: [[1,'100'], [2,'200'], [3,'300'], [4,'400']], the cache contents will be updated as follows. 1. get(3)[[1,100],[2,200],[4,4000],[3,300]] 2. put(2,6)[[1,100],[4,400],[3,300],[2,6]] 3. put(10)1)[[1,,100],[4,400],[3,300],[2,,6],[10,,1]] 4. put(100,5)[[4,400],[3,300],[2,6],[10,1],[100,5]] In addition to the inputs you received from Level 1 , you will also receive a list of commands. "get, KEY" denotes the get operation with key KEY and "put, KEY, VALUE" denotes the put operation with key KEY and value VALUE. Get operation prints the value of the keyvalue pair if the key exists, otherwise prints "NULL". Put operation does not need to print anything. The command "end" denotes the end of the inputs and you should then print the final contents of the cache. 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]] with the 1st (leftmost) item being the least recently used while the last (rightmost) item being the most recently used. Assumptions: - 1C1000 or C=1 - 1Nk,Nv1000 - 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" \begin{tabular}{|l|l|l|} \hline put,6,600 & \\ put, 1,10000 \\ put, 7,700 & \\ get, 3 & \\ get, 2 & & \\ & end & \\ \hline \end{tabular}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions