Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement_deliver_mail and_get_mail Mutation is crucial for ADTs. Recall the bank account example given in lecture. The bank account ADT can be deposited to and withdrawn
Implement_deliver_mail and_get_mail Mutation is crucial for ADTs. Recall the bank account example given in lecture. The bank account ADT can be deposited to and withdrawn from. When we deposit or withdraw, we want to change the state of the bank account so that the withdrawal or deposit will be reflected next time we access it. Below, we can see the power of mutability which allows us to change the name and amount of money in account_1 def mailbox) account_1 account("Jessica", 10) get-mail, deliver-mail mailbox() >>> >>get account_name(account 1) Jessica >change_ account_name(account 1, "Ting") >get mail("Amir") deliver_mail("Amir", ["postcard"]) >get_mail("Amir") ['postcard'] accoun t_name(account_1) Ting get_mail("Amir") deliver maiL("Ting", ["paycheck", "ads"]) get_mail("Ting") deposit(account 1, 20) >get_account_savings(account 1) 30 >>>withdraw(account 1, 10) >get account_savings (account 1) 20 ['paycheck 'ads' deliver mail("Ting", ["bills"]) >get_mail("Ting") deliver_mail("Alex", ["survey"]) get_mail("Alex") During the mini-lecture, we introduced the new concept of a constructor returning the very functions we want to run on the object! This kind of abstraction is very common in the programming world. ['survey' get_mail("Alex") Let's apply this concept to a new ADT a mallbox. The mail box's internal representation is a dictionary that holds key value pairs corresponding to a person, and a list of their mail. Below is an example of a TA mailbox >>get_mail("John") deliver mail("John", ["postcard", "paycheck"1) deliver mail("John", "ads") get_mail("John") f"Jessica": ["receipt", "worksheets", "Alex": ["worksheets", "form", "bills"] ['postcard', 'paycheck', "ads'] The_get_mail(mailbox, name) function, given a mailbox, should return the mail corresponding to the name argument given. The postman can also put in mail by using the _deliver mail(mailbox, name, mail) function. You may have noticed that the function names are a bit strange: they each have an underscore in the beginning. We use this kind of syntax when a function is "hidden", meaning they are hidden to the outside. Remember, our goal is for the mailbox constructor to return all functions that the mailbox uses. YOUR CODE HERE return get mail, deliver_mail def deliver_mail(mailbox, name, mail): YOUR CODE HERE Please provide implementations for these functions. Make sure to mutate the dictionary! (You can assume mail will always be in a list.) def_get mail(mailbox, name): After doing that, implement the constructor so that it returns get mail and deliver mail such that when called, they will perform the function on the original mailbox. Hint use_get_mail and deliver mail YOUR CODE HERE Implement_deliver_mail and_get_mail Mutation is crucial for ADTs. Recall the bank account example given in lecture. The bank account ADT can be deposited to and withdrawn from. When we deposit or withdraw, we want to change the state of the bank account so that the withdrawal or deposit will be reflected next time we access it. Below, we can see the power of mutability which allows us to change the name and amount of money in account_1 def mailbox) account_1 account("Jessica", 10) get-mail, deliver-mail mailbox() >>> >>get account_name(account 1) Jessica >change_ account_name(account 1, "Ting") >get mail("Amir") deliver_mail("Amir", ["postcard"]) >get_mail("Amir") ['postcard'] accoun t_name(account_1) Ting get_mail("Amir") deliver maiL("Ting", ["paycheck", "ads"]) get_mail("Ting") deposit(account 1, 20) >get_account_savings(account 1) 30 >>>withdraw(account 1, 10) >get account_savings (account 1) 20 ['paycheck 'ads' deliver mail("Ting", ["bills"]) >get_mail("Ting") deliver_mail("Alex", ["survey"]) get_mail("Alex") During the mini-lecture, we introduced the new concept of a constructor returning the very functions we want to run on the object! This kind of abstraction is very common in the programming world. ['survey' get_mail("Alex") Let's apply this concept to a new ADT a mallbox. The mail box's internal representation is a dictionary that holds key value pairs corresponding to a person, and a list of their mail. Below is an example of a TA mailbox >>get_mail("John") deliver mail("John", ["postcard", "paycheck"1) deliver mail("John", "ads") get_mail("John") f"Jessica": ["receipt", "worksheets", "Alex": ["worksheets", "form", "bills"] ['postcard', 'paycheck', "ads'] The_get_mail(mailbox, name) function, given a mailbox, should return the mail corresponding to the name argument given. The postman can also put in mail by using the _deliver mail(mailbox, name, mail) function. You may have noticed that the function names are a bit strange: they each have an underscore in the beginning. We use this kind of syntax when a function is "hidden", meaning they are hidden to the outside. Remember, our goal is for the mailbox constructor to return all functions that the mailbox uses. YOUR CODE HERE return get mail, deliver_mail def deliver_mail(mailbox, name, mail): YOUR CODE HERE Please provide implementations for these functions. Make sure to mutate the dictionary! (You can assume mail will always be in a list.) def_get mail(mailbox, name): After doing that, implement the constructor so that it returns get mail and deliver mail such that when called, they will perform the function on the original mailbox. Hint use_get_mail and deliver mail YOUR CODE HERE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started