Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The dictionary is the most natural data structure to hold the apple information from the previous lesson. Below are the parallel arrays that track a

The dictionary is the most natural data structure to hold the apple information from the previous lesson. 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).

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).

Step 3: Apple Sorting

Write a function called apple_sorting that has two parameters: data (the data model) 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))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions