Assessment01 review Flashcards

1
Q

factors algorithm

A
def factors(num)
  (1..num).select { |i| (num % i) == 0 }
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain Array#take

A

Retrieves the first n numbers from an array

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

bubble sort algorithm

A
def bubble_sort!(&prc)
    # See how I create a Proc if no block was given; this eliminates
    # having to later have two branches of logic, one for a block and
    # one for no block.
    prc = Proc.new { |x, y| x  y } if prc.nil?
sorted = false
until sorted
  sorted = true

  (0...(count - 1)).each do |i|
    if prc.call(self[i], self[i + 1]) == 1
      # Parallel assignment; use it when swapping.
      self[i], self[i + 1] = self[i + 1], self[i]
      sorted = false
    end
  end
end

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

Fibonacci recursive algorithm

A
def fibs_rec(count)
  # If you didn't get fibs_rec right, step through my version in the
  # debugger. You need to know how this works *cold*.
  return [0, 1].take(count) if count << next_fib

first_fibs
end

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