Iterations Flashcards

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

What would the following code show?

basket = [“apple 1”,”apple 2”,”apple 3”,”apple 4”,”apple 5”,”apple 6”,”apple 7”,”apple 8”,”apple 9”,”apple 10”]

basket.each do |apple|
puts “Taking out #{apple}”
end

A
"Taking out apple 1"
"Taking out apple 2"
"Taking out apple 3"
"Taking out apple 4"
"Taking out apple 5"
"Taking out apple 6"
"Taking out apple 7"
"Taking out apple 8"
"Taking out apple 9"
"Taking out apple 10"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between looping and iteration.

A

Looping is a construct that tells your program to run a certain amount of times until a condition has been met.

Iteration is a way to operate on a collection object, like an array and do something with each element in that collection.

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

What would the following code show?

primary_colors = [“Red”, “Yellow”, “Blue”]
primary_colors.each do |color|
puts “Primary Color #{color} is #{color.length} letters long.”
end

A

“Primary Color Red is 3 letters long.”
“Primary Color Yellow is 6 letters long.”
“Primary Color Blue is 4 letters long.”

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

What are | | used for in iterations?

A

| (also known as pipes) are used to hold arguments that are being passed into the iteration.

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

What would the following code look like?

brothers = [“Tim”, “Tom”, “Jim”]
brothers.each do |brother|
puts “Stop hitting yourself #{brother}!”
end

A

“Stop hitting yourself Tim!”
“Stop hitting yourself Tom!”
“Stop hitting yourself Jim!”

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

What is the shorter way of writing the following code using the { } syntax?

brothers = [“Tim”, “Tom”, “Jim”]
brothers.each do |brother|
puts “Stop hitting yourself #{brother}!”
end

A

brothers = [“Tim”, “Tom”, “Jim”]

brothers.each{|brother| puts “Stop hitting yourself #{brother}!”}

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

What is the differences between each iteration and collect/map iteration?

A

If you want the manipulated data returned back to you, use map or collect.

If you want to return the original array, use each.

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

What value will the following code return?

all_odd = [1,3].all? do |number|
number.odd?
end

A

True

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

What value will the following code return?

all_odd = [1,2,3].all? do |number|
number.odd?
end

A

False

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

How would you write an iteration that checks to see if all items are even numbers in the following array?

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

A

array.all? do |number|
number.even?
end

Should return false

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

What value will the following code return?

[1,3].none?{|i| i.even?}

A

True

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

How would you write an iteration that checks to see if none of the items are even numbers in the following array?

array = [1,3,5,7]

A

array.none? do |number|
number.even?
end

Should return true

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

What value will the following code return?

[1,2,100].any?{|i| i > 99}

A

True

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

How would you write an iteration that checks to see if any of the items are greater than 6 in the following array?

array = [1,3,5,7]

A

array.any?{|i| i > 6}

Should return true

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

What value will the following code return?

the_numbers = [4,8,15,16,23,42]
the_numbers.include?(42)

A

True

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

What value will the following code return?

the_numbers = [4,8,15,16,23,42]
the_numbers.include?(6)

A

False

17
Q

What value will the following code return?

[1,2,3,4,5].select do |number|
number.even?
end

A

[2, 4]

18
Q

How would you write an iteration that only selects odd numbers in the following array?

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

A

array.select do |number|
number.odd?
end

Should return [1,3,5]

19
Q

What value will the following code return?

[1,2,3].select{|i| i.is_a?(String)}

A

[ ]

20
Q

What value will the following code return?

[1,2,3].detect{|i| i.odd?}

A

1

21
Q

What value will the following code return?

[1,2,3].find{|i| i.even?}

A

2

22
Q

True or False: detect/find method will return the first value that meets the condition that is given.

A

True

23
Q

What value will the following code return?

[1,2,3,4].detect{|i| i.even?}

A

2

24
Q

What value will the following code return?

[1,2,3,4].detect{|i| i.is_a?(String)}

A

nil

25
Q

What value will the following code return?

[1,2].reject{|i| i.even?}

A

[1]

26
Q

How would you write an iteration that rejects all odd numbers in the following array?

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

A

array.reject{|i| i.odd?}

Should return [2,4]

27
Q

How would you sort an array of numbers? (There are two ways)

array = [7,3,1,2,6,5,4]

A
array.sort do |a, b|
  if a == b
    0
  elsif a < b
    -1
  elsif a > b
    1
  end
end

OR

array.sort do |a, b|
a <=> b
end

28
Q

How would you sort an array of strings by the amount of characters they have? (There are two ways)

array=[“love”, “Richard”, “I”]

A
array.sort do |a, b|
  if a.length == b.length
    0
  elsif a.length < b.length
    -1
  elsif a.length > b.length
    1
  end
end

OR

array.sort do |a, b|
a.length <=> b.length
end

29
Q

What will the following code show?

array=[“Chico”, “Momi”, “Kehau”, “Tomo”, “Rusty”]
array.each_with_index do |name, index|
puts “#{name} is at index #{index} of the array”
end

A
"Chico is at index 0 of the array"
"Momi is at index 1 of the array"
"Kehau is at index 2 of the array"
"Tomo is at index 3 of the array"
"Rusty is at index 4 of the array"