Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Identify Complex Nested Data Structures Let's suppose someone gave you a Ruby file and all it had in it was this: vm = [[[{:name=>Vanilla Cookies,

Identify Complex Nested Data Structures

Let's suppose someone gave you a Ruby file and all it had in it was this:

 
  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}]]]

We can paste this code into IRB and vm will be successfully assigned. There's nothing wrong with this code. Ruby can read it easily. But whoever left us this file forgot that code has to be understood by humans too. Because it's so dense, our minds actively start finding ways to not read what it says. Our brains start suggesting we skip over this monster NDS. In our experience, one sure way to have a hard time reading and writing code that uses an NDS is to skim it and not read it.

So what can we do to help our poor brains out? Ruby (ta-dah!) to the rescue (again)!

Print Out a Complex Nested Data Structure Using the pp Library

We can get a human-friendly version of this output by using the pp, or "pretty-print," library provided by Ruby. In order to "activate" pp, we have to add a require statement at the top of the file.

Why do we have to add a require statement? Ruby ships with lots of features by default. Some of these can slow Ruby down. By default, Ruby only "activates" the most-commonly-used features. Some of its features are inactive by default and we say we want to "activate" them by using require. In time, you'll want to use other libraries (debugging libraries, network libraries, etc.).

Customarily, require statements are stacked at the top of the file.

 
  1. require 'pp'
  2. # Our NDS
  3. 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}]]]
  4. # Some simple things to pp
  5. a_privateer = ["John", "Paul", "Jones"]
  6. an_integer = 42
  7. ## pp something nested, but simple
  8. a_o_a = [ [1,2,3], [4,5,6], [7,8,9]]
  9. pp a_privateer
  10. pp an_integer
  11. pp a_o_a
  12. pp vm

Output:

 
  1. ["John", "Paul", "Jones"]
  2. 42
  3. [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  4. [[[{:name=>"Vanilla Cookies", :price=>3},
  5. {:name=>"Pistachio Cookies", :price=>3},
  6. {:name=>"Chocolate Cookies", :price=>3},
  7. {:name=>"Chocolate Chip Cookies", :price=>3}],
  8. [{:name=>"Tooth-Melters", :price=>12},
  9. {:name=>"Tooth-Destroyers", :price=>12},
  10. {:name=>"Enamel Eaters", :price=>12},
  11. {:name=>"Dentist's Nightmare", :price=>20}],
  12. [{:name=>"Gummy Sour Apple", :price=>3},
  13. {:name=>"Gummy Apple", :price=>5},
  14. {:name=>"Gummy Moldy Apple", :price=>1}]],
  15. [[{:name=>"Grape Drink", :price=>1},
  16. {:name=>"Orange Drink", :price=>1},
  17. {:name=>"Pineapple Drink", :price=>1}],
  18. [{:name=>"Mints", :price=>13},
  19. {:name=>"Curiously Toxic Mints", :price=>1000},
  20. {:name=>"US Mints", :price=>99}]]]

As we can see, pp has tried to make our structures easier to read for humans. It doesn't have much to offer when dealing with simple data, but we start to see its power with NDS'. Let's focus on the vm output. We'll just work with the first few lines.

It's a good idea to save the output of pp into a file. Then we can use our editor to reformat the output to help us get a handle on things. We've added some comments to show our thought process as we looked at the pp'd NDS.

 
  1. [ # outermost structure is an Array
  2. [ #oh, but another one immediately, so it's an AoA
  3. [ # yet another!? It's an AoAoA where the inner Arrays are full of ..
  4. {:name=>"Vanilla Cookies", :price=>3}, # Hashes with two keys!
  5. {:name=>"Pistachio Cookies", :price=>3}, # and another Hash
  6. {:name=>"Chocolate Cookies", :price=>3}, # and another Hash
  7. {:name=>"Chocolate Chip Cookies", :price=>3} # and another Hash
  8. ], # end of inner array
  9. [ #...and so on...
  10. {:name=>"Tooth-Melters", :price=>12},
  11. {:name=>"Tooth-Destroyers", :price=>12},

From the above, we've learned a lot about what we're working with. As you already know from previous lessons, we have an AoAoAoH with keys :name and :price.

Print Out a Complex Nested Data Structure Using Iteration

We can use what we just learned to help guide us to write some "scratch" Ruby code to help us understand our structure. It's important to realize that we don't just write code to solve problems, sometimes we need to write code to understand how we might solve a problem. Sometimes pretty-printing with pp is just simply not enough.

Here's some simple nested while...do...end statements used to reveal the structure of the NDS. Writing this looping code was greatly helped by pp's output as guidance:

Reflect and Recall: Remember, we taught some basic loops to memorize in a previous lesson, the "Nested Iteration Lab." If reading this while...end code feels like writing an alien newspaper, go back and review those materials. The remaining labs will not stop using this pattern and you need to be strong as granite with this skill.

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

Produces:

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

With this looping code and pretty-printing, we should see how to access all those :price keys that we can sum together to create the total value of all the snacks in the vending machine. We'll do that work in the next lab.

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

Students also viewed these Databases questions

Question

2. Define communication.

Answered: 1 week ago