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 trying to "reinvent the wheel" because they are not familiar with some key Computer Science concepts (especially among self-taught developers). In this talk, I'll give you the cliff 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

October 17, 2015
Tweet

Transcript

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

    Patientco
  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.complatform • 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. Let’s Go!

  6. “Don’t reinvent the wheel”

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

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

  9. Regular Expressions ‘Regex’

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

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

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

    000-000-0000 (000) 000-0000
  13. $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.”;
  14. 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.
  15. $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.”;
  16. $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
  17. (?:(?:\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:    http://www.ex-­‐parrot.com/~pdw/Mail-­‐RFC822-­‐Address.html
  18. 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/

  19. Finite State Machines

  20. State Machine Example From:  http://en.wikipedia.org/wiki/Finite-­‐state_machine

  21. None
  22. 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
  23. 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$$!!
  24. 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
  25. 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
  26. <?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";
  27. 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; }
  28. 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;} handleError("ERROR: INVALID STATUS\n"); }
  29. public function chargeCard() { if ($this->getStatus() !== self::ORDER_STATUS_CONFIRMED && $this->getStatus()

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

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

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

    Confirmed ERROR: Can't ship order- INVALID STATUS
  33. 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
  34. 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
  35. 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

  36. Graph Theory

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

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

    really a graph problem 2. Determine appropriate algorithm 3. Implement algorithm properly 4. TEST! PATTERN RECOGNITION!
  43. Trees • Directed, connected, acyclic graph • Example: – Binary

    Search Tree
  44. Flow • Directed, connected, weighted graph • Flows from a

    source (s) to a sink (t)
  45. Bipartite Graphs • Vertices partitioned into 2 sets. Each edge

    has one endpoint in each set. • Concepts often used: – Vertex cover (node cover) - set of vertices that includes at least one endpoint of each edge – minimum vertex cover - no other vertex cover has fewer vertices.
  46. Dijkstra's algorithm • Finds the shortest path between nodes in

    a graph – Directions between cities – Finding cheapest flight – Network packet routing
  47. Dijkstra's algorithm SFO DFW ORD NYC ATL 70 180 100

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

    30 40 80 80
  49. 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
  50. Social Graph Searching

  51. Social Graph Searching

  52. 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

  53. 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
  54. Questions? joshua.silver@patientco.com http://www.PatientcoLife.com/