The best programmers I’ve met knew how to sort a deck of cards pretty D*mn well

Preview:

Citation preview

The best programmers I’ve meet knew how to sort a deck of cards pretty d*mn well

Tony Tran

Image source: http://www.flickr.com/photos/60601862@N00/942476116/

What if…

Given: Deck of playing cardsGoal: To retrieve any card as fast as possible!Constraints: No real constraints, do whatever you want with the cards! Just don’t be a smart @s*!

WHAT WOULD YOU DO!?!

Find me:

Approaches:1. Do nothing to the deck (Cost: 52 lookups)

Cost is analyzed in worst case scenario."Anything that can go wrong will go wrong.“ – Murphy’s Law

Approaches:1. Do nothing to the deck (Cost: 52 lookups)

2. Group cards by suits (Cost: 4+12 = 16 lookups)

3. Group cards by faces (Cost: 12+4 = 16 lookups)

Approaches:1. Do nothing to the deck (Cost: 52 lookups)

2. Group cards by suits (Cost: 4+12 = 16 lookups)

3. Group cards by faces (Cost: 12+4 = 16 lookups)

=)

Image source: http://www.eyebrowmagazine.com/__data/assets/image/0010/10045/megan_fox_naked_5.jpg

What if…

Given: 99 note cards with a unique number written on each card.Goal: To retrieve any card as fast as possible!Constraints: No real constraints, do whatever you want with the cards! Just don’t be a smart @s*!

WHAT WOULD YOU DO!?!

Find me:

Approaches:1. Do nothing to the deck (Cost: 99 lookups)2. Create bins for groups of 100 and just throw the cards into the bin.

Example:

Cost is analyzed in worst case scenario."Anything that can go wrong will go wrong.“ – Murphy’s Law

bins

(Cost: 99 lookups)When given cards:1,2,3,…,99

Find me: Approaches:

1. Do nothing to the deck (Cost: 99 lookups)2. Create bins for groups of 100 and just throw the cards into the bin. (Cost: 99 lookups)

3. Sort the deck! Then perform binary search to find card.

(cost: log2(99) lookups ~= 7 look ups)

Cost is analyzed in worst case scenario."Anything that can go wrong will go wrong.“ – Murphy’s Law

Depiction of binary search (does not show entire search)

Find me: Approaches:

1. Do nothing to the deck (Cost: 99 lookups)2. Create bins for groups of 100 and just throw the cards into the bin. (Cost: 99 lookups)

3. Sort the deck! Then perform binary search to find card.

(cost: log2(99) lookups ~= 7 look ups)

Cost is analyzed in worst case scenario."Anything that can go wrong will go wrong.“ – Murphy’s Law

Depiction of binary search (does not show entire search)

How does this apply to programming?

Abstraction!

How does this apply to programming?

Abstraction!

Programming Problem --> Real Life Problem

Key point #1:1. Being able to abstract a problem to something less complex, or more familiar, will make the problem easier!

Image source: http://www.scientificamerican.com/media/inline/3E0F9160-E7F2-99DF-358998AA3C1A910F_1.jpg

“the purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise.” - Edsger W. Dijkstra

Fans of abstraction

What is the shortest path from 1 to any other node?

“[OSPF] computes the shortest path tree for each route using a method based on Dijkstra's algorithm, a shortest path first algorithm.”

INTERNET!!!

Keypoint #2:Abstracting a problem to a problem that has already been solved is even better! You don’t really have to do any work!

Keypoint #33. There’s no such thing as a dumb problem

Image source: http://sfist.com/attachments/SFist_Brock/pancakes.jpg

Problem: Given a spatula and a dish of pancakes (say 6 for now), how wouldyou flip the pancakes such that the pancakes become sorted with the largest pancake at the bottom and smallest at the top? How many steps would it require?

FLIP

Initial State: GOAL

Someone familiar:

Bill Gates & Christos Papadimitriou, Bounds For Sorting By Prefix Reversal. Discrete Mathematics, vol 27, pp 47-57, 1979.

We referred to bounds as “Costs”

SORTING PANCAKES == SORTING BY PREFIX REVERSAL

Key Points:1. Being able to abstract a problem to something less complex, or more familiar, will make the problem easier!2. Abstracting a problem to a problem that has already been solved is even better!3. There’s no such thing as a dumb problem.

4. Be creative and have fun with problems. You have the ability to create an entire world at your fingertips!

The best programmers I’ve meet knew how to ABSTRACT PROBLEMS pretty d*mn well

“The hardest part of solving a problem is to completely understand it toa point where you can see it from many different perspectives and abstractions”-A very wise T.A.

-THANK YOU-

New title proposal:

P.S. My gf will probably be reading this, so the story at the beginning didn’t really happen 0-=) But really, it didn’t =)

Thank you,-Tony Tran@quicksorter

Recommended