Question

In: Computer Science

Below are the parallel arrays that track a different attribute of the apples (note that the...

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

Solutions

Expert Solution

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


Related Solutions

C program: 1, Make two parallel arrays, one to keep track of food names and another...
C program: 1, Make two parallel arrays, one to keep track of food names and another to keep track of the food prices. There will be a total of 10 items, with a total of about 100 characters for each food name title (i think we can use malloc for the strings and allocate it accordingly) Basically it will be something like a menu: 1, we will enter the food information 2, it will list all the food names and...
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2)...
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2) a) How would you compare two arrays to check if they have the same values? b) Assume array1 and array2 are int arrays with 10 elements in each. if(array1 == array2) What is this comparing? 3 a) Can you encounter memory violation using an array? b) If yes explain. If no, explain why not.
Read and print parallel array - How can this be made to read parallel arrays and...
Read and print parallel array - How can this be made to read parallel arrays and then print them? The program presented here is intended to read from the text file and build parallel arrays. Then it will print as shown. The first line will not be stored in the array. The second line will be stored in firstArray[] and the third line will then be stored in secondArray[] and the arrays will repeat until the file is read. begin...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
4) The weights (in ounces) of 14 different apples are shown below. Find the mode(s) for...
4) The weights (in ounces) of 14 different apples are shown below. Find the mode(s) for the given sample data. (If there is more than one modes, enter the largest value for credit. If there is no mode, enter 0 for credit.) 9, 11, 9, 8, 7, 9, 8,13, 8, 7, 9, 8, 8, 9 5) Andrew asked eight of his friends how many cousins they had. The results are listed below. Find the standard deviation of the number of...
A music player or music organization program can keep track of how many different artists are in a library. First note how many different
A music player or music organization program can keep track of how many different artists are in a library. First note how many different artists are in your music library. Then find the probability that if 25 songs are selected at random, none will have the same artist.
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Define parallel arrays, tell how the data in each array is accessed and give an example...
Define parallel arrays, tell how the data in each array is accessed and give an example of parallel arrays.
Parallel Arrays This question is in MindTap Cengage Summary In this lab, you use what you...
Parallel Arrays This question is in MindTap Cengage Summary In this lab, you use what you have learned about parallel arrays to complete a partially completed C++ program. The program should: Either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop Or it should print the message Sorry, we do not carry that. Read the problem description carefully before you begin. The file provided for this lab includes the necessary variable declarations and input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT