Question
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
- 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}]]]
- row_index = 0
- while row_index < vm.length do
- puts "Row #{row_index} has #{vm[row_index]} columns"
- column_index = 0
- while column_index < vm[row_index].length do
- coord = "#{row_index}, #{column_index}"
- inner_len = vm[row_index][column_index].length
- # Remember \t is a TAB character for indentation
- puts "\tCoordinate [#{coord}] points to an #{vm[row_index][column_index].class} of length #{inner_len}"
- inner_index = 0
- while inner_index < inner_len do
- puts "\t\t (#{coord}, #{inner_len}) is: #{vm[row_index][column_index][inner_index]}"
- inner_index += 1
- end
- column_index += 1
- end
- row_index += 1
- end
Outputs:
- 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
- Coordinate [0, 0] points to an Array of length 4
- (0, 0, 4) is: {:name=>"Vanilla Cookies", :price=>3}
- (0, 0, 4) is: {:name=>"Pistachio Cookies", :price=>3}
- (0, 0, 4) is: {:name=>"Chocolate Cookies", :price=>3}
- (0, 0, 4) is: {:name=>"Chocolate Chip Cookies", :price=>3}
- Coordinate [0, 1] points to an Array of length 4
- (0, 1, 4) is: {:name=>"Tooth-Melters", :price=>12}
- (0, 1, 4) is: {:name=>"Tooth-Destroyers", :price=>12}
- (0, 1, 4) is: {:name=>"Enamel Eaters", :price=>12}
- (0, 1, 4) is: {:name=>"Dentist's Nightmare", :price=>20}
- Coordinate [0, 2] points to an Array of length 3
- (0, 2, 3) is: {:name=>"Gummy Sour Apple", :price=>3}
- (0, 2, 3) is: {:name=>"Gummy Apple", :price=>5}
- (0, 2, 3) is: {:name=>"Gummy Moldy Apple", :price=>1}
- 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
- Coordinate [1, 0] points to an Array of length 3
- (1, 0, 3) is: {:name=>"Grape Drink", :price=>1}
- (1, 0, 3) is: {:name=>"Orange Drink", :price=>1}
- (1, 0, 3) is: {:name=>"Pineapple Drink", :price=>1}
- Coordinate [1, 1] points to an Array of length 3
- (1, 1, 3) is: {:name=>"Mints", :price=>13}
- (1, 1, 3) is: {:name=>"Curiously Toxic Mints", :price=>1000}
- (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.
- 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}]]]
- grand_total = 0
- row_index = 0
- while row_index < vm.length do
- column_index = 0
- while column_index < vm[row_index].length do
- inner_len = vm[row_index][column_index].length
- inner_index = 0
- while inner_index < inner_len do
- # Explanation!
- # vm[row][column][spinner]
- # spinner is full of Hashes with keys :price and :name
- grand_total += vm[row_index][column_index][inner_index][:price]
- inner_index += 1
- end
- column_index += 1
- end
- row_index += 1
- end
- 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:
- hash = {
- "1st Director's Name"=>1234567890,
- "2nd Director's Name"=>1234577890,
- "3rd Director's Name"=>1234709136,
- ...
- }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started