Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_containing_state(states):
- """Returns a function that takes a tweet and returns the name of the state
- containing the given tweet's location.
- Use the geo_distance function (already provided) to calculate distance
- in miles between two latitude-longitude positions.
- Arguments:
- tweet -- a tweet abstract data type
- us_states -- a dictionary from state names to positions.
- >>> sf = Tweet("Welcome to San Francisco", None, 38, -122)
- >>> ny = Tweet("Welcome to New York", None, 41.1, -74)
- >>> find_state = find_containing_state(us_states)
- >>> find_state(sf)
- 'CA'
- >>> find_state(ny)
- 'NY'
- """
- def find_state(tweet):
- for state in states:
- polygon = states[state]
- collisions = 0
- for i in range(len(polygon)-1):
- point_a = min(polygon[i], polygon[i+1],
- key=lambda position: position.latitude())
- point_b = max(polygon[i], polygon[i+1],
- key=lambda position: position.latitude())
- if not point_a.latitude() <= tweet.lat < point_b:
- continue
- slope = (point_a.longitude() - point_b.longitude()) /\
- (point_a.latitude() - point_b.longitude())
- y = slope*(tweet.lat - point_a.latitude()) +\
- point_a.longitude()
- is_collision = y > tweet.lon
- collisions += is_collision
- if collisions%2 == INSIDE_MODULU:
- return state
- return None
- return find_state
Advertisement
Add Comment
Please, Sign In to add comment