Question

In: Computer Science

The dictionary is the most natural data structure to hold the apple information from the previous...

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

Solutions

Expert Solution

Please find the solution below. Let me know if you have any doubt.

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]

apples={}

for i in range(len(names)):
    inner_dict={}
    inner_dict[('sweetness','tartness')]=(sweetness[i],tartness[i])

    apples[names[i]]=inner_dict

#by_sweetness function which return the sweetness value of the incoming tuple.
def by_sweetness(tup):
    for key, val in tup[1].items():
        return val[0]

for ele in apples.items():
    print('Sweetness for',ele[0],'is',by_sweetness(ele))

Output:


Related Solutions

State a data structure that is suitable for storing (i) the words in a dictionary to...
State a data structure that is suitable for storing (i) the words in a dictionary to facilitate searching (ii) the set of folders you have in your computer Explain your choices.
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
C++ program: Define a structure to hold the contact's information including: name, phone number, and a...
C++ program: Define a structure to hold the contact's information including: name, phone number, and a pointer to the next node on the list. Each node on the list will be a contact instead of a number (like the example in the book). struct ContactNode { string name; string phoneNumber; ContactNode *next; } Define a class containing the structure, private member variable head (that will point to the beginning of the list) and the following member functions: A constructor that...
What decisions on the basis of this information, including the same data for the previous period...
What decisions on the basis of this information, including the same data for the previous period and possibly two years prior, can investors or shareholders take (focus on future growth, evolution of risk level, or volatility of future earnings and evolution of the 'time horizon of visibility' into the firm's future)?
Using the data set from the previous question (scatterplot.xlsx). With the data point with the smallest...
Using the data set from the previous question (scatterplot.xlsx). With the data point with the smallest x value removed, the scatter plot now suggests a ________   ["nonlinear" OR "linear"]         relationship between the dependent and independent variables. Data: x y 45 2358 56 4204 26 287 54 3849 24 925 23 3273 34 -678 45 3748 47 2898 43 1974 32 226
Create a class BankAccount to hold at least the following data / information about a bank...
Create a class BankAccount to hold at least the following data / information about a bank account: Account balance Total number of deposits Total number of withdrawals Interest rate e.g., annual rate = 0.05 Service charges per month The class should have the following member functions: Constructor To set the required data. It may be parameterized or user’s input. depositAmount A virtual function used to accept an argument for the amount to be deposited. It should add the argument (amount)...
Inference from two independent samples. The data in the previous question (?̅ = 217,900, ? =...
Inference from two independent samples. The data in the previous question (?̅ = 217,900, ? = 91,200, ? = 25) was from 2013. The real estate agent believes that home prices have increased in the five years since then. In order to assess whether or not this is true, she gathers data on a new random sample of 40 existing single-family home sales from her neighborhood in 2018. The average sale price from her 2018 sample is ?̅ = 252,900...
Grasshopper ketone is a natural product isolated from the flightless grasshopper Romalea microptera. From its structure,...
Grasshopper ketone is a natural product isolated from the flightless grasshopper Romalea microptera. From its structure, locate the next types of bonds or atoms. (Note: the dark dot in the middle of the double bonds corresponds to the presence of a carbon.) An sp3-hybridized carbon atom A highly polarized covalent single bond A nearly nonpolar covalent bond An sp2-hybridized carbon atom A highly polarized covalent double bond An sp-hybridized carbon atom A bond between atoms of different hybridizations
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do...
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result I need to see the state of the stack1 and stack2 after doing the following operation. (stack1 and stack2 need to have individually there own state result) stack1.push(1) stack1.push(2) stack2.push(3) stack2.push(4) stack1.pop() stackTop = stack2.peek() stack1.push(stackTop) stack1.push(5) stack2.pop() stack2.push(6) ArrayStackInterface.h // Created by Frank M. Carrano and Tim Henry. // Copyright...
Using the data from the previous question on LDL cholesterol, you decide to test if the...
Using the data from the previous question on LDL cholesterol, you decide to test if the variance in LDL cholesterol of patients admitted to the hospital with a heart attack is the same as that of those who have not (the control). You use the R function var.test and obtain the following output.    F test to compare two variances data:  ldl.ha and ldl.cont F = 7.683, num df = 9, denom df = 15,    p-value = 0.0006501 alternative hypothesis:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT