Last active
September 12, 2016 18:39
-
-
Save jtallieu/083acd01ce9e46a3258860f159d74657 to your computer and use it in GitHub Desktop.
Eagle Eye Networks Inc. :: Python screening sample - Optimize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
::Eagle Eye Networks Inc.::: | |
::Python screening:: | |
The following script is purposley written poorly. | |
The City class depends on an external service that is really | |
slow. Improve the code below so that 'test1' is more performant | |
but preserves: | |
- the ability to get a City as a dict or string. | |
- the City class interface. | |
Hint: You should get below 10s | |
""" | |
""" | |
!!!YOU CANNOT TOUCH THIS PART OF THE SAMPLE!!! | |
Imagine this as an external service that provides | |
a geolocation hash for an address/business/city/etc | |
""" | |
import time | |
class GeoService(object): | |
def location_hash(self, location): | |
time.sleep(5) | |
return str(abs(hash(location))) | |
""" | |
End of the external service definition | |
""" | |
""" | |
You are free to modify the code below | |
""" | |
import cProfile | |
class City(object): | |
def __init__(self, name): | |
self.__geoservice = GeoService() | |
self.name = name | |
@property | |
def location(self): | |
return self.__geoservice.location_hash(self.name) | |
def city_to_dict(city): | |
return {'name': city.name, 'location': city.location} | |
def city_str(city): | |
return "%s is @ %s" % (city.name, city.location) | |
def test1(): | |
austin = City("Austin") | |
print city_to_dict(austin) | |
print city_str(austin) | |
if __name__ == "__main__": | |
cProfile.run("test1()") |
Author
jtallieu
commented
Sep 9, 2016
misspelled "purposley"
other than that, 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment