In: Computer Science
Below are the parallel arrays that track a different attribute of the apples (note that the above chart (Sweet-Tart Chart) doesn't align with the data from the previous lesson):
names = ["McIntosh", "Red Delicious", "Fuji", "Gala", "Ambrosia", "Honeycrisp", "Granny Smith"]
sweetness = [3, 5, 8, 6, 7, 7.5, 1]
tartness = [7, 1, 3, 1, 1, 8, 10]
Step 1: Data Model (parallel arrays to dictionary)
Build a dictionary named apples. The apple dictionary keys will be the names of the apples. The values will be another dictionary. This value dictionary will have two keys: "sweetness" and "tartness". The values for those keys will be the respective values from the sweetness and tartness lists given above. You will build this by defining a variable named apples (see the complex_map example).
This is why dicitionarries are also calleed associative arrays. The arrays need to be kept in order so they can associate the same index with the same corrresoponding value that make holding this kind of data easier.
Step 2: Apple Aid
Now that we have our model, create a function named by_sweetness whose parameter will be a tuple (from apples.items()) The function by_sweetness will return the sweetness value of the incoming tuple. This helper function cannot reference the global variable apples (from step 1).
Create another function by_tartness that is essentially the same as by_sweetness but returns the tartness value of the incoming tuple.
Step 3: Apple Sorting
Write a function called apple_sorting that has two parameters: data (the data model dictionary) and sort_helper (the function used to sort the model). The apple_sorting should use the sorted function to sort the data (e.g. data.items()) with sort_helper. The function returns a list of tuples in order of their sweetness (sweetest apples listed first).
Once done, this should work:
print(apple_sorting(apples, by_sweetness))
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
names = ["McIntosh", "Red
Delicious", "Fuji",
"Gala", "Ambrosia",
"Honeycrisp", "Granny
Smith"]
sweetness = [3, 5, 8, 6, 7, 7.5, 1]
tartness = [7, 1, 3, 1, 1, 8, 10]
#creating an empty dict
apples=dict()
#looping through each index in the parallel lists
for i in range(len(names)):
#creating a dict
value=dict()
#adding sweetness value to the value
dict
value['sweetness']=sweetness[i]
#adding tartness value
value['tartness']=tartness[i]
#adding values dict to apples dict with key
being current name
apples[names[i]]=value
#method to return sweetness value in a given tuple from
apples.dict()
def by_sweetness(app):
#here, first element in tuple is the name of
apple, second element is
#the dict containing sweetness and tartness, so
we just return the sweetness
return
app[1]['sweetness']
#method to return tartness value in a given tuple from
apples.dict()
def by_tartness(app):
#returning the tartness
return
app[1]['tartness']
#method to sort given items by method specified by
sort_helper
def apple_sorting(data, sort_helper):
#returning sorted list of tuples sorted in
the descending order of
#sweetness or tartness specified by
sort_helper
return
sorted(data.items(),key=lambda e: sort_helper(e),
reverse=True)
#printing list of tuples sorted by sweetness
print(apple_sorting(apples, by_sweetness))