AP Computer Science Principles
Week 3 Slides
(“Algorithms + python 1”)
2024-25
AP CSP Week 3 … and camp!
The End of Algorithms; The Beginning of Python I
• Last Week
• Algorithms V:
Flow charts,
Reasonable time,
Solvability, etc.
• Homework:
Install Python
Homework:
confirm python (idle) is installed.
it’s on software center.
I will grade this on completion.
An algorithm is a finite set of instructions
Last Week that accomplish a specific task.
Natural Language
Good & Not-so-good Algorithms: We played with cards, to practice
Shampoo High-Level Languages writing algorithms using limited sets
IBM Watson / Healthcare of instructions (limited language).
Peanut Butter & Jelly Low-Level Languages
Toll House Cookies Assembly Code
Snow Pudding (1 so far)
IRS Form 1040 Machine Code
3 Types of Instructions: Hardware (Computer)
Sequencing, Selection, Iteration
A good algorithm is a We played with LEGOs, to practice
and explore natural language
well-ordered collection of algorithms & isometric drawings.
unambiguous and effectively
Why did I belabor the bottom end of the language stack?
computable operations that,
• So you could see & understand a bit of machine/assembly code
when executed, produces a
• To drive home the differences between natural language & a program
result and halts. • So we don’t have to revisit this later.
AP CSP Week 3
The End of Algorithms; The Beginning of Python I
• Last Week
• Algorithms V:
Flow charts,
Reasonable time,
Solvability, etc.
• Homework:
Install Python
Flowcharts (Context for Assignment 1.1)
A flowchart is an easy way to diagram a
process or algorithm and, if done
properly, is very hard to mess up.
A proper
flow chart
begins and
ends with
ovals…
Flowcharts (Context for Assignment 1.1)
There are standard shapes that you must get familiar with for this
AP exam and for your future classes at STEM and in life:
Shape Oval Rectangle Diamond Arrows
Terminator: A step in the Decision: Connectors: the
the start or end algorithm or one arrow goes in, two arrows connect
What it represents of the algorithm process or more arrows go out, the shapes in
or process reflecting possible sequence from
results of the decision. start to end.
Examples:
Remember
this bad
algorithm?
It’s a bad
Flow Chart,
too.
These flow
charts are a
little better,
but don’t
really
articulate a
process…
This is better,
but the topic is
boring-er
It doesn’t have to
be boring:
You can use
flowcharts to
pick a boardgame,
a dog breed,
a Disney fairy…
Flowcharts (Context for Assignment 1.1)
There are standard shapes that you must get familiar with for this
AP exam and for your future classes at STEM and in life:
Sequencing Selection Iteration All of the above
AP CSP Week 3
The End of Algorithms; The Beginning of Python I
• Last Week
• Algorithms V:
Flow charts,
Reasonable time,
Solvability, etc.
• Homework:
Install Python
Last Notes on Algorithms: Unsolvable-ness
Are all problems solvable?
That is, are there problems for which a computer cannot give a correct
answer in any cases?
Some problems are unsolvable. Here are a few examples:
Leaving an Escape Room that has no exit.
Dividing by zero.
Finding an even prime number that isn’t two.
In no cases are these solvable.
If there are NO solutions, the problem is unsolvable.
Last Notes on Algorithms: Undecideable-ness
Are all problems that can be solved decideable?
That is, are there problems for which a computer cannot give a correct answer
in all cases? (2 minutes, table groups)
Some solvable problems are undecideable. Here are a few examples:
Traffic routing
The Halting Problem: can we ensure that every program will stop given
every possible set of inputs? Alan Turing proved this is not possible.
Magic: the Gathering: Given a set of cards, will I always win?
There are other problems in topology, logic, and combinatorial group theory.
Why do fools fall in love? We know for some fools, but not for all of them.
Unsolvable problems:
problems which have no solution
Vs. Undecideable problems
problems that have at least some solutions,
but a computer cannot
find a solution in all cases,
within a reasonable time.
Last Notes on Algorithms: Reasonable Time 0
What problems can be solved in a reasonable time?
Example:
It takes 10 seconds to serve lunch to a student.
There are currently 2400 students at EHS.
EHS adds 600 students.
How long will it take to serve lunch to everyone?
[Hint: This is constant time.]
Last Notes on Algorithms: Reasonable Time 0
What problems can be solved in a reasonable time?
Example:
5 students took a quiz.
Out of 100 points, their scores were 80, 80, 90, 60, and 50.
What was the average score?
5 more students took the quiz.
Out of 100 points, their scores were 100, 90, 100, 60, and 70.
What was the total average score of all 10 students?
[Hint: This is linear time.]
Last Notes on Algorithms: Reasonable Time I
What problems can be solved in a reasonable time?
Here are few:
Computing a GPA or the average of a set of numbers
Sorting a list
Moving sprites in a video game
Even for very large sets or lists or sprites? (Yes.)
If so, then what is unreasonable?
Last Notes on Algorithms: Reasonable Time II
Stand up.
Work with your table partner.
One of you choose a one-letter (letters only) password
and the other tries to crack it.
Then switch; the other chooses the password
and the one tries to crack it.
Sit down once you are done.
Last Notes on Algorithms: Reasonable Time II
Stand up.
Work with your table partner. One of you choose a one-letter (letters only)
password and the other tries to crack it. Then switch; the other chooses the
password and the one tries to crack it.
Now try the same thing with a 2-letter password. How much harder is it?
If you finish that, try a 3-letter password.
Table Group Interlude:
Knowing what you know now, what steps would you include in an algorithm to
crack longer passwords (8-20 characters)?
Last Notes on Algorithms: Reasonable Time III
Stand up.
Work with your table group.
Find the largest subset of your table group who were all strangers at the
beginning of this school year. (Stranger = someone you didn’t know, through
school, sports, clubs, other activities)
Once you have found the group, those not in the group should sit down.
Last Notes on Algorithms: Reasonable Time III
Stand up.
Work with your table group.
Find the largest subset of your table group who were all strangers at the
beginning of this school year.
Once you have found the group, those not in the group should sit down.
… now solve this across the entire class.
That is, what is the largest subset of the class that were strangers prior to the
beginning of this school year?
Last Notes on Algorithms: Reasonable Time
Reasonable time is arbitrarily defined,
based on how quickly problems can be solved as inputs increase.
If, as inputs (n = # of inputs) increase, the solution time increases in constant,
linear, or polynomial time (n2, n3, n4, &c.),
then the problem can be solved in reasonable time.
If, as inputs increase, the solution time increases in factorial time (n!) or
exponential time (anything to the n power, e.g., 2n, 10n),
then it cannot be solved in reasonable time. (Factorial time: seating charts!)
We just did two examples to show unreasonable time:
Passwords: even if only the lowercase alphabet is used, these scale as 26n
The Clique Problem: this is exponential as well
Reasonable Time:
Problems for which, as the number of inputs grows, the
solution takes constant, linear, or polynomial time.
Even large powers, e.g., n10, are solvable in reasonable time.
Not Reasonable Time:
Problems for which, as the number of inputs grows, the
solution takes factorial or exponential time. For these, we use
Heuristics: algorithms that give approximate answers
Summary (you will read this more than once)
Unsolvable problems have no solution.
Undecideable problems are problems that have at least one case with a solution,
but a computer cannot give a correct answer in all cases.
Reasonable time is arbitrarily defined based on how quickly a computer can
solve a decideable problem as the number of inputs increases.
• If, as the number of inputs grows, the solution takes constant, linear, or
polynomial time, then the problem can be solved in reasonable time.
• If, as the number of inputs grows, the solution takes factorial or exponential
time, then the problem cannot be solved in reasonable time.
We use heuristics to approximate good-enough answers for problems that are
undecideable or for problems that cannot be solved in reasonable time.
AP CSP Week 3
The End of Algorithms;
The Beginning of Python I
• Python 1!
Python I Background
After a few years of including block programming, we did the opposite last year and it was
hard, but it worked. Why? Rather than removing details to make it easy, we are stripping
away abstraction. You will give instructions to your computer much more directly, in a
minimalist environment loathed by professionals and your peers.
5 weeks, 2-3 days per week: is approximately what we will spend on Python I.
4 Handouts: there are four handouts you MUST complete. I might make one that’s optional.
New for 2023: Instead of self-studying most of this, I will walk you through it.
New for 2024: I will not collect the exercises in each set. Instead, I will test you. Thank ’27.
Advice:
• Don’t rush. Try to figure out why each example is included in the material.
• Parts of this are hard. That is by design. Stay engaged and thinking; ask questions.
• Parts of this are ambiguous. That is by design, too. Some things are wrong on purpose.
• If you are finished early, make yourself available to help others. Don’t give them answers,
though. Abide by the Academic Honesty Policy.
TSHS AP CSP 2024-25
Python 1.1
Python 1.1:
Agenda
Install Python
Learn how to use the Shell
Learning Targets: I can
(1) install Python,
(2) use the Shell, and
(3) use the most basic
functions—print() and round().
Python 1.1 30
Mistakes are proof that you are trying
or
I am showing you an error in Python
or
you are tpying
Please install Python
Python 1.1 32
Find and open “IDLE”.
This window is called the ‘Shell window’.
Find and open “IDLE”.
The “>>>” is a prompt called the ‘Shell prompt’.
If you see the >>> prompt, you are using the Shell.
If not, you are making a program in an ‘Editor’.
Now try these:
All of the exercises that we did in class are in the handout—
if you didn’t understand any part of them, work through the packet or come see me.
G ďŌ ďġ
ü Ō ďć ı ď ď Ųď
Ūď ďď ďŌŲ ď Ō ď
print() is a function
A function is a set of instructions with a name—in this case, “print()”.
When you call a function, you add the parentheses right after the name.
In the parentheses, you can add 0, 1, or more arguments. An argument is
data given to the function that the function uses. Here, the print() function
uses the arguments to display whatever you provide to it!
Some functions must be given the exact, correct number of arguments
(e.g., round() takes 2 and only 2 arguments); print is very flexible!
Python 1.1 36
round() is a function, just like print()
A function is a set of instructions with a name—in this case, “round()”.
When you call a function, you add the parentheses right after the name.
I thought round() only took 2 arguments. I was wrong. If you give 1 argument, round
will round that number to the nearest integer. If you use 2 arguments, the first
argument is the number to be rounded; the second argument is the number of places
to which you want to round it. Interestingly, if you use a negative number as the second
argument, it will round to that many zeros!
If you use more than 2 arguments? round() will throw an error.
Python 1.1 37
AP CSP Week 3
The End of Algorithms;
The Beginning of Python I
Python 1! We’re just
instructing a computer.
On Friday, if you are
attending camp, you should
be released after SECOND
PERIOD. Go the busses at
9:45am.
Are you a Top 5 Track & Field
KaMS Record Holder? See me!
TSHS AP CSP 2023-24
Python 1.2
Python 1.2:
Agenda
Programs
Variables
Data Types,
including Strings and Lists, Input, Slicing
Learning Targets: I can
(1) make, save, and run a program;
(2) create, change, and use variables to
hold different types of data;
(3) use different data types, including
Python 1.2 integers, floats, strings, and lists.. 40
Create a New File (a new program!)
Python 1.2 41
1.2.1 Write a simple program
Î ď í ŷ Ō Ūďŷ Ōġď ď
ġŲ ď Ō ćď ġ ďćŌď # <Andy Christensen>
Ōćď ŷŌ ď ďď Ō ġ
# <18 September 2024>
E ď Ō Ō
ç Ō ď ġŲ ćď ďí ď ďď # <AP CSP Period 1>
ć ď ďŌć Ō ŷ Ō Ų ď Ōųď Ō
print(“Hello, world!”)
ý ŲŌ Ōď ďŲ ď ď
Ūďŷ Ō ć ď ć ŷď ď # This one-line program is easy
ö ďí ď ďď ďŌć Ō ġ ď to understand...
Ų ď Ūď ďď ď ġ ď ď
Save and run your program
EŲì ì ä ć ď
í Ōď Ōď ŷŌ
Ōď ď Ų ď ďį ďŲŌďġ
Q ŌďŌ ďŲŌġ ćď ŲŌďć í í ŷŌ
ç Ųď Ōď ď ŷŌ
ď ŷŌ
ü Ō Ō ď
variable
A variable is
(1) a location (address) in the computer’s memory that
(2) has a name, and
(3) stores data.
Variables are how a computer and a program remember data.
Python 1.2 44
data types
Every variable stores data according to data type:
Integers whole numbers, e.g., -2, -1, 0, 1, 2, 3, 147
Numbers
Floats decimals, e.g., 3.14, 2.71828, 2.023, 4.
Strings groups of characters (letters, numbers, characters) in quotes
Lists groups of anything (numbers, strings, images, lists, etc.)
Boolean Is always either ‘True’ or ‘False’
The data type tells Python how much memory to allocate for the data.
Python 1.2 45
Now try these:
All of the exercises that we did in class are in the handout—
if you didn’t understand any part of them, work through the packet or come see me.
G ďŌ ďġ
ü Ō ďć ı ď ď Ųď
Ūď ďď ďŌŲ ď Ō ď
AP CSP Week 3
The End of Algorithms;
The Beginning of Python I
• The Age of
Snow Pudding
• Languages I
… and a warning:
If you didn’t take the quiz on
11 September, you have
through Tuesday 24
September to make it up.
I don’t chase you for
quizzes; only for some
(bigger) projects
2020…
Snow
Pudding in
COVID: 2021
(KEEP CALM AND SNOW PUDDING)
AP CSP Week 3
The End of Algorithms;
The Beginning of Python I
• The Age of
Snow Pudding
• Languages I
… and a warning:
If you didn’t take the quiz on
11 September, you have
through Tuesday 24
September to make it up.
I don’t chase you for
quizzes; only for some
(bigger) projects
Natural Language
→
Algorithms & Language
High-Level Languages
Higher ABSTRACTION
The most basic challenge is the
Low-Level Languages
difference between the language
we speak (Natural Language) and Assembly Code
the language computers speak.
Machine Code
At what level of the stack do you Hardware (Computer)
think Human Machine Language
sits?
Today, we “flesh out” the upper part
of the language stack. Take notes.
Why are there so
many languages?
Plankakül Visual Basic
Fortran SQL
COBOL HTML
C JavaScript
Ada R
C++ Rust
Each language met the needs of an area of
Java Go application or platform or priority...
Python Swift • Engineering/Databases/Graphics
• Ease of Programming
C# PHP • Locking Developers onto a Platform (GOOG,
Groovy Ruby APPL, MSFT)
• Graphics
Often, there is a choice between efficient compilation, efficient execution, & ease of programming.
Major languages: “flash talk”
I have given each table a card or two,
each with the name of two major programming languages on it.
Please take out your computers and in the next 15 minutes,
learn about the language on each card.
Prepare a 1-2-minute summary for each language that includes
• where you looked other than Wikipedia
• the year the language was released
• Who created it
• Why? What are the major uses of the language (and why it is suited)
• Is it a general purpose language or a special purpose language?
• Something else interesting about the language
Each of you should do a bit of talking.
This is for y’all. I already know it. Pay attention like you don’t.