77
How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Embed Size (px)

Citation preview

Page 1: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Think Like a

ProgrammerCS 21a: Introduction to

Computing IFirst Semester, 2013-2014

Page 2: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Last Time…

►Civilization advances by extending the number of operations that we can perform without thinking about them.►Alfred North Whitehead (1861-1947)

Page 3: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Outline

►Motivational Activity►Thinking About Programming►Proper Writing Practice►Abstraction►Practice Problems

Page 4: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Motivational Activity

Page 5: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Try Writing Down the Following

►Instructions for Shampooing Hair►Your Weekly Schedule►How to Make a Notebook

Page 6: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

1. Open the bottle of shampoo.►Don’t have to say how to open a

bottle►“Open” is considered a primitive

operation.

Page 7: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

2. Pour a generous amount of shampoo onto hand.►“Generous amount” isn’t really

specific.►Not too important for shampooing hair►An exact amount like “10 mL” is

important if this were a manufacturing process involving dangerous chemicals.

Page 8: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

►The amount of shampoo needed can depend upon hair length.►We say that the amount of shampoo

and the hair length are variables.►Hair length is an input parameter.►The amount of shampoo is a

function of hair length.

Page 9: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

►Note the difference►Amount of shampoo function: rule that

states how the amount of shampoo should vary with hair length

►Amount of shampoo value: the actual amount of shampoo needed given a specific instance of the problem, a specific hair length

►Here it is the value that is variable, not the function

Page 10: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

►Note: The next two steps will be presented in reverse order, but only for educational purposes.

Page 11: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

4. Rinse and repeat, if necessary.► There are actually 2 statements here.► “Repeat” refers to the entire process,

and not just to the rinsing. It is important to specify what exactly is repeated.

► “If necessary” makes the repetition a conditional iteration►Means the process can be invariably long,

but the algorithm that describes it remains short

Page 12: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

3. Spread shampoo all over hair until lather forms.► “Spread shampoo all over hair” is a

bounded iteration.►Another way to say it: “For every area of scalp,

put some shampoo from hand.”►There is a fixed number of areas (simplistically

speaking) that the shampoo has to be placed.► “Spread shampoo all over hair” is nested in

a conditional iteration “until lather forms.”

Page 13: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Shampooing Hair

►But the act of spreading shampoo can also be seen as a single unit, a single subroutine, and there is no nesting here.

►The how-to knowledge for properly spreading shampoo over your hair can be described elsewhere.

Page 14: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

What’s the point?

►There are many features of our language that we take for granted.

►Think explicitly about these features►When we want to convey a difficult

idea very clearly►When we want a dumb computer to

execute a difficult solution

Page 15: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedule

►Level of detail is important.►You don’t just say, “Perform Monday

activities.”►But you also don’t go into details of

“perform bathroom rituals.”

Page 16: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedule

►The time in which activities happen give a natural direct sequencing to the actions performed.

Page 17: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedule

►Since MWF classes are the same, and TTh classes are the same, some might write only 2 daily schedules, and say…► If MWF, do this…►Otherwise if TTh, do this…

►Two things happened here:►MWF schedule is chunked into one

subroutine, TTh schedule is chunked into another.

►Conditional branching

Page 18: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Here’s A Different Way of Writing The Weekly Schedule

►This is my weekly schedule:► For each day in the week

►If the day is Monday, Wednesday, or Friday►Do MWF activities

►If the day is Tuesday or Thursday►Do TTh activities

►If the day is Saturday►Do Saturday activities

►If the day is Sunday►Do Sunday activities

Page 19: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Here’s A Different Way of Writing The Weekly Schedule

►Here is my TTh schedule:►Wake up at 7:00 A.M.►Get ready for school from 7:00-8:00

A.M.►Travel to school from 8:00-8:30 A.M.►CS class at 8:30-10:30 A.M.►etc.

►And so on for the other days…

Page 20: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedule

►Each activity can be described in further detail if there’s a need.► In the same way that a daily schedule needs to

be described in further detail►Algorithms can be described in terms of smaller

algorithms.►The entire weekly schedule can be integrated

into a larger (monthly/yearly/life) schedule.►Algorithms can be part of larger algorithms.► Programs can be part of larger programs.

Page 21: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedule

►Schedules can be seen as an example of recursive algorithms.►A schedule for a timeframe can be expressed

in terms of schedules for smaller timeframes.► The problem of living for a certain amount of

time can be broken down into the problem of living for smaller amounts of time.

► There will be a schedule (daily) called the base case that is small enough to be described directly, instead of in terms of smaller schedules.

Page 22: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedules

►Schedules can be seen as an example of recursive algorithms.►But in a true recursion, the way that a

larger problem is decomposed into a smaller problem is the same for all sizes.

Page 23: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Weekly Schedules

►Now that you think of your life as a program, it kind of makes you feel like a robot, doesn’t it?

Page 24: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Make a Notebook

1. Get a bunch of plain writing papers.2. Get some glue.3. Get a cardboard sheet.4. Arrange the papers in a neat stack.5. Glue the papers together onto the

cardboard sheet.

Page 25: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Make a Notebook

►Getting and naming the objects needed (declaring and assigning variables) is part of the procedure.

►Paper is an object that we assume exists, but really we know it is composed of more primitive data types.► This is a physical example, so it doesn’t really

make sense to talk about data types, but the analogy should be clear.

► In computer science, the primitive data types are usually numbers.

Page 26: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Make a Notebook

►By changing the position of a piece of paper (during rearranging) or applying glue to it…► Its state is changed by invoking a

method on it or passing a message to it.

Page 27: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Make a Notebook

►We are getting a bunch of plain writing papers.► Each piece of paper is an instance of the

more abstract idea or class of plain writing papers.

►By talking about a bunch or array of papers, we don’t care about the individual papers. We want to talk about and manipulate the entire group of papers as a unit.

►Contrast this with the single cardboard paper.

Page 28: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Make a Notebook

►You are acting on the entire array as a unit.►No matter how many papers there are,

since the same action is done on each paper, a bounded iteration makes it possible to describe an arbitrarily long process on an arbitrarily large volume of objects using a short algorithm.

►Question: Why bounded iteration?► Iteration and arrays often go hand-in-hand.

Page 29: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Make a Notebook

►Both “plain writing paper” and “cardboard paper” are special cases of the more abstract “paper.”► “Paper” is an abstract class.► “Plain writing paper” and “cardboard paper”

are subclasses of paper, they inherit several features of generic paper, but have special features of their own.

►Ultimately, there is a most abstract class that contains all objects, which we’ll call Object.

Page 30: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Outline

►Motivational Activity►Thinking About Programming►Proper Writing Practice►Abstraction►Practice Problems

Page 31: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Thinking About Programming

Page 32: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Input and Output

►If programming is about reasoning about an intelligent process, then programs have to interface with the conditions surrounding that process.► Input: the initial condition►Output: the final condition►An algorithm correctly describes a

process when it predicts the final condition given any initial condition

Page 33: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Input and Output

►If programming is all about problem solving, then programs have to interface with the real world.► Input: the current state of the world

different from the desired state►Output: the desired state of the world►An algorithm solves a problem when it

produces the correct output given any specific input

Page 34: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Instances

►Each particular selection of inputs constitutes an instance of a problem.

►Each particular execution of an algorithm/program is independent of each other.

Page 35: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Instances

►The blueprint is not the house.►The instructions for building a house

is not the same as each act of building a specific house.

►Use is not the same as definition.

Page 36: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Data Flow

►Think of data transforming from input into output in successive stages.

►Control structures like branching and iteration control the flow of data

Page 37: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Sub-problems

►When the problem is too complex, it may be broken down into smaller problems.

►A solution can be expressed in terms of smaller solutions.

►Relevant data is passed as input into the sub-problem when you call a subroutine.

►The output of the subroutine is returned to the original problem for any further transformations.

Page 38: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Names and Scope

►What the data is named in a sub-problem need not be the same as in the outer problem.

►The namespaces of independent algorithms are separate.

►The context in which a name refers to something refers to scope.►Once you exit a scope, the name no

longer means the same thing.

Page 39: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Instance Versus Scope

►Names take on different values in different instances, but mean the same thing.

►Names mean different things in different scopes.

Page 40: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Outline

►Motivational Activity►Thinking About Programming►Proper Writing Practice►Abstraction►Practice Problems

Page 41: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Proper Writing Practice

Page 42: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Write For Humans

►Programs must be written for people to read, and only incidentally for machines to execute.►Not just for making computers do

things, but for expressing ideas about processes

►Abelson and Sussman, preface to the first edition of Structure and Interpretation of Computer Programs

Page 43: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How To Write For Humans

►Write clearly.►Make the code speak for itself.

►Level of detail is important.►Don’t go down to the level of 0’s and

1’s.►Don’t assume all high-level actions

can be understood.

Page 44: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Too Low-Level

1. Raise your left leg at a 70-degree angle from the ground.

2. Let your bottom come into contact with the bike seat, from the left side, using a velocity of 3 inches per second.

3. Lower your left leg to a 20-degree angle from the ground.

4. …

Page 45: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Too High-Level

1. Sit on the bike.2. Move.

Page 46: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Just Right

1. Carefully situate yourself on the bike seat until you are comfortable. Grasp the handlebars and put your feet on the pedals.

2. Use your feet to begin pedalling.

3. Keep pedalling to gain speed. Stop pedalling once you’re at the desired speed.

4. Start pedalling again if you slow down. You’ll fall if the bike stops moving.

5. Turn the handlebars to steer. Don’t turn too much to keep your balance.

6. If you feel like falling to one side, use your foot to kick yourself back to balance.

Page 47: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Write For Computers

►Of course, algorithms can be applied in all aspects of life.

►But many interesting problems require that solutions run on computers to be useful.

Page 48: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Computers are Stupid

►The order in which instructions are given matter.

► Instructions have to be dead-specific.►All high-level instructions must eventually

be translated into 0’s and 1’s.►How?

►High-level code is translated to low-level code by an interpreter or compiler.

►Programs must be written following exact syntax.

Page 49: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Another Advantage of Compilation

►Allow the same machine to run programs in different languages.►But wait, I thought we had a universal

language for computer science?►Universal, in the sense of having the same

basic operations at the lowest level►Universal, in the sense that any problem that

can be solved in a good language can be solved in another

►But ways of combining the lower-level ideas into higher-level ones can differ

Page 50: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Why So Many Programming Languages?

►Different application areas and problem domains need different high-level constructs.

►Some languages make it easier to express certain ideas than others.

►BUT THEY’RE ALL EQUIVALENT IN EXPRESSIVE POWER.► It’s always possible to translate from

one language to another.And because HTML and CSS are less expressive than the standard programming language, most people do not consider them to be programming languages.

Page 51: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Why So Many Programming Languages?

►Practical Tip:►Get exposed to different languages.►Realize they’re all fundamentally the

same though they look different.►Learn how to learn a language

quickly.►Master a language when the need

arises.

Page 52: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

So Going Back to the Subject…

►Conflicting goals: Easy for humans to understand versus easy for computers to perform.

Page 53: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Write For Both Humans and Computers

►Write in modules, instead of linearly.►Describe a complex process in

terms of simpler processes.► If the simpler algorithms aren’t

primitives yet, repeat.►Obey the language primitives, then

trust the compiler to translate them into code the computer can understand.

Page 54: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

How to Write For Both Humans and Computers

►More on how this is technologically possible, later.

Page 55: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Write Generally, but Not too Generally

►Describe algorithms in terms of parameters and variables to account for many cases.►Employ conditionals whenever necessary.►The most useful programs are written

once, then run multiple times.►Algorithms must be general enough to

handle any specific assignment of values to input parameters.

Page 56: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Write Generally, but Not too Generally

►Ideally, you’d want your algorithm to work for infinite mappings of inputs to outputs.►Theoretical CS says we can’t make a

universal algorithm to solve all problems.

►For practical purposes, algorithms only need to be general enough to solve the problem at hand.

Page 57: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Write Generally, but Not too Generally

►For example, it might be better to write a recipe for x muffins than for a fixed batch of 12 muffins, with all the ingredients expressed in terms of x.

► It might even be amazing to have a single recipe that can make both muffins and cakes (for example, if the chocolate mixture is the same).

►But it probably wouldn't be nice to mix instructions for baking and instructions for doing laundry.

Page 58: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Quick Summary

►Write for humans►Write for computers, but not at the

expense of humans►Write generally, but not too

generally

Page 59: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Outline

►Motivational Activity►Thinking About Programming►Proper Writing Practice►Abstraction►Practice Problems

Page 60: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Abstraction

Page 61: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Abstraction

►One algorithm can be defined in terms of another algorithm.►Example: Exponentiation is repeated

multiplication, which is repeated addition.

►Primitives are algorithms that we don’t define in terms of other algorithms.►Depends on the level of abstraction or

detail that we agree to work upon►Here, we take addition as a primitive

Page 62: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Abstraction► It’s difficult to express complex algorithms purely in

terms of primitives► Imagine if every time you wanted to go somewhere,

apart from worrying about directions, you also had to worry where to position your knees, your ankles, etc.

► Structures built from smaller structures arise in nature.► Chords from notes► Societies from people► Clouds from water droplets► Organisms from cells► Computers from transistors

Page 63: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Abstraction

►Chunking is necessary to achieve complexity►Difference between chess masters and

beginners►A good programmer can:►Chunk pieces of code for reuse and to

decrease complexity►Learn to use another good

programmer’s chunked piece of code

Page 64: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Layers of Abstraction

Real World

Mathematical Representation

High-level Programs

Library and Language Primitives

Assembly or Binary

Real-world Conditions

Real-worldAnswer

INPUTS OUTPUTS

Encoded InfoEncoded Answer

Parameters Return Values

Scientists, Businessmen,

Humanists

Hardware

Real World

YOU

Computer Engineers

Electrical Engineers

System CallsSystem

Responses

Electronic Signals

Electronic Signals

ElectronMovements

ElectronStates

Page 65: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Layers of Abstraction

Page 66: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Two Kinds of Abstraction

►Procedural Abstraction►Writing algorithms in terms of simpler

algorithms►Data Abstraction►Defining data types in terms of

simpler data types►Procedural Abstraction + Data

Abstraction = Objects

Page 67: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Quick Summary

►Abstraction is all about managing complexity.

►Abstraction is all about trust.►Programmers at a higher-level trust

that lower-level programmers did their job correctly.

►Each layer of abstraction corresponds to a problem with inputs and outputs.

Page 68: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

SummaryAlgorithms Data

Primitive operations Primitive data types

Infinity managed by iteration Infinity managed by arrays

Infinity also managed by recursive procedures

Infinity also managed by recursive structures

Chunked with subroutines: more complex algorithms arise from simpler algorithms

Chunked with tuples: more complex data types arise from simpler data types

Invoking with specific parameters constitute an instance

Declaring with specific values constitute an instance

Takes data as input and “transforms” it to an output to solve a problem

Is at first the input supplied, transforms by applying series of algorithms, and finally becomes the required output

Objects

Page 69: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Outline

►Motivational Activity►Thinking About Programming►Proper Writing Practice►Abstraction►Practice Problems

Page 70: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Practice Problems

Page 71: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Blog Post

►Define what a blog post is in terms of the smaller pieces of data associated with it.►Assume that the only primitives are letters

and numbers (characters).►Think as deeply as you can about all the

other things you need to define to define a blog post.

►Hint: First define what a word is and build up from there.► This strategy is called bottom-up design.

Page 72: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Algorism

►Write your own version of Al-Khawarizmi's algorism for multiplying two Hindu-Arabic numbers.► Assume the following primitive operations:

►Add any number of zeros to a number (multiply a number by a power of 10)

►Multiply two one-digit numbers (by looking it up in a multiplication table, for example)

►Add two one-digit numbers►Get the ith digit of a number (where the rightmost

digit is the 0th digit)►Get the number of digits in a number

Page 73: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Algorism

►Write your own version ofAl-Khawarizmi’s algorism for multiplying two Hindu-Arabic numbers.►Assume the following language constructs:

►The ability to name anything (see example)►The ability to perform iterations (see example)►The ability to define subroutines (see

example)

Page 74: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Algorism

►Hint: Write the algorism in terms of simpler but non-primitive subroutines first (example later), then write those subroutines.►This strategy is called top-down

design.►Hint: Work in small groups and

assign subroutines to people.

Page 75: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Algorism

►Hint: Here's how you might start.► Here's how to multiply two numbers a and b:

1. Let n be the number of digits of b

2. For each number i from 0 to n:1. Let c be the result of multiplying the ith digit of b

with a

2. ...

3. ...

►Here's how to multiply a number x with a single digit y1. ...

Page 76: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Next Time…

►Applying these concepts to be actually run by a computer, in an actual programming language called Java

Page 77: How to Think Like a Programmer CS 21a: Introduction to Computing I First Semester, 2013-2014

Really Serious about Being an Excellent Computer Scientist?

►Read the Structure and Interpretation of Computer Programs by Abelson and Sussman along with this course.►Warning: It’s a quite difficult read.►You can also read it after taking this

course.