Assessment01 review Flashcards
1
Q
factors algorithm
A
def factors(num) (1..num).select { |i| (num % i) == 0 } end
2
Q
Explain Array#take
A
Retrieves the first n numbers from an array
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
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