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

Computer Science Fundamentals for Self-Taught P...

Computer Science Fundamentals for Self-Taught Programmers by Justin Abrahms

PyCon 2014

April 11, 2014
Tweet

More Decks by PyCon 2014

Other Decks in Programming

Transcript

  1. Who am I? • Justin Abrahms • Director of Product

    Engineering at Quick Left • Author of Imhotep • @justinabrahms or github://justinabrahms
  2. Overview • How I learn about Big O? • What

    is Big O? • How is Big O done? • Resources and learned wisdom
  3. What does N+1 Selects look like? entries = get_post_ids() final_list

    = [] for entry_id in entries: entry = get_entry(entry_id) final_list.append(entry)
  4. Questions • How did I miss this? • How did

    this guy know about it and I didn’t? • How can I make sure this never happens again.
  5. –Wikipedia (aka Inducer of Impostor Syndrome) In mathematics, big O

    notation describes the limiting behavior of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions.
  6. –Me Big O is how programmers talk about the relation

    of how much stuff is being done when comparing two pieces of code.
  7. Google Study List Studied: • Data Structures • Algorithms •

    System Design • Java Internals • Concurrency Issues
  8. Google Study List Studied: • Data Structures! • Algorithms! •

    System Design • Java Internals • Concurrency Issues These are Big O things
  9. –Wikipedia A data structure is a particular way of storing

    and organizing data in a computer so that it can be used efficiently.
  10. What sorts of Big O are there? • O(1) —

    Constant Time • O(log n) — Logarithmic Time • O(n) — Linear Time • O(n²) — Quadratic Time • O(n!) — Factorial Time
  11. def item_in_list(item, lst): for entry in lst: if entry ==

    item: return True return False O(n) — Broken Down
  12. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: return True return False O(n) — Broken Down
  13. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: O(1) return True return False O(n) — Broken Down
  14. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: O(1) return True O(1) return False O(1) O(n) — Broken Down
  15. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: O(1) return True O(1) return False O(1) O(n) — Broken Down =O(n) * O(1) + O(1)
  16. In non-“math-y” terms • If we plot our function, we

    can also plot M * the big O and end up with a line that our function never crosses (for certain values of X)
  17. Example O(n) * O(1) + O(1) Big O: To Plot:

    x * ? O(n) always means x
  18. Example O(n) * O(1) + O(1) Big O: To Plot:

    x * 5 + 9 O(1) means pick any constant number
  19. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 6? Nope.
  20. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 3? Nope.
  21. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 4? Yes!
  22. def get_pairs(lst): pair_list = [] for i1 in lst: for

    i2 in lst: pair_list.append([i1, i2]) return pair_list ! O(n²)
  23. def get_pairs(lst): pair_list = [] O(1) for i1 in lst:

    O(N) for i2 in lst: O(N) pair_list.append([i1, i2]) O(1) return pair_list O(1) ! O(n²)
  24. def get_pairs(lst): pair_list = [] O(1) for i1 in lst:

    O(N) for i2 in lst: O(N) pair_list.append([i1, i2]) O(1) return pair_list O(1) ! O(n²) = O(1) + O(n) * O(n) * O(1) + O(1)
  25. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1) = O(n) * O(n) * O(1) + O(1) + O(1)
  26. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1) = O(n) * O(n) * O(1) + O(1) + O(1) = x * x + 7 + 9 + 13
  27. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1) = O(n) * O(n) * O(1) + O(1) + O(1) = x * x + 7 + 9 + 13 = x² + 29
  28. Big O is… • useful in communicating about complexity of

    code • basic arithmetic and algebra • used in talking about algorithms and data structures • not as hard as it originally sounds