Arrays Flashcards

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

What does “each” method return on an array?

A
It always returns the original array.
e.g.
x = [1, 2, 3, 4, 5]
x.each do |a|
a + 1
end

=>[1, 2, 3, 4, 5]

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

What do “array.first” and “array.last” do? (+examples)

A

They return the first and the last element of the array.

e.g.
array = [1,2,3,4,5]

array.first
=>1
array.last
=>5

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

What are the two result

options after a method is evaluated?

A

It either returns new data or modifies the original data.

new data:(does not mutate the caller)
.sort (arranges numbers in the array in order)

modifies: (mutate the caller)
.pop (takes the last element of the array)

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

How do you add an argument to the beginning and take one away from the end of a(n) array/list?

A

a = [3,4,5,6]

a.unshift(10)
=> [10,3,4,5,6]

a.pop
=>[10,3,4,5]

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

How to add an item back to the array permanently?

A

a = [2,5,6]

a.push(3)
=>[2,5,6,3]

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

What is the alternative to the .pop method?

A

«

a = [2,3,4,6]

a &laquo_space;6
=>[2,3,4]

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

What is the difference between .map and .collect methods and how do they work?

A

They are the same.

a = [1,2,3,4]

a.collect {|num| num*5}
=> [5,10,15,20]

a.map {|num| num*5}
=> [5,10,15,20]

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

What is the difference between the “delete” and “delete_at” methods?

A

Both can delete an element
from the array, the “delete_at” targets it by its index (array[2]) while the “delete” targets it by its value (array[“lemon”]).
Both mutate the caller.

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

How can you iterate through an array and delete the duplicated values?

A
With the .uniq method.
e.g.
array = [1,2,3,4,2,1,3,2,1]
array.uniq
=> [1,2,3,4]

It doesn’t mutate the caller.

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

Write a method that iterates through an array and return those numbers that are less than 8!
array = [2,6,9,8,12,1,4,7,25]

A

array.select {|num| num < 8}

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

What is the alternative to the .pop method?

A

«

a = [2,3,4,6]

a &laquo_space;6
=>[2,3,4]

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

What is the difference between .map and .collect methods and how do they work?

A

They are the same.

a = [1,2,3,4]

a.collect {|num| num*5}
=> [5,10,15,20]

a.map {|num| num*5}
=> [5,10,15,20]

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

What is the difference between the “delete” and “delete_at” methods?

A

Both can delete an element
from the array, the “delete_at” targets it by its index (array[2]) while the “delete” targets it by its value (array[“lemon”]).
Both mutate the caller.

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

How can you iterate through an array and delete the duplicated values?

A
With the .uniq method.
e.g.
array = [1,2,3,4,2,1,3,2,1]
array.uniq
=> [1,2,3,4]

It doesn’t mutate the caller.

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

Write a method that iterates through an array and return those numbers that are less than 8!
array = [2,6,9,8,12,1,4,7,25]

A

array.select {|num| num < 8}

=> [2,6,1,4,7]

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

What does the bang suffix (!) indicate usually?

A

It indicates that the method will change (or mutate) the caller permanently.

17
Q

How do you compare arrays?

A

With the equality operator (==)
The result is a boolean value.

e.g.
a = [1,2,3]
b = [2,5,6]
=> false

18
Q

What do you use “include?” method for?

A

It checks to see if the argument given is included in the array.
It returns a boolean value.

e.g.
a = [2,6,8,9]
a.include?(8)
=> true

19
Q

How do you turn an array containing nested arrays into a one-dimensional array?

A
Use the ".flatten" method!
e.g.
a = [1,2, [3,4,5], [2,9]
a.flatten
=>[1,2,3,4,5,2,9]

It doesn’t mutate the caller.

20
Q

What is the difference between “.each”, “.each_index” and “.each_with_index”?

A

They all iterate through an array.
“.each” represents the value at each index
“.each_index” represents the index number
“.each_with_index” can manipulate both the value and the index by passing two parameters to the block of code.

e.g.
a = ["a", "b", "c"]
a.each_with_index {|val, idx| puts "#{idx+1}. #{val}" }
1. a
2. b
3. c
21
Q

What does the .sort array do?

A

It is a handy way to order an array.
a = [2,9,3,5,4,6]
a.sort
=> [2,3,4,5,6,9]

It mutates the caller if we add (!).
It works with strings and floats as well.

22
Q

Which method combines two arrays and returns their combination of all elements as an array?

A

The “.product” method.
e.g.
[“a”, “d”].product([1,3,8])
=> [[“a”, 1], [“a”, 3], [“a”, 8], [“b”, 1], [“b.”, 3], [“b”, 8]]

23
Q

What is the difference between “.each” and “.map”?

A
Both iterate over a collection.
".each" it does the operation and returns the original value.
".map" creates and returns a new array containing the values by the block.
e.g.
a = [1,2,3]
a.each {|e| print e}
1,2,3 
=> [1,2,3]

a = [1,2,3]
a.map {|e| e**2}
=>[1,4,9]

24
Q

Insert 3 into the following array, after 1!

array = [1,2,3,6,9]

A

puts array.insert(1, 3)
=> [1,3,2,3,6,9]

(inserts before the element’s index, in this case we want it after 1 which is index 0, next element (3) is index 1 that’s why we use it)

25
Q

What is the value of a and why?

a = 7
array = [1,2,3]

array.each do |a|
a += 1
end

puts a

A

7 because the second a is initialized inside the block and executes on the array and never sees the first a that is initialized in the outer scope, that’s why a stays 7.
this is called shadowing.
(when a block argument hides a local variable that is defined outside the block)

26
Q

What does .each method return?

A

It returns the original array it was called on.

e.g.
array = [1,2,3]

array.each do |a|
a += 1
end

puts array
=> [1,2,3]

27
Q

What does %w() represents?

A

It represents an array!
e.g.
%w(y n) is the same as [‘y’, ‘n’]

28
Q

What does the sample method do on an array?

A

It chooses a random element from the array.

a = [1,2,3,4,5,6,7,8,9]
a.sample
=> 7

29
Q

What does “select” method do?

A

Returns a new array containing all elements of array for which the given block returns a true value.
e.g.
numbers = [5,9,21,26,39]
div_by_three = numbers.select do |number|
number % 3 == 0
end

p div_by_three
=> [9,21,39]

30
Q

What is the difference between “select” and “map” methods?

A

It is the way the new element’s value is chosen.
“map” returns a new array with each element transformed based on the block’s return value.
“select” returns a new array containing elements selected only if the block’s return value evaluates to true.