Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python please don't create other definitions and try to avoid importing anything closest (d, what, here): Consider a dictionary that has pairs of integers

In Python

please don't create other definitions and try to avoid importing anything

image text in transcribed

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 >> d= {(3,1): "gas", (1,4): "gas", (2,1): 'food', (55),'food') >>> closest (d, "gas", (2,2)) #closest thing isn't gas. >>> closest(d, "gas" , (5,5)) >> closest(d, "food", (5,5)) (5, 5) >> closest(d, "food", (1,4)) >>> print (closest(d, "hotel", (1,4))) None

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions