(Solved) : Identify Complex Nested Data Structures Let S Suppose Someone Gave Ruby File Vm Name Vanil Q43912044 . . .
Identify Complex Nested Data Structures
Let’s suppose someone gave you a Ruby file and all it had in itwas this:
- 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=>”EnamelEaters”, :price=>12}, {:name=>”Dentist’s Nightmare”,:price=>20}], [{:name=>”Gummy Sour Apple”, :price=>3},{:name=>”Gummy Apple”, :price=>5}, {:name=>”Gummy MoldyApple”, :price=>1}]], [[{:name=>”Grape Drink”, :price=>1},{:name=>”Orange Drink”, :price=>1}, {:name=>”PineappleDrink”, :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 successfullyassigned. There’s nothing wrong with this code. Ruby can read iteasily. But whoever left us this file forgot that code has to beunderstood by humans too. Because it’s so dense, our mindsactively start finding ways to not read what it says. Ourbrains start suggesting we skip over this monster NDS. In ourexperience, one sure way to have a hard time reading and writingcode 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!) tothe rescue (again)!
Print Out a Complex Nested Data Structure Using the ppLibrary
We can get a human-friendly version of this output by using thepp, or “pretty-print,” library provided by Ruby. In order to”activate” pp, we have to add a require statement at the top of thefile.
Why do we have to add a require statement? Ruby ships with lotsof features by default. Some of these can slow Ruby down. Bydefault, 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 otherlibraries (debugging libraries, network libraries, etc.).
Customarily, require statements are stacked at the top of thefile.
- require ‘pp’
- # Our NDS
- 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=>”EnamelEaters”, :price=>12}, {:name=>”Dentist’s Nightmare”,:price=>20}], [{:name=>”Gummy Sour Apple”, :price=>3},{:name=>”Gummy Apple”, :price=>5}, {:name=>”Gummy MoldyApple”, :price=>1}]], [[{:name=>”Grape Drink”, :price=>1},{:name=>”Orange Drink”, :price=>1}, {:name=>”PineappleDrink”, :price=>1}], [{:name=>”Mints”, :price=>13},{:name=>”Curiously Toxic Mints”, :price=>1000},{:name=>”US Mints”, :price=>99}]]]
- # Some simple things to pp
- a_privateer = [“John”, “Paul”, “Jones”]
- an_integer = 42
- ## pp something nested, but simple
- a_o_a = [ [1,2,3], [4,5,6], [7,8,9]]
- pp a_privateer
- pp an_integer
- pp a_o_a
- pp vm
Output:
- [“John”, “Paul”, “Jones”]
- 42
- [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- [[[{: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}]]]
As we can see, pp has tried to make our structures easier toread for humans. It doesn’t have much to offer whendealing 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 fewlines.
It’s a good idea to save the output of pp into a file. Then wecan use our editor to reformat the output to help us get a handleon things. We’ve added some comments to show our thought process aswe looked at the pp’d NDS.
- [ # outermost structure is an Array
- [ #oh, but another one immediately, so it’s an AoA
- [ # yet another!? It’s an AoAoA where the inner Arrays are fullof ..
- {:name=>”Vanilla Cookies”, :price=>3}, # Hashes with twokeys!
- {:name=>”Pistachio Cookies”, :price=>3}, # and anotherHash
- {:name=>”Chocolate Cookies”, :price=>3}, # and anotherHash
- {:name=>”Chocolate Chip Cookies”, :price=>3} # andanother Hash
- ], # end of inner array
- [ #…and so on…
- {:name=>”Tooth-Melters”, :price=>12},
- {:name=>”Tooth-Destroyers”, :price=>12},
From the above, we’ve learned a lot about what we’re workingwith. As you already know from previous lessons, we have an AoAoAoHwith 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’simportant to realize that we don’t just write code tosolve problems, sometimes we need to write code tounderstand how we might solve a problem. Sometimespretty-printing with pp is just simply not enough.
Here’s some simple nested while…do…end statements used toreveal the structure of the NDS. Writing this looping code wasgreatly helped by pp’s output as guidance:
Reflect and Recall: Remember, we taught somebasic loops to memorize in a previous lesson, the “Nested IterationLab.” If reading this while…end code feels like writing an aliennewspaper, go back and review those materials. The remaining labswill not stop using this pattern and you need to be strong asgranite with this skill.
- 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=>”EnamelEaters”, :price=>12}, {:name=>”Dentist’s Nightmare”,:price=>20}], [{:name=>”Gummy Sour Apple”, :price=>3},{:name=>”Gummy Apple”, :price=>5}, {:name=>”Gummy MoldyApple”, :price=>1}]], [[{:name=>”Grape Drink”, :price=>1},{:name=>”Orange Drink”, :price=>1}, {:name=>”PineappleDrink”, :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 “tt (#{coord}, #{inner_index}) is:#{vm[row_index][column_index][inner_index]}”
- inner_index += 1
- end
- column_index += 1
- end
- row_index += 1
- end
Produces:
- 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=>”EnamelEaters”, :price=>12}, {:name=>”Dentist’s Nightmare”,:price=>20}], [{:name=>”Gummy Sour Apple”, :price=>3},{:name=>”Gummy Apple”, :price=>5}, {:name=>”Gummy MoldyApple”, :price=>1}]] columns
- Coordinate [0, 0] points to an Array of length 4
- (0, 0, 0) is: {:name=>”Vanilla Cookies”, :price=>3}
- (0, 0, 1) is: {:name=>”Pistachio Cookies”,:price=>3}
- (0, 0, 2) is: {:name=>”Chocolate Cookies”,:price=>3}
- (0, 0, 3) is: {:name=>”Chocolate Chip Cookies”,:price=>3}
- Coordinate [0, 1] points to an Array of length 4
- (0, 1, 0) is: {:name=>”Tooth-Melters”, :price=>12}
- (0, 1, 1) is: {:name=>”Tooth-Destroyers”,:price=>12}
- (0, 1, 2) is: {:name=>”Enamel Eaters”, :price=>12}
- (0, 1, 3) is: {:name=>”Dentist’s Nightmare”,:price=>20}
- Coordinate [0, 2] points to an Array of length 3
- (0, 2, 0) is: {:name=>”Gummy Sour Apple”, :price=>3}
- (0, 2, 1) is: {:name=>”Gummy Apple”, :price=>5}
- (0, 2, 2) is: {:name=>”Gummy Moldy Apple”,:price=>1}
- Row 1 has [[{:name=>”Grape Drink”, :price=>1},{:name=>”Orange Drink”, :price=>1}, {:name=>”PineappleDrink”, :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, 0) is: {:name=>”Grape Drink”, :price=>1}
- (1, 0, 1) is: {:name=>”Orange Drink”, :price=>1}
- (1, 0, 2) is: {:name=>”Pineapple Drink”, :price=>1}
- Coordinate [1, 1] points to an Array of length 3
- (1, 1, 0) is: {:name=>”Mints”, :price=>13}
- (1, 1, 1) is: {:name=>”Curiously Toxic Mints”,:price=>1000}
- (1, 1, 2) is: {:name=>”US Mints”, :price=>99}
With this looping code and pretty-printing, we should see how toaccess all those :price keys that we can sum together to create thetotal value of all the snacks in the vending machine. We’ll do thatwork in the next lab.
Expert Answer
Answer to Identify Complex Nested Data Structures Let’s suppose someone gave you a Ruby file and all it had in it was this: vm = […
OR