Question
This must be in Python and use dictionaries!!! Read everything carefully!!! Dictionaries, File I/O Reminder: you can run individual inputs, such as counts(mississippi), by loading
This must be in Python and use dictionaries!!! Read everything carefully!!!
Dictionaries, File I/O
Reminder: you can run individual inputs, such as counts("mississippi"), by loading your file into interactive mode with the i flag. You should be using this technique on projects as well.
demo$ python3 i gmason_2B5_L9.py
>>> counts("mississippi")
{'m': 1, 'p': 2, 'i': 4, 's': 4}
Dictionaries serve a special niche of problem type. Being able to pair up "keys" with "values", and yet disallowing duplicate entries with matching keys, helps us organize data by the exact keys we want to use to initiate searching and inspecting our information.
What can I use?
There are no restrictions on what functions to use on this lab use this time to learn how to build, navigate, and modify dictionaries, and minimally how to read a file.
weeklies(plants_d): Consider a dictionary, plants_d, where the keys are names of plants, and the values are descriptions of how often to water them. Search through the entire structure for all plants that need to be watered "weekly", put them into a list, sort() the list, and return it.
plants_d :: dictionary of plant names to watering instructions.
Return value: sorted list of plants that need watering "weekly".
>>> weeklies({'shamrock':'weekly', 'cactus':'monthly', 'rose':'weekly', 'succulent':'biweekly'}) ['rose', 'shamrock']
>>> weeklies({'fern':'weekly', 'shamrock':'weekly', 'carnation':'weekly'}) ['carnation', 'fern', 'shamrock']
closest(d, what, here): Consider a dictionary that has pairs of integers as the keys, to represent a spot on a 2D grid (like pixels on a screen or integer points on the real numbers plane). The associated values will be strings, describing something that is present at that location. Using the distance formula between two points (see Wikipedia for a refresher if needed), complete the closest function that looks through the data for the spot closest to here that contains a what. For instance, "I'm at (2,3). Where's the closest gas station?" could be phrased as closest(d, "gas station", (2,3)). Don't worry about ties; anything equally closest will do (but we'll test with no ties to keep this short).
Parameters:
o d :: dictionary where keys are pairs of integers and values are strings.
o what :: string describing a point of interest.
o here :: tuple of coordinates of where we currently are.
Returns: a pair of integers of the closest what to here. When no what is found, return the None value. (If we had learned about exceptions it would have been a great time for one here!)
Examples:
o >>> d = {(3,1):'gas', (1,4):'gas', (2,1):'food', (5,5):'food'} >>> closest(d,"gas",(2,2)) #closest thing isn't gas. (3, 1) >>> closest(d,"gas",(5,5)) (1, 4) >>> closest(d,"food",(5,5)) (5, 5) >>> closest(d,"food",(1,4)) (2, 1) >>> print(closest(d,"hotel",(1,4))) None
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