Q1 is no coding, deals with retrieving vals by index - WWPD (What Would Python Display?)
Q2: Couple
Implement the function couple, which takes in two lists and returns a list that contains lists with i-th elements of two sequences coupled together. You can assume the lengths of two sequences are the same. Try using a list comprehension.
defcouple(s1,s2):"""Return a list that contains lists with i-th elements of two sequences coupled together.>>> s1 = [1, 2, 3]>>> s2 = [4, 5, 6]>>> couple(s1, s2) [[1, 4], [2, 5], [3, 6]]>>> s3 = ['c', 6]>>> s4 = ['s', '1']>>> couple(s3, s4) [['c', 's'], [6, '1']] """assertlen(s1)==len(s2)"*** YOUR CODE HERE ***"
Q2: Solution
defcouple(s1,s2):"""Return a list that contains lists with i-th elements of two sequences coupled together.>>> s1 = [1, 2, 3]>>> s2 = [4, 5, 6]>>> couple(s1, s2) [[1, 4], [2, 5], [3, 6]]>>> s3 = ['c', 6]>>> s4 = ['s', '1']>>> couple(s3, s4) [['c', 's'], [6, '1']] """assertlen(s1)==len(s2)"*** YOUR CODE HERE ***"return [[x, y] for x, y inzip(s1, s2)]
Q3: Enumerate
Implement enumerate, which pairs the elements of a sequence with their indices, offset by a starting value. enumerate takes a sequence s and a starting value start. It returns a list of pairs, in whichthe i-th element is i + start paired with the i-th element of s. For example:
defenumerate(s,start=0):"""Returns a list of lists, where the i-th list contains i+start and the i-th element of s.>>> enumerate([6, 1, 'a']) [[0, 6], [1, 1], [2, 'a']]>>> enumerate('five', 5) [[5, 'f'], [6, 'i'], [7, 'v'], [8, 'e']] """"*** YOUR CODE HERE ***"
Q3: Solution
defenumerate(s,start=0):"""Returns a list of lists, where the i-th list contains i+start and the i-th element of s.>>> enumerate([6, 1, 'a']) [[0, 6], [1, 1], [2, 'a']]>>> enumerate('five', 5) [[5, 'f'], [6, 'i'], [7, 'v'], [8, 'e']] """"*** YOUR CODE HERE ***" enumerated = []for i inrange(len(s)): enumerated.append([start + i, s[i]])return enumerated
We will now implement the function distance, which computes the distance between two city objects. Recall that the distance between two coordinate pairs (x1, y1) and (x2, y2) can be found by calculating the sqrt of (x1 - x2)**2 + (y1 - y2)**2. We have already imported sqrt for your convenience. Use the latitude and longitude of a city as its coordinates; you'll need to use the selectors to access this info!
Q4: Distance
from math import sqrtdefdistance(city1,city2):""">>> city1 = make_city('city1', 0, 1)>>> city2 = make_city('city2', 0, 2)>>> distance(city1, city2) 1.0>>> city3 = make_city('city3', 6.5, 12)>>> city4 = make_city('city4', 2.5, 15)>>> distance(city3, city4) 5.0 """"*** YOUR CODE HERE ***"
Next, implement closer_city, a function that takes a latitude, longitude, and two cities, and returns the name of the city that is relatively closer to the provided latitude and longitude.
You may only use the selectors and constructors introduced above and the distance function you just defined for this question.
defcloser_city(lat,lon,city1,city2):""" Returns the name of either city1 or city2, whichever is closest to coordinate (lat, lon).>>> berkeley = make_city('Berkeley', 37.87, 112.26)>>> stanford = make_city('Stanford', 34.05, 118.25)>>> closer_city(38.33, 121.44, berkeley, stanford) 'Stanford'>>> bucharest = make_city('Bucharest', 44.43, 26.10)>>> vienna = make_city('Vienna', 48.20, 16.37)>>> closer_city(41.29, 174.78, bucharest, vienna) 'Bucharest' """"*** YOUR CODE HERE ***"
Q5: Solution
defcloser_city(lat,lon,city1,city2):""" Returns the name of either city1 or city2, whichever is closest to coordinate (lat, lon).>>> berkeley = make_city('Berkeley', 37.87, 112.26)>>> stanford = make_city('Stanford', 34.05, 118.25)>>> closer_city(38.33, 121.44, berkeley, stanford) 'Stanford'>>> bucharest = make_city('Bucharest', 44.43, 26.10)>>> vienna = make_city('Vienna', 48.20, 16.37)>>> closer_city(41.29, 174.78, bucharest, vienna) 'Bucharest' """"*** YOUR CODE HERE ***" coords =make_city('coords', lat, lon)returnget_name(city1)ifdistance(city1, coords)<distance(city2, coords)elseget_name(city2)
Q6: Don't violate the abstraction barrier!
No Code - just a test that checks whether you've written the previous 2 correctly - using constructors, selectors, etc - manipulating data as a compound value.