Problems for Written
1pets = [['cat', 'henning', 10], ['dog', 'rock', 15], ['dog', 'scud', 90]]
- given an array of pets, return the total weight of dogs in the array
Another problem to solve with the same dataset would be: #
- process the array of pets to return a hash with the count of each species, e.g.
1{ 'cat' => 1, 'dog' => 2 }
shallow vs. deep copy #
1a1 = [1, 2, 3, [4, 5]]
2a2 = a1.dup
3a3 = a1.map do |el|
4 el.dup
5end
6
7p a1.object_id == a2.object_id
8
9a1.each_index do |idx|
10 p a1[idx].object_id == a2[idx].object_id
11end
other notes #
-
.equal?
compares the object id's of the objects
Eg:a1.equal?(a2)
wherea1
anda2
are arrays -
experiment with pairs of operations like iterate + transform on nested/complex data structures
-
iterate, transform, select, reduce(
#any?
and#all?
are also reduce operations)