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

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

Embed Size (px)

Citation preview

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

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

Tony Tran

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

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

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

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

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

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

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

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)

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

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)

=)

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

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

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

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

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

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

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

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)

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

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)

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

How does this apply to programming?

Abstraction!

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

How does this apply to programming?

Abstraction!

Programming Problem --> Real Life Problem

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

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

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

Fans of abstraction

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

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

“[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!

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

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

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

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

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

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!

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

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