Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python generator def min_key_order(adict): # Test min_key_order c-->d = {1:'a', 2:'x', 4:'m', 8:'d', 16:'f'} c-->i = min_key_order(d) ==-->[next(i), next(i), d.pop(8), next(i), d.setdefault(32,'z'), next(i), next(i)]-->[(1, 'a'),

Python generator

image text in transcribed

def min_key_order(adict):

# Test min_key_order c-->d = {1:'a', 2:'x', 4:'m', 8:'d', 16:'f'} c-->i = min_key_order(d) ==-->[next(i), next(i), d.pop(8), next(i), d.setdefault(32,'z'), next(i), next(i)]-->[(1, 'a'), (2, 'x'), 'd', (4, 'm'), 'z', (16, 'f'), (32, 'z')] ==-->d-->{1:'a', 2:'x', 4:'m', 16:'f', 32:'z'} c-->d = {1:'a', 2:'x', 4:'m', 8:'d', 16:'f'} c-->i = min_key_order(d) ==-->[next(i), next(i), next(i), d.setdefault(3,'n'), d.setdefault(10,'o'), d.setdefault(32,'z'), next(i), next(i), next(i), next(i)]-->[(1, 'a'), (2, 'x'), (4, 'm'), 'n', 'o', 'z', (8, 'd'), (10, 'o'), (16, 'f'), (32, 'z')] ==-->d-->{1:'a', 2:'x', 3:'n', 4:'m', 8:'d', 10:'o', 16:'f', 32:'z'} c-->d = {1:'a', 2:'x', 4:'m', 8:'d', 16:'f'} c-->i = min_key_order(d) ==-->[next(i), d.pop(2), d.pop(4), d.pop(8), next(i)]-->[(1, 'a'), 'x', 'm', 'd', (16, 'f')] ==-->d-->{1:'a', 16:'f'} c-->d = {1:'a', 2:'x', 4:'m', 8:'d', 16:'f'} c-->i = min_key_order(d) ==-->[next(i), d.pop(2), next(i), d.pop(4), d.pop(8), next(i)]-->[(1, 'a'), 'x', (4, 'm'), 'm', 'd', (16, 'f')] ==-->d-->{1:'a', 16:'f'} c-->d = {} c-->i = min_key_order(d) ^-->next(i)-->StopIteration

2. (5 pts) Define a generator named nin key_order whose parameter is a dict that has keys that can all be compared with each other (e.g., all numbers, all strings, etc; don't check this property). The min key order generator produces a 2-tuple of the dict's keys and values, in increasing order of the keys. For example: for i in nin-key-order ({1:'a', 2:'x', 4: 'n', 8: 'd', 16:'f'}): print (i,end-') prints: (1, 'a') (2,'x') (4, 'n') (8, 'd') (16, 'f') It would be trivial to write this generator as def nin_key_order (adict): for k,v in sorted (adict.itens()) : # iterate over a sorted list of keys/values yield (k,v) but the generator must have one more property: it must iterate over mutations made to the dict while the dict is being iterated over. That is Prints: i-min keys-order (d) print (next(i)) print (next(i)) del d[8] print (next(i)) d[16] = 'f' print (nezt(i)) print(next (i)) Note that the process of iterating through the dict should not mutate the dict (although the dict can be mutated directly by code during the iteration, as is shown above), Hints: The first time next is called, find the minimum key in the dict it is unique, if the dict isn't empty) and yield it and the key and its value first; later, repeatedly find the smallest key bigger than the previously yielded key (so long as there is one) and yield the key and its value; you may create a local list/tuple whose length is no bigger than the dict (although there is a way to solve this problem without such a data structure, but it requires more code) 2. (5 pts) Define a generator named nin key_order whose parameter is a dict that has keys that can all be compared with each other (e.g., all numbers, all strings, etc; don't check this property). The min key order generator produces a 2-tuple of the dict's keys and values, in increasing order of the keys. For example: for i in nin-key-order ({1:'a', 2:'x', 4: 'n', 8: 'd', 16:'f'}): print (i,end-') prints: (1, 'a') (2,'x') (4, 'n') (8, 'd') (16, 'f') It would be trivial to write this generator as def nin_key_order (adict): for k,v in sorted (adict.itens()) : # iterate over a sorted list of keys/values yield (k,v) but the generator must have one more property: it must iterate over mutations made to the dict while the dict is being iterated over. That is Prints: i-min keys-order (d) print (next(i)) print (next(i)) del d[8] print (next(i)) d[16] = 'f' print (nezt(i)) print(next (i)) Note that the process of iterating through the dict should not mutate the dict (although the dict can be mutated directly by code during the iteration, as is shown above), Hints: The first time next is called, find the minimum key in the dict it is unique, if the dict isn't empty) and yield it and the key and its value first; later, repeatedly find the smallest key bigger than the previously yielded key (so long as there is one) and yield the key and its value; you may create a local list/tuple whose length is no bigger than the dict (although there is a way to solve this problem without such a data structure, but it requires more code)

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

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

Define and discuss the process of planned change.

Answered: 1 week ago