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.

Joshua Silver

October 17, 2015
Tweet

More Decks by Joshua Silver

Other Decks in Technology

Transcript

  1. Agenda • About me • Approach • Deep dives: –

    Regex – Finite State Machines – Graph Theory • Overview of other topics
  2. 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
  3. 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
  4. Typical Uses of Regex • Pattern matching within strings •

    Find & replace • Input Validation • Text filtering
  5. $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.”;
  6. 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.
  7. $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.”;
  8. $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
  9. (?:(?:\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
  10. 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
  11. 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$$!!
  12. 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
  13. 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
  14. <?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";
  15. 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; }
  16. 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"); }
  17. 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”; }
  18. public function ship() { if ($this->getStatus() !== self::ORDER_STATUS_READYTOSHIP) { handleError("ERROR:

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

    echo $o1->getStatus(); -------- Confirmed Ready To Ship Completed
  20. 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
  21. 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
  22. What’s a graph? • Set of vertices (the nodes) •

    Set of edges connecting nodes – Can be directed or undirected – Can optionally be weighted
  23. 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
  24. 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!
  25. 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.
  26. Dijkstra's algorithm • Finds the shortest path between nodes in

    a graph – Directions between cities – Finding cheapest flight – Network packet routing
  27. 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
  28. 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