Consider the following sequence of expressions: animals = {'a': ['aardvark'), 'b': ['baboon'), 'c': ['coati']} animals['d'] = ['donkey'] animals['d'].append('dog') animals['d'].append('dingo')
We want to write some simple procedures that work on dictionaries to return information. This time, write a procedure, called biggest, which returns the key cor- responding to the entry with the largest number of values associated with it. If there is more than one such entry, return any one of the matching keys. Example usage: >>> biggest (animals) 'd' If there are no values in the dictionary, biggest should return None.

Respuesta :

Answer:

Here it the Python program:    

def biggest(Dict):  #function definition

   big = None      #biggest key is initialized to None

   for key in Dict.keys():  #iterates through the keys of dictionary

       if big is None or len(Dict[big]) < len(Dict[key]):  #if big is none OR if the length of the Dict at big value (index) is less than length of Dict at key (index key) value

               big = key   #sets the key value as largest number of values and stores the key value to big variable

   return big       #returns the biggest key stored in big variable

animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}  #dictionary with keys

animals['d'] = ['donkey']  #adds donkey to dictionary and d is index key

animals['d'].append('dog')  #appends dog as item to the existing list

animals['d'].append('dingo')  #appends dingo as item to the existing list

print(biggest(animals))  //calls biggest method by passing animals to it as parameter in order to return in animals,  the key corresponding to the entry with the largest number of values associated with it

   

Explanation:

You can also write this program as:

The program works as follows:

Lets say we have this dictionary with keys and corresponding values

animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}

for key in Dict.keys():

this will iterates through the keys of Dict. Here animals = Dict

big is set to None initially

Now the if big is None or len(Dict[big]) < len(Dict[key]):  checks

len(Dict[big]) < len(Dict[key]):

Its none for len(Dict[big]) and 1 for len(Dict[key])

So big = key  sets the value of key to big  

Lets say aardvark is the value at that key index. So the key corresponding to this value is a. Hence

big = a            

As the dictionary order keeps changing so if you want to see what is the value at the specified key then you can use this statement;

print(Dict[key])

This will print ['aardvark']    

So this is how this loop works.

Now for the lines                          

animals['d'] = ['donkey']

animals['d'].append('dog')  

animals['d'].append('dingo')

This will add three items in the dictionary with key 'd'

So when length of len(Dict[key])) is 3. So whenever this is the highest length and so whenever print(biggest(animals))  is called, it displays 'd' in output because  Dict[key] is ['donkey', 'dog', 'dingo'] which corresponds to key 'd'. So this key corresponds to the entry with the largest number of values associated with it.    

The program along with its output is attached.

Ver imagen mahamnasir

Procedures are collections of named code blocks that are executed when evoked.

The procedure named biggest written in Python, where comments are used to explain each line is as follows

#This defines the biggest function

def biggest(Animals):

   #This initializes biggest to none

   biggestAnimal = None

   #This iterates through the dictionary

   for key in Animals.keys():

       #This checks for the key with most values

       if biggestAnimal is None or len(Animals[biggestAnimal]) < len(Animals[key]):

           biggestAnimal = key

   #This returns the biggest animal

   return biggestAnimal

Read more about Procedures at:

https://brainly.com/question/24866381