Talk presented to High School students at the Utah DigiForge Student Conference: "Think Like A Programmer". (Attributes: Laziness, Impatience, Hubris. And how algorithms, abstraction, and generalization will help you to achieve those attributes.)
15. Who am I?
● Dave Oswald – A Propetual Hobbiest.
16. Who am I?
● Dave Oswald – A Propetual Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, and self-guided.
17. Who am I?
● Dave Oswald – A Propetual Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, and self-guided.
● Independent software developer and consultant.
– Focus on Perl, C++, and server-side development.
18. Who am I?
● Dave Oswald – A Propetual Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, and self-guided.
● Independent software developer and consultant.
– Focus on Perl, C++, and server-side development.
● Solving problems is my hobby. Surprisingly, people pay
me to do it.
19. Who am I?
● Dave Oswald – A Propetual Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, and self-guided.
● Independent software developer and consultant.
– Focus on Perl, C++, and server-side development.
● Solving problems is my hobby. Surprisingly, people pay me to do it.
● daoswald@gmail.com
● Salt Lake Perl Mongers
– http://saltlake.pm.org
22. Our Goal
● Learn programming in one hour.
● Sit around and tweet / text / play Temple Run
for one hour.
23. Our Goal
● Learn programming in one hour.
● Sit around and tweet / text / play Temple Run
for one hour.
24. Our Goal
● Learn programming in one hour.
● Sit around and tweet / text / play Temple Run
for one hour.
● Get out of class for four hours.
25. Our Goal
● Learn programming in one hour.
● Sit around and tweet / text / play Temple Run
for one hour.
● Get out of class for four hours.XXXXXXXXXXXXXXXXXXXXX
26. Our Goal
● Learn programming in one hour.
● Sit around and tweet / text / play Temple Run
for one hour.
● Get out of class for four hours.XXXXXXXXXXXXXXXXXXXXX
● Learn simple thought processes useful to
programmers.
27. Our Goal
● Learn programming in one hour.
● Sit around and tweet / text / play Temple Run
for one hour.
● Get out of class for four hours.XXXXXXXXXXXXXXXXXXXXX
●
Learn simple thought processesLearn simple thought processes
useful to programmers.useful to programmers.
39. The Three Virtues...of Programmers
● Laziness
● Impatience
– The anger you feel
when the computer
is being lazy, which
happens when
another
programmer is not
lazy.
41. The Three Virtues...of Programmers
● Laziness
● Impatience
● Hubris
– The pride that
makes you write
and maintain
programs that you
can be proud of,
and that your
peers will admire.
42. The Three Virtues...of Programmers
● Laziness
– ...makes you go to great effort to reduce
your overall energy expenditure.
– …makes you write robust, modular, well-
documented programs so you can reuse
[the effort].
● Impatience
– ...anger you feel when the computer is being lazy, which
happens when another programmer is not lazy.
– ...makes you write programs that use minimal code so they’re
fast, efficient, and anticipate what needs to be done.
● Hubris
– ...pride that makes you write
and maintain programs that you
and your peers will admire.
– ...uncontrolled or undeserved, it
can also get you in trouble.
43.
44. So... Who am I?
● Dave Oswald – Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, Independent.
● Independent developer and consultant.
– Focus on Perl, C++, and server-side development.
● Solving problems is my hobby. Surprisingly, people pay me to do it.
● daoswald@gmail.com
● Salt Lake Perl Mongers
– http://saltlake.pm.org
45. I am...
● Dave Oswald – Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, Independent.
● Independent developer and consultant.
– Focus on Perl, C++, and server-side development.
● Solving problems is my hobby. Surprisingly, people pay me to do it.
● daoswald@gmail.com
● Salt Lake Perl Mongers
– http://saltlake.pm.org
● Aspiring to be:
– Lazy
– Impatient
– Hubristic
47. Abstraction
● An emphasis on the idea, qualities and
properties rather than the particulars (a
suppression of detail).
48. Abstraction
● An emphasis on the
idea, qualities and
properties rather than
the particulars (a
suppression of detail).
● Goat
– Small Livestock.
– Horns.
– Horizontal Pupils.
– Eat almost anything.
– Curious.
– Used in Production of
Milk, Cheese, Meat.
49. My abstraction: Dave Oswald
● Hobbiest.
● Studied Economics and Computer Science at U of U.
– Also CS in High School, SLCC, LAVC, Independent.
● Independent developer and consultant.
– Focus on Perl, C++, and server-side development.
● Solving problems is my hobby. Surprisingly, people pay me to do it.
● daoswald@gmail.com
● Aspiring to be:
– Lazy
– Impatient
– Hubristic
50. “All problems in computer science can be solved
by another level of indirection [or abstraction]...”
– David Wheeler
52. Generalization
● A broadening of
application [or tool] to
encompass a larger
domain of objects of
the same or different
type.
● Dremel Tool
– Spins a bit receptical
really fast.
– Has a hand grip.
53. Generalization
● A broadening of
application to
encompass a larger
domain of objects of
the same or different
type.
● Dremel Tool
– Spins a bit receptical
really fast.
– Has a hand grip.
67. Rules
● I choose a random number between 1 and 100.
● You try to guess the number by asking, “Is it
X?”
● I respond with either “Yes”, “Higher”, or “Lower”.
68. How would you solve it with pencil and paper?How would you solve it with pencil and paper?
70. Rules
● I choose a random
number between 1
and 100.
● You try to guess the
number by asking,
“Is it X?”
● I respond with either
“Yes”, “Higher”, or
“Lower”.
● Step 1:
– Ask is it X?
● Step 2:
– ??????
71. The Blind Guess Algorithm
● Steps
– Call one person on the phone. Person guesses 23.
● Higher.
● Hang up.
– Call another person. Ask, “Is it 19?”
● Higher.
● Hang up.
● Worst case: It could take an infinite amount of guesses.
● There's only a 63% chance that it will be guessed within
100 tries.
● There's a 1% chance that it will not be guessed even after
450 tries.
72. The brute force algorithm.
● Steps
– Is it 1?
● Higher.
– Is it 2?
● Higher.
– Is it 3?
● Higher.
– Is it 4?
● Higher
● Worst case: Linear.
73. Divide and Conquer
● Is it 50?
– Lower
● Is it 25?
– Higher
● Is it 38?
– Lower.
● Each question bisects the remaining range.
● Each answer constrains the remaining range.
74. Divide and Conquer
● Secret number is between 1 and 10: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
– Is it 5?
● Higher.
● The secret is between 6 and 10: [ 6, 7, 8, 9, 10 ]
– Is it 8?
● Lower.
● The secret is between 6 and 7: [ 6, 7 ]
– Is it 6?
● Higher.
●
The secret number is between 7 and 7: [ 7 ]
– Is it 7?
● Yes.
75. How good is divide and conquer?
● Order of growth:
– As 'n' (the size of the data set) grows toward infinity,
queries needed to find an item grows at log2
(n).
● log2
(n), rounded up to nearest integer:
– n = 100, guesses = 7
– n = 1,000, guesses = 10.
– n = 1,000,000, guesses = 20.
– n = 1,000,000,000, guesses = 30.
– n = 1,000,000,000,000, guesses = 40.
– n = 1 Googol (10e100, or ten duotrigintillion), guesses = 336.
76. Putting it in perspective
● A computer can do 338 guesses 100 times for
each time your heart beats.
77. Putting it in perspective
● A computer can do 338 guesses 100 times for
each time your heart beats.
– A totally made-up statistic.
78. Putting it in perspective
● A computer can do 338 guesses 100 times for
each time your heart beats.
– A totally made-up statistic.
● 1.33e80 atoms in the universe.
– 266 guesses.
– ( If only the atoms
were all lined up
in order. )
79. Generalization
● “Divide and Conquer” number
guessing can be generalized.
– Is “oxter” is in the dictionary?
– Search 171,476 words in the
Oxford dictionary.
● 18 divide and conquer
steps.
80. Generalization
● “Divide and Conquer” number
guessing can be generalized.
– Is “oxter” is in the dictionary?
– Search 171,476 words in the
Oxford dictionary
● 18 divide and conquer
steps.
● n – Outdated word
meaning “armpit”.
● “Oxter” is in the dictionary.
81. Generalization
● “Divide and Conquer” number
guessing can be generalized.
– Find if “oxter” is in the
dictionary.
– Find if we have a 19 year old
on the roster.
– Find the address from John
Doe's directory listing.
– ...and about a million other
uses.
● “Binary Search”
82. A Generalized Binary Search
●
found = binary_search( [target], [big_list], [comparator] )
● What does our black-box Binary Search need?
– Something to find: a target.
– Somewhere to find it: a big list.
– Some means of comparing items: a comparator.
83. What is a comparator?
● Another black box.
– comparator( a, b )
● Return -1 if a < b
● Return 1 if b < a
● Otherwise return 0.
● Dan < David
– D == D
– a == a
– n < v
● 7 > 5
● 2.71828 < 3.14151
84. A Generalized Binary Search
●
found = binary_search( [target], [big_list], [how_to_compare] )
● What does our black-box Binary Search need?
– Something to find: a target.
– Somewhere to find it: a sorted big list.
– Some means of comparing items: a comparator.
● An item comparer black-box.
sub comparator {
my( $a, $b ) = ( shift, shift );
return $a <=> $b; # ( returns 1, 0, or 1 )
}
my $found
= bsearch( $target, @list, &comparator );
85. A Generalized Binary Search
Implementation
sub binsearch (&$@) {
my ( $code, $target, $aref ) = @_;
my $min = 0;
my $max = $#{$aref};
while ( $max > $min ) {
my $mid = int( ( $min + $max ) / 2 );
no strict 'refs'; ## no critic(strict)
local ( ${caller() . '::a'}, ${caller() . '::b'} )
= ( $target, $aref->[$mid] );
if ( $code->( $target, $aref->[$mid] ) > 0 ) {
$min = $mid + 1;
}
else {
$max = $mid;
}
}
{
no strict 'refs'; ## no critic(strict)
local ( ${caller() . '::a'}, ${caller() . '::b'} )
= ( $target, $aref->[$min] );
return $min if $code->( $target, $aref->[$min] ) == 0;
}
return;
}
86. Guessing numbers programatically
use List::BinarySearch 'binsearch';
use IO::Prompt::Hooked 'prompt';
print "Please think of a number between 1 and 10...nnn";
my $tries = 0;
binsearch {
++$tries;
my $response = prompt(
message => "Is the number $b? (higher, lower, or yes):",
validate => qr/higher|lower|yes/i,
error => sub { "Bad input: $_[0] .Try again.n" },
);
return 1 if $response =~ /higher/i;
return -1 if $response =~ /lower/i;
print "Found $b in $tries guesses.n";
exit(0);
} undef, @{[1 .. 10]};
89. Generalization, Abstraction,
Encapsulation
● We generalized the Binary Search algorithm.
– It doesn't care what a target, or comparator look like
inside.
● Our target and list items are abstractions on their
underlying characteristics.
90. Generalization, Abstraction,
Encapsulation
● We generalized the Binary Search algorithm.
– It doesn't care what a target, or comparator look like
inside.
● Our target and list items are abstractions on their
underlying characteristics.
● Our comparator specializes our Binary Search algorithm.
– Black box (encapsulation of functionality).
91. Generalization, Abstraction,
Encapsulation
● We generalized the Binary Search algorithm.
– It doesn't care what a target, or comparator look like
inside.
● Our target and list items are abstractions on their underlying
characteristics.
● Our comparator specializes our Binary Search algorithm.
– Black box (encapsulation of functionality).
● We can use it anywhere.
– Just provide a target, a list, and a means to compare.
– Never re-invent it again.
– Fewer bugs, through code re-use.
92. We turned the number guessing
game into a Dremel tool.
93. A good programmer should seek opportunities to
apply generalized solutions to specific problems.
94. A good programmer should seek to make
solutions general, so that they can be reused by
providing a specialization to them.
95. A good programmer needs to learn where and
how to find these opportunities.
98. The Virtues You Should Seek
● Be Lazy
– Work hard now to
make it possible.
● Be Impatient
– The status quo is
never good enough
● Be Hubristic
– Write code you can be
proud of.
101. Think Like A Programmer
● #UtahDigiCon
● #thinklikeaprogrammer
● David Oswald
● daoswald@gmail.com
● Salt Lake Perl Mongers
● http://saltlake.pm.org