Please use PYTHON only and follow all the following instructions carefully. Than
ID: 3597255 • Letter: P
Question
Please use PYTHON only and follow all the following instructions carefully. Thank you
Please make sure that it can pass all the test cases as follows: https://repl.it/Mz2p
counts(xs): Consider a sequence of values, xs. It can contain duplicates, and we'd like to know how many of each present value there are. Construct and return a dictionary whose keys are the things found in the sequence, and whose corresponding values are the counts of occurrences. Return value: a dictionary of things and their number of occurrences in xs 5: xs :: sequence of values. (It could be a list, a string, or other things...) Examples o counts([1,1,1,2,3,3,3,3,5]) o counts ("abracadabra" {1: 3, 2: 1, 3: 4, 1} 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" >>> [ rose, shamrock' >>> [ carnation', 'fern 'shamrock' . weeklies({'shrock','weekly', 'cactus ','monthly', .rose','weekly', .succulent. :'biweekly')) * weeklies({'fern .:"weekly', .shrock','weekly', 'carnation .:.weekly.)) 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, "T'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 closest(d, "gas", (5,5)) >>> closest(d, "food", (5,5)) (5, 5) >closest (d, "food", (1,4)) >>>print (closest(d,"hotel", (1,4))) None file_counts(filename): Re-implementation of the counts function that obtains a list of numbers from a file. In the file, every single line will contain a single integer and nothing else. The last character will be a newline. file1.txt file2.txt 100 1 100 2 filename :: a string indicating the name of a file in the current directory Return value: a dictionary of int as keys and # of occurrences as the values. *file_counts("file1.txt") *file_counts("file2.txt") 100 4 {100:3, 3:1, 92)Explanation / Answer
def counts(xs):
count_dict = {}
for x in xs:
if x in count_dict:
count_dict[x] += 1
else:
count_dict[x] = 1
return count_dict
def weeklies(plants_d):
water_weekly = []
for plant in plants_d:
if plants_d[plant] == 'weekly':
water_weekly.append(plant)
water_weekly.sort()
return water_weekly
def distance(coord1, coord2):
return ((coord1[0]-coord2[0])**2 + (coord1[1]-coord2[1])**2)**.5
def closest(d, what, here):
min_distance = float('inf')
min_coord = None
for coord in d:
if d[coord] == what:
what_distance = distance(coord, here)
if what_distance < min_distance:
min_distance = what_distance
min_coord = coord
return min_coord
def file_counts(filename):
count_dict = {}
with open(filename) as fh:
for line in fh:
x = int(line)
if x in count_dict:
count_dict[x] += 1
else:
count_dict[x] = 1
return count_dict
# copy pastable code link: https://paste.ee/p/dDp1j
'''
Sample execution
python3 tester.py hw.py
Running required definitions:
.........................
----------------------------------------------------------------------
Ran 25 tests in 0.006s
OK
25/25 Required test cases passed (worth 4 each)
Score based on test cases: 100.00/100 (25.00*4)
'''