Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Building on NDS Exploring Code vm = [[[{:name=>Vanilla Cookies, :price=>3}, {:name=>Pistachio Cookies, :price=>3}, {:name=>Chocolate Cookies, :price=>3}, {:name=>Chocolate Chip Cookies, :price=>3}], [{:name=>Tooth-Melters, :price=>12}, {:name=>Tooth-Destroyers, :price=>12}, {:name=>Enamel

Building on NDS Exploring Code

  1. vm = [[[{:name=>"Vanilla Cookies", :price=>3}, {:name=>"Pistachio Cookies", :price=>3}, {:name=>"Chocolate Cookies", :price=>3}, {:name=>"Chocolate Chip Cookies", :price=>3}], [{:name=>"Tooth-Melters", :price=>12}, {:name=>"Tooth-Destroyers", :price=>12}, {:name=>"Enamel Eaters", :price=>12}, {:name=>"Dentist's Nightmare", :price=>20}], [{:name=>"Gummy Sour Apple", :price=>3}, {:name=>"Gummy Apple", :price=>5}, {:name=>"Gummy Moldy Apple", :price=>1}]], [[{:name=>"Grape Drink", :price=>1}, {:name=>"Orange Drink", :price=>1}, {:name=>"Pineapple Drink", :price=>1}], [{:name=>"Mints", :price=>13}, {:name=>"Curiously Toxic Mints", :price=>1000}, {:name=>"US Mints", :price=>99}]]]
  2. row_index = 0
  3. while row_index < vm.length do
  4. puts "Row #{row_index} has #{vm[row_index]} columns"
  5. column_index = 0
  6. while column_index < vm[row_index].length do
  7. coord = "#{row_index}, #{column_index}"
  8. inner_len = vm[row_index][column_index].length
  9. # Remember \t is a TAB character for indentation
  10. puts "\tCoordinate [#{coord}] points to an #{vm[row_index][column_index].class} of length #{inner_len}"
  11. inner_index = 0
  12. while inner_index < inner_len do
  13. puts "\t\t (#{coord}, #{inner_len}) is: #{vm[row_index][column_index][inner_index]}"
  14. inner_index += 1
  15. end
  16. column_index += 1
  17. end
  18. row_index += 1
  19. end

Outputs:

 
  1. Row 0 has [[{:name=>"Vanilla Cookies", :price=>3}, {:name=>"Pistachio Cookies", :price=>3}, {:name=>"Chocolate Cookies", :price=>3}, {:name=>"Chocolate Chip Cookies", :price=>3}], [{:name=>"Tooth-Melters", :price=>12}, {:name=>"Tooth-Destroyers", :price=>12}, {:name=>"Enamel Eaters", :price=>12}, {:name=>"Dentist's Nightmare", :price=>20}], [{:name=>"Gummy Sour Apple", :price=>3}, {:name=>"Gummy Apple", :price=>5}, {:name=>"Gummy Moldy Apple", :price=>1}]] columns
  2. Coordinate [0, 0] points to an Array of length 4
  3. (0, 0, 4) is: {:name=>"Vanilla Cookies", :price=>3}
  4. (0, 0, 4) is: {:name=>"Pistachio Cookies", :price=>3}
  5. (0, 0, 4) is: {:name=>"Chocolate Cookies", :price=>3}
  6. (0, 0, 4) is: {:name=>"Chocolate Chip Cookies", :price=>3}
  7. Coordinate [0, 1] points to an Array of length 4
  8. (0, 1, 4) is: {:name=>"Tooth-Melters", :price=>12}
  9. (0, 1, 4) is: {:name=>"Tooth-Destroyers", :price=>12}
  10. (0, 1, 4) is: {:name=>"Enamel Eaters", :price=>12}
  11. (0, 1, 4) is: {:name=>"Dentist's Nightmare", :price=>20}
  12. Coordinate [0, 2] points to an Array of length 3
  13. (0, 2, 3) is: {:name=>"Gummy Sour Apple", :price=>3}
  14. (0, 2, 3) is: {:name=>"Gummy Apple", :price=>5}
  15. (0, 2, 3) is: {:name=>"Gummy Moldy Apple", :price=>1}
  16. Row 1 has [[{:name=>"Grape Drink", :price=>1}, {:name=>"Orange Drink", :price=>1}, {:name=>"Pineapple Drink", :price=>1}], [{:name=>"Mints", :price=>13}, {:name=>"Curiously Toxic Mints", :price=>1000}, {:name=>"US Mints", :price=>99}]] columns
  17. Coordinate [1, 0] points to an Array of length 3
  18. (1, 0, 3) is: {:name=>"Grape Drink", :price=>1}
  19. (1, 0, 3) is: {:name=>"Orange Drink", :price=>1}
  20. (1, 0, 3) is: {:name=>"Pineapple Drink", :price=>1}
  21. Coordinate [1, 1] points to an Array of length 3
  22. (1, 1, 3) is: {:name=>"Mints", :price=>13}
  23. (1, 1, 3) is: {:name=>"Curiously Toxic Mints", :price=>1000}
  24. (1, 1, 3) is: {:name=>"US Mints", :price=>99}

Based on this code and its output, we can slowly step toward a model of how to get the prices we need to add together. As part of our strategy, we're still printing out useful "debug" data to the screen. In that debug data we see a way forward. Instead of printing out:

(1, 0, 3) is: {:name=>"Grape Drink", :price=>1}

we can take the value pointed to by :price and add it to a grand total. We'll build on the code we just demonstrated above, and add a few code comments to help us remember what's happening inside our code.

 
  1. vm = [[[{:name=>"Vanilla Cookies", :price=>3}, {:name=>"Pistachio Cookies", :price=>3}, {:name=>"Chocolate Cookies", :price=>3}, {:name=>"Chocolate Chip Cookies", :price=>3}], [{:name=>"Tooth-Melters", :price=>12}, {:name=>"Tooth-Destroyers", :price=>12}, {:name=>"Enamel Eaters", :price=>12}, {:name=>"Dentist's Nightmare", :price=>20}], [{:name=>"Gummy Sour Apple", :price=>3}, {:name=>"Gummy Apple", :price=>5}, {:name=>"Gummy Moldy Apple", :price=>1}]], [[{:name=>"Grape Drink", :price=>1}, {:name=>"Orange Drink", :price=>1}, {:name=>"Pineapple Drink", :price=>1}], [{:name=>"Mints", :price=>13}, {:name=>"Curiously Toxic Mints", :price=>1000}, {:name=>"US Mints", :price=>99}]]]
  2. grand_total = 0
  3. row_index = 0
  4. while row_index < vm.length do
  5. column_index = 0
  6. while column_index < vm[row_index].length do
  7. inner_len = vm[row_index][column_index].length
  8. inner_index = 0
  9. while inner_index < inner_len do
  10. # Explanation!
  11. # vm[row][column][spinner]
  12. # spinner is full of Hashes with keys :price and :name
  13. grand_total += vm[row_index][column_index][inner_index][:price]
  14. inner_index += 1
  15. end
  16. column_index += 1
  17. end
  18. row_index += 1
  19. end
  20. p grand_total #=> 1192

Look at that! It's an insight! All these snacks are worth $1192.00

Lab

In this lab, you're going to work through the directors database and create a Hash that records the director's name as a key, and the total grosses of all of their movies as the value. For example:

 
  1. hash = {
  2. "1st Director's Name"=>1234567890,
  3. "2nd Director's Name"=>1234577890,
  4. "3rd Director's Name"=>1234709136,
  5. ...
  6. }

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

Question

Enhance the basic quality of your voice.

Answered: 1 week ago

Question

Describe the features of and process used by a writing team.

Answered: 1 week ago