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

Practical Computer Science Concepts Simplified

Practical Computer Science Concepts Simplified

I often see developers (especially self-taught developers) trying to "reinvent the wheel" because they are not familiar with some key computer science concepts. In this talk, I'll give you the Cliffs notes version of a CS degree, focusing primarily on the highly relevant and immediately usable principles. Sample topics include: object-oriented design, graph theory, state diagrams, regular expressions, and complexity theory. To get the most out of this talk, you should have a good working knowledge of PHP, but no formal computer science education is required.

Joshua Silver

May 21, 2015
Tweet

More Decks by Joshua Silver

Other Decks in Programming

Transcript

  1. Housekeeping This is YOUR time, so: –  Try another session

    if this one isn’t for you. Please REVIEW + COMMENT https://joind.in/13719
  2. Agenda •  About me •  Approach •  Deep dives: – Regex

    – Finite State Machines – Graph Theory •  Overview of other topics
  3. About Me •  CS degree from GA Tech •  Helped

    internationalize CareerBuilder.com platform •  Technical co-founder of Patientco •  Fun Fact: –  Travel junkie: 25 countries by the time I was 25
  4. About Patientco •  Simplifies patient billing for healthcare providers • 

    Bills that patients can actually understand! •  Founded in 2009 in Atlanta •  Inc. 500 Fastest Growing Company •  We’re hiring: http://www.patientcolife.com
  5. Typical Uses of Regex •  Pattern matching within strings • 

    Find & replace •  Input Validation •  Text filtering
  6. ! $phoneNumbers = array(! "202-555-1234", "1-789-555-0281", "(770) 555-7422", ! "789

    555-7898", "555-8790", "890-000-233", ! "(789 029-8293", "903.555.1093" );! ! $regex = '/^(\d{3}\-|\(\d{3}\) )[0-9]{3}-\d{4}$/';! ! foreach ($phoneNumbers as $phoneNumber)! if (preg_match($regex, $phoneNumber) === 1)! echo $phoneNumber . ” is valid.";! else! echo $phoneNumber . ” is NOT valid.”;!
  7. Output:! ! 202-555-1234 is valid.! 1-789-555-0281 is NOT valid.! (770)

    555-7422 is valid.! 789 555-7898 is NOT valid.! 555-8790 ! is NOT valid.! 890-000-233 is NOT valid.! (789 029-8293 is NOT valid.! 903.555.1093 is NOT valid.!
  8. ! $phoneNumbers = array(! "202-555-1234", "1-789-555-0281", "(770) 555-7422", ! "789

    555-7898", "555-8790", "890-000-233", ! "(789 029-8293", "903.555.1093" );! ! $regex = '/^(\d{3}\-|\(\d{3}\) )[0-9]{3}-\d{4}$/';! ! foreach ($phoneNumbers as $phoneNumber)! if (preg_match($regex, $phoneNumber) === 1)! echo $phoneNumber . ” is valid.";! else! echo $phoneNumber . ” is NOT valid.”;!
  9. $regex = '/^(\d{3}\-|\(\d{3}\) )[0-9]{3}-\d{4}$/';! ! ^ start of string! (!

    \d{3}\- exactly 3 digits, followed by a -! | OR! \(\d{3}\) open paren, 3 digits, close paren! )! [0-9]{3} alternative notation for 3 digits! -! \d{4} exactly 4 digits! $ end of string! !
  10. (?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]! )+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:! \r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(!

    ?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ ! \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0! 31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\! ](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+! (?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:! (?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z! |(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)! ?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\! r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[! \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)! ?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]! )*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[! \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*! )(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]! )+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)! *:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+! Regexes  can  get  CRAZY.     Don’t  do  this.   **  FROM:    h;p://www.ex-­‐parrot.com/~pdw/Mail-­‐RFC822-­‐Address.html  
  11. Typical Uses of State Machines •  Business rules requiring flows

    –  Order management –  Payment Processing •  Complex parsing applications •  Embedded systems •  Background-type jobs (“servers”) •  Many game engines
  12. How to implement? 1.  Draw a state machine diagram 2. 

    Convert to variable-based state table 3.  Implement getStatus() in code 4.  Implement transition functions (with error checking) in code 5.  PROFIT$$!!
  13. Order Management System Confirmed   Cancelled   On  Hold  

    Completed   Ready  to  ship   Order     Created   Order   Cancelled     Card   Declined   Order   Cancelled     Card     Successful   Card     Successful   Order     Shipped   Card  Declined  
  14. Order Management System Status timeCancelled timeShipped lastBillingOutcome Confirmed NULL NULL

    NULL Ready to ship NULL NULL SUCCESS Completed NULL NOT NULL SUCCESS Cancelled NOT NULL NULL -- On Hold NULL NULL DECLINED Confirmed   Cancelled   On  Hold   Completed   Ready  to  ship  
  15. <?php! class Order {! private $timeCreated;! private $timeCancelled;! private $timeShipped;!

    private $lastBillingOutcome;! ! const ORDER_STATUS_CONFIRMED = "Confirmed";! const ORDER_STATUS_READYTOSHIP = "Ready To Ship";! const ORDER_STATUS_COMPLETED = "Completed";! const ORDER_STATUS_CANCELLED = "Cancelled";! const ORDER_STATUS_ONHOLD = "On Hold";! !
  16. public function getStatus() {! if ($this->timeCancelled === NULL &&! $this->timeShipped

    === NULL &&! $this->lastBillingOutcome === NULL){! return self::ORDER_STATUS_CONFIRMED;! } ! ! if ($this->timeCancelled === NULL &&! $this->timeShipped === NULL &&! $this->lastBillingOutcome === "SUCCESS"){! return self::ORDER_STATUS_READYTOSHIP;! }! !
  17. public function getStatus() {! . . .! if ($this->timeCancelled !==

    NULL &&! $this->timeShipped === NULL){! return self::ORDER_STATUS_CANCELLED;}! ! if ($this->timeCancelled === NULL &&! $this->timeShipped === NULL &&! $this->lastBillingOutcome === "DECLINED"){! return self::ORDER_STATUS_ONHOLD;}! ! die("ERROR: INVALID STATUS\n");! }! !
  18. public function chargeCard() {! if ($this->getStatus() !== self::ORDER_STATUS_CONFIRMED ! &&

    $this->getStatus() !== ORDER_STATUS_ONHOLD) {! die("ERROR: Can't charge card - INVALID STATUS\n");! }! ! if (wasCardChargeSuccessful() === true)! $this->lastBillingOutcome = "SUCCESS";! else! $this->lastBillingOutcome = "DECLINED”;! }! ! !
  19. ! public function ship() {! if ($this->getStatus() !== self::ORDER_STATUS_READYTOSHIP) {!

    die("ERROR: Can't ship order - INVALID STATUS\n");! }! $this->timeShipped = time();! }!
  20. $o1 = new Order();! echo $o1->getStatus();! $o1->chargeCard();! echo $o1->getStatus();! $o1->ship();!

    echo $o1->getStatus();! ! --------! Confirmed! Ready To Ship! Completed!
  21. $o2 = new Order();! echo $o2->getStatus();! $o2->ship();! echo $o2->getStatus();! !

    --------! Confirmed! ERROR: Can't ship order- INVALID STATUS!
  22. Why use a state machine? •  Business logic self-contained and

    can be centralized –  e.g. If rules for when an order is ready to ship ever change, only need to change a single function. •  Complex rules can be implemented simply and compactly •  Easier to test
  23. Related to regex? •  Regular Expressions are a specific case

    of state machine •  EVERY regex can be converted to a state machine •  State machines can also represent much more complex grammars
  24. What’s a graph? •  Set of vertices (the nodes) • 

    Set of edges connecting nodes –  Can be directed or undirected –  Can optionally be weighted
  25. Typical Uses of Graphs •  More than just “Traveling salesman

    problem” •  Job/resource scheduling •  Navigation, maps •  Ordering links (eg. Google ranking) •  Social graph analysis (eg. Facebook, LinkedIn) •  Matching (eg. Dating app) •  Indexing, searching
  26. Graphs are hard easy 1.  Determine if the problem is

    really a graph problem 2.  Determine appropriate algorithm 3.  Implement algorithm properly
  27. Dijkstra's algorithm •  Finds the shortest path between nodes in

    a graph –  Directions between cities –  Finding cheapest flight –  Network packing routing
  28. Dijkstra's algorithm SFO   DFW   ORD   NYC  

    ATL   70   180   100   30   40   80   80  
  29. Dijkstra's algorithm SFO   DFW   ORD   NYC  

    ATL   70   180   100   30   40   80   80  
  30. Matching •  Bipartite graphs used for matching –  Match boys

    / girls on a dating site –  Match medical students & residency programs •  Implementation –  Max-flow min-cut theorem –  Hall's marriage theorem –  König's theorem
  31. Other common “reinventions” •  Mutexes, locks, semaphores –  Manage exclusive

    access of a resource •  Queues, Priority Queues –  Don’t put them in a DB!!! –  Any tasks that can be done asynchronously •  Caching –  Don’t roll your own –  Cache invalidation