Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Look ma' I know my algorithms!

Look ma' I know my algorithms!

RubyConf Argentina 2014

Lucia Escanellas

October 24, 2014
Tweet

Other Decks in Programming

Transcript

  1. def count! combinations = @numbers.combination(2).to_a! ! combinations! .map{ |a,b| a

    + b }! .select do |sum|! sum.abs <= THRESHOLD! end.size! end
  2. Remember: f < g => O(f + g) = O(g)

    O(K . f) = O(f) O(1) < O(ln N) < O(N) < O(N2) < O(eN)
  3. Ex: Binary Search Find 7 in [1, 2, 3, 4,

    5, 6, 7, 8] 1. element in the middle is 5 2. 5 == 7 ? NO 3. 5 < 7 ? YES => Find 7 in [6, 7, 8] Step 1
  4. ! Find 7 in [0, 1, 2, 3, 4, 5,

    6, 7, 8] 1. element in the middle is 7 2. 7 == 7 ? YES! FOUND IT!! Step 2
  5. Typical examples Access to a Hash O(1) Binary search O(log

    N) Sequential search O(N) Traverse a matrix NxN O(N2)
  6. What’s the ORDER of this code? @nums.each_with_index do |a,i|! !

    puts @nums.slice(i+1,N).reverse! .inspect! end
  7. What’s the ORDER of this code? @nums.each_with_index do |a,i|! !

    puts @nums.slice(i+1,N).reverse! .inspect! end Looks like O(N)
  8. What’s the ORDER of this code? @nums.each_with_index do |a,i|! !

    puts @nums.slice(i+1,N).reverse! .inspect! end Behaves like O(N2)
  9. What’s the ORDER of this code? @nums.each_with_index do |a,i|! !

    puts @nums.slice(i+1,N).reverse! .inspect! end O(N2)!