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.

12ab843bf4b1533e3d7897721e583dfd?s=128

Joshua Silver

May 21, 2015
Tweet

Transcript

  1. Practical Computer Science Concepts Simplified Joshua Silver VP, Product Development

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

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

    – Finite State Machines – Graph Theory •  Overview of other topics
  4. 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
  5. 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
  6. Let’s Go!

  7. “Don’t reinvent the wheel”

  8. “Don’t reinvent the wheel” Only works if you know the

    wheel exists!
  9. Let’s explore some wheels…

  10. Regular Expressions ‘Regex’

  11. Typical Uses of Regex •  Pattern matching within strings • 

    Find & replace •  Input Validation •  Text filtering
  12. Regex Example Validate a US phone number. Acceptable format: 000-000-0000

  13. Regex Example Validate a US phone number. Acceptable formats include:

    000-000-0000 (000) 000-0000
  14. ! $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.”;!
  15. 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.!
  16. ! $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.”;!
  17. $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! !
  18. (?:(?:\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  
  19. Resources General info: http://en.wikipedia.org/wiki/Regular_expression http://php.net/manual/en/function.preg-match.php http://www.regular-expressions.info/ Online Regex Tester: https://regex101.com/

  20. Finite State Machines

  21. State Machine Example From:  h;p://en.wikipedia.org/wiki/Finite-­‐state_machine  

  22. None
  23. 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
  24. 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$$!!
  25. 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  
  26. 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  
  27. <?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";! !
  28. 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;! }! !
  29. 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");! }! !
  30. 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”;! }! ! !
  31. ! public function ship() {! if ($this->getStatus() !== self::ORDER_STATUS_READYTOSHIP) {!

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

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

    --------! Confirmed! ERROR: Can't ship order- INVALID STATUS!
  34. 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
  35. 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
  36. Resources General info: http://en.wikipedia.org/wiki/Automata_theory http://www.i-programmer.info/babbages-bag/223- finite-state-machines.html http://en.wikipedia.org/wiki/Finite-state_machine

  37. Graph Theory

  38. None
  39. None
  40. None
  41. What’s a graph? •  Set of vertices (the nodes) • 

    Set of edges connecting nodes –  Can be directed or undirected –  Can optionally be weighted
  42. 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
  43. Graphs are hard easy 1.  Determine if the problem is

    really a graph problem 2.  Determine appropriate algorithm 3.  Implement algorithm properly
  44. Trees •  Directed, connected, acyclic graph •  Example: –  Binary

    Search Tree
  45. Dijkstra's algorithm •  Finds the shortest path between nodes in

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

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

    ATL   70   180   100   30   40   80   80  
  48. 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
  49. Social Graph Searching

  50. Social Graph Searching

  51. Resources General info: http://en.wikipedia.org/wiki/Graph_(mathematics) http://en.wikipedia.org/wiki/Graph_traversal http://en.wikipedia.org/wiki/Category:Graph_algorithms

  52. 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
  53. Questions? joshua.silver@patientco.com http://www.PatientcoLife.com/ https://joind.in/13719