Arrays Flashcards

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

build up multi-dimensional arrays

A
empty_table = Array.new(3) { Array.new(3) }
#=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

return the first n elements of an array

A

arr.take(3) #=> [1, 2, 3]

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

special methods first

A

arr.first #=> 1

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

special methods last

A

arr.last #=> 6

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

returning the elements after n elements

A

arr.drop(3) #=> [4, 5, 6]

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

arr = [1, 2, 3, 4, 5, 6]

arr[2]

A

=> 3

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

arr = [1, 2, 3, 4, 5, 6]

arr[100]

A

=> nil

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

arr = [1, 2, 3, 4, 5, 6]

arr[-3]

A

=> 4

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

arr = [1, 2, 3, 4, 5, 6]

arr[2, 3]

A

=> [3, 4, 5]

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

arr = [1, 2, 3, 4, 5, 6]

arr[1..4]

A

=> [2, 3, 4, 5]

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

arr = [1, 2, 3, 4, 5, 6]

arr.at(0)

A

=> 1

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

arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]

arr.fetch(100)

A

=> IndexError: index 100 outside of array bounds: -6…6

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

arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]

arr.fetch(100, “oops”)

A

=> “oops”

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

arr = [1, 2, 3, 4, 5, 6]

arr.first

A

=> 1

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

arr = [1, 2, 3, 4, 5, 6]

arr.last

A

=> 6

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

browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]

browsers.length #=> 5

A

=> 5

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

browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]

browsers.count

A

=> 5

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

browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]

browsers.empty?

A

=> false

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

browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]

browsers.include?(‘Konqueror’)

A

=> false

20
Q

arr = [1, 2, 3, 4]

arr.push(5)

A

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

21
Q

arr = [1, 2, 3, 4]

arr

A

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

22
Q

add item to array

A

use arr.push(5) or arr

23
Q

add a new item to the beginning of an array

A

arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]

24
Q

[1, 2, 3, 4, 5, 6]

arr.unshift(0)

A

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

25
Q

add a new element to an array at any position

A

arr.insert(3, ‘apple’) #=> [0, 1, 2, ‘apple’, 3, 4, 5, 6]

26
Q

[0, 1, 2, 3, 4, 5, 6]

arr.insert(3, ‘apple’)

A

=> [0, 1, 2, ‘apple’, 3, 4, 5, 6]

27
Q

insert multiple values at once

A
arr.insert(3, 'orange', 'pear', 'grapefruit')
#=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
28
Q

[0, 1, 2, ‘apple’, 3, 4, 5, 6]

arr.insert(3, ‘orange’, ‘pear’, ‘grapefruit’)

A

=> [0, 1, 2, “orange”, “pear”, “grapefruit”, “apple”, 3, 4, 5, 6]

29
Q

removes the last element in an array and returns it

A

arr = [1, 2, 3, 4, 5, 6]
arr.pop #=> 6
arr #=> [1, 2, 3, 4, 5]

30
Q

arr = [1, 2, 3, 4, 5, 6]

arr.pop

A

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

31
Q

retrieve and at the same time remove the first item

A

arr.shift #=> 1

arr #=> [2, 3, 4, 5]

32
Q

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

arr.shift

A
#=> 1
arr #=> [2, 3, 4, 5]
33
Q

delete an element at a particular index

A

arr.delete_at(2) #=> 4

arr #=> [2, 3, 5]

34
Q

arr #=> [2, 3, 4, 5]

arr.delete_at(2) #=> 4

A

arr #=> [2, 3, 5]

35
Q

delete a particular element anywhere in an array

A

arr = [1, 2, 2, 3]

arr.delete(2) #=> [1, 3]

36
Q

arr = [1, 2, 2, 3]

arr.delete(2)

A

=> [1, 3]

37
Q

arr = [1, 2, 2, 3]

arr.delete(2)

A

delete a particular element anywhere in an array

arr.delete(2) #=> [1, 3]

38
Q

remove nil values from an array

A

arr = [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]
arr.compact #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
arr #=> [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]
arr.compact! #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
arr #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]

39
Q

arr = [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]

arr.compact

A

=> [‘foo’, 0, ‘bar’, 7, ‘baz’]

40
Q

arr #=> [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]

arr.compact!

A

arr #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]

41
Q

remove duplicate elements from an array, non-destructive

A

arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]

42
Q

arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]

arr.uniq

A

[2, 5, 6, 556, 8, 9, 0, 123]

non-destructive arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]

43
Q

arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]

arr.uniq!

A

[2, 5, 6, 556, 8, 9, 0, 123]

destructive arr = [2, 5, 6, 556, 8, 9, 0, 123]

44
Q

remove duplicate elements from an array, destructive

A

arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq! #=> [2, 5, 6, 556, 8, 9, 0, 123]
arr = [2, 5, 6, 556, 8, 9, 0, 123]

45
Q

start at iterating over arrays, http://docs.ruby-lang.org/en/2.0.0/Array.html

A

start at iterating over arrays,http://docs.ruby-lang.org/en/2.0.0/Array.html