Slide 1

Slide 1 text

Practical Computer Science Concepts Simplified Joshua Silver VP, Product Development Patientco

Slide 2

Slide 2 text

Housekeeping This is YOUR time, so: –  Try another session if this one isn’t for you. Please REVIEW + COMMENT https://joind.in/13719

Slide 3

Slide 3 text

Agenda •  About me •  Approach •  Deep dives: – Regex – Finite State Machines – Graph Theory •  Overview of other topics

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Let’s Go!

Slide 7

Slide 7 text

“Don’t reinvent the wheel”

Slide 8

Slide 8 text

“Don’t reinvent the wheel” Only works if you know the wheel exists!

Slide 9

Slide 9 text

Let’s explore some wheels…

Slide 10

Slide 10 text

Regular Expressions ‘Regex’

Slide 11

Slide 11 text

Typical Uses of Regex •  Pattern matching within strings •  Find & replace •  Input Validation •  Text filtering

Slide 12

Slide 12 text

Regex Example Validate a US phone number. Acceptable format: 000-000-0000

Slide 13

Slide 13 text

Regex Example Validate a US phone number. Acceptable formats include: 000-000-0000 (000) 000-0000

Slide 14

Slide 14 text

! $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.”;!

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

! $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.”;!

Slide 17

Slide 17 text

$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! !

Slide 18

Slide 18 text

(?:(?:\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  

Slide 19

Slide 19 text

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/

Slide 20

Slide 20 text

Finite State Machines

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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$$!!

Slide 25

Slide 25 text

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  

Slide 26

Slide 26 text

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  

Slide 27

Slide 27 text

Slide 28

Slide 28 text

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;! }! !

Slide 29

Slide 29 text

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");! }! !

Slide 30

Slide 30 text

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”;! }! ! !

Slide 31

Slide 31 text

! public function ship() {! if ($this->getStatus() !== self::ORDER_STATUS_READYTOSHIP) {! die("ERROR: Can't ship order - INVALID STATUS\n");! }! $this->timeShipped = time();! }!

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

$o2 = new Order();! echo $o2->getStatus();! $o2->ship();! echo $o2->getStatus();! ! --------! Confirmed! ERROR: Can't ship order- INVALID STATUS!

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Graph Theory

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

What’s a graph? •  Set of vertices (the nodes) •  Set of edges connecting nodes –  Can be directed or undirected –  Can optionally be weighted

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Graphs are hard easy 1.  Determine if the problem is really a graph problem 2.  Determine appropriate algorithm 3.  Implement algorithm properly

Slide 44

Slide 44 text

Trees •  Directed, connected, acyclic graph •  Example: –  Binary Search Tree

Slide 45

Slide 45 text

Dijkstra's algorithm •  Finds the shortest path between nodes in a graph –  Directions between cities –  Finding cheapest flight –  Network packing routing

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Social Graph Searching

Slide 50

Slide 50 text

Social Graph Searching

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Questions? [email protected] http://www.PatientcoLife.com/ https://joind.in/13719