Generation

generate functionThu, 15 Dec 2022

Create a function that allows you to move doc to another shelf directories. It should check for: 1) if both doc and shelf exist in the dictionary, the function should move doc value to the specified shelf and make return updated directories. 2) if the specified shelf exists in the directories and return ERROR NO SUCH KEY if it doesn't exist 3) if there is a specified doc in the dictionary and return ERROR NO SUCH VALUE if it doesn't exist Note that directories is a dictionary and doc values are in lists! Example: Input: directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [] } doc = '11-2' shelf = '3' Output: directories = { '1': ['2207 876234'], '2': ['10006'], '3': ['11-2'] }

def move(directories, doc, shelf): if doc in directories[shelf]: return "ERROR VALUE ALREADY EXISTS" elif doc not in directories: return "ERROR NO SUCH VALUE" elif shelf not in directories: return "ERROR NO SUCH KEY" else: directories[shelf].append(doc) for shelf in directories: if doc in directories[shelf]: del directories[shelf] return directories directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [] } doc = '11-2' shelf = '3' move(directories, doc, shelf)

Questions about programming?Chat with your personal AI assistant