Data Structures Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is an array index?

A

Each element in the array has what’s called an index. The first element is at index 0, the next is at index 1, the following is at index 2, and so on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you access an object in an array with the index?

A

With brackets

array = [5, 7, 9, 2, 0]
array[2]
# returns “9”, since “9”
# is at index 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are multidimensional arrays?

A

It’s an array of arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are hashes?

A

A hash is a collection of key-value pairs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you create a hash?

A

You use the Hash.new method.
by setting a variable to Hash.new

my_hash = Hash.new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you add objects to a hash?

A

We add object to hash with bracket notation:

pets = Hash.new
pets["Stevie"] = "cat"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you accesses objects in a hash?

A

We accesses it by using the same method like an array:
puts pets[“Stevie”]

pets = {
  "Stevie" => "cat",
  "Bowser" => "hamster",
  "Kevin Sorbo" => "fish"
}

puts pets[“Stevie”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do we call it when we loop over a hash or an array?

A

We call it that we iterate over the hash or array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we iterate over arrays?

A

We use the .each method
numbers = [1, 2, 3, 4, 5]
numbers.each { |element| puts element }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you iterate over multidimensional arrays?

A
s.each do | sub_array |
  sub_array.each do | y |
    puts y
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you iterate over a hash?

A

As same as in a Array, nbut you need to have two arguments instead of one.

restaurant_menu.each do | item, price |
puts “#{item}: #{price}”
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly