Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use python to implement mailbox() along with _get_mail and _deliver_mail so that the functions meets requirements on the second pic. Mutation is crucial for ADTs.
Use python to implement mailbox() along with _get_mail and _deliver_mail so that the functions meets requirements on the second pic.
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. >>>account_1account("Jessica", 10) >get_account_name (account_1) Jessica >>>change_account_name(account_1, "Ting") >>>get_account_name (account_1) Ting >>deposit(account_1, 20) >get_account_savings (account_1) 30 withdraw(account_1, 10) >get_account_savings(account 1) 20 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 Let's apply this concept to a new ADT: a mailbox. 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 {"Jessica": ["receipt", "worksheets"], "Alex": ["worksheets", "form", "bills"], "Andrew": ["paycheck"], 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 Please provide implementations for these functions. Make sure to mutate the dictionary! (You can assume mail will always be in a list.) 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 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. >>>account_1account("Jessica", 10) >get_account_name (account_1) Jessica >>>change_account_name(account_1, "Ting") >>>get_account_name (account_1) Ting >>deposit(account_1, 20) >get_account_savings (account_1) 30 withdraw(account_1, 10) >get_account_savings(account 1) 20 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 Let's apply this concept to a new ADT: a mailbox. 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 {"Jessica": ["receipt", "worksheets"], "Alex": ["worksheets", "form", "bills"], "Andrew": ["paycheck"], 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 Please provide implementations for these functions. Make sure to mutate the dictionary! (You can assume mail will always be in a list.) 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
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