cps101-150115.pptx

advertisement
CompSci 101
Introduction to Computer Science
January 15, 2015
Prof. Rodger
1
Don’t forget the rule
• You can’t sit in the last two rows
• That’s you!
2
Announcements
• Want a permission number to add course?
– Fill out form on course web page (main page)
• Reading for next time, do RQ 3
• Assignment 1 due Tuesday, Jan 20
• APT 1 is out and due on Thursday, Jan 22
• Today – Problem Solving, Python practice,
solve an APT
3
Starting with Python
• Variable
– Name of a storage location – holds a value
= to assign a value to a variable
• Type
– Different types of data
– A variable can stores data of a certain type
– int, float, str
• operators in Python for numbers
– Arithmetic: +
-
*
/
%
**
• Built-in functions: pow, abs, round, int, float
example:
pow(2,3) + round (1.6) 4
Eclipse – Three ways to run
1. Write program and store in file
– Create a PyDev project – a folder for programs
– Create a PyDev module for each program (file)
– Run in console
2. Snarf an APT and run in Eclipse
3. Run interactively
– Open PyDev console
– Execute each line as typed
– Code not saved
5
Variables, Types, Expressions?
a=5
b=4
c = "fred"
print c
print a + b * 3
print (a + b) * 3
print a / b
print a / (b * 1.0)
6
Demo: Run interactively in
Eclipse PyDev Console
• If Console window is not showing then
– Click on Window, Show View, Console
• Then at the bottom of Eclipse, click here:
• Select PyDev Console, Python Console
7
Latanya Sweeney
I am a computer scientist with a long
history of weaving technology and policy
together to remove stakeholder barriers to
technology adoption. My focus is on
"computational policy" and I term myself a
"computer (cross) policy" scientist. I have
enjoyed success at creating technology that
weaves with policy to resolve real-world
technology-privacy clashes.
http://latanyasweeney.org/
Identify 87% of US population using
(dob,zip,gender). Director of Harvard
Data Privacy Lab, instrumental in HIPAA
because if de-identification work,
currently Chief Technologist FTC
8
Function – define vs call
• def functionName(parameters):
block
• def sum(a, b):
return a+b
• Call function
sum(7, 4)
sum(“a”, “cat”)
• Advantages
– Repeat code, call multiple times
– Flexible, call with different arguments
9
Demo 2
• In Eclipse write a file with a function and
run
• Stuff.py
10
What is an APT? BMI APT
• Automated/Algorithmic Problem Testing
– Write one function, 2-30 lines, solve a problem
– Tested automagically in Eclipse or the browser
– Lots of test cases – test test test
• Start simple, build toward more complex
– What is a function? A function call?
– What is a parameter? Argument?
– How do you run/execute a program
11
Demo 3 on Solving APT BMI
• Write your code in Eclipse
• Snarf the apt testing materials
– Create the python file in apt folder
– Write your code
• Run in Eclipse or Run on the web
12
Skills and Practice for Game Playing
• http://www.youtube.com/watch?v=AEBbsZK39es
$193, $540, $820,
$700, $749. Are
these reasonable?
Why?
13
I'm thinking of a number …
bit.ly/101S15-0113-03
• You guess. I'll tell you high, low, or correct
– Goal: guess quickly, minimal number of guesses
– Number between 1 and 100…
– Number between 1 and 1000…
• Can you describe an algorithm, instructions, that
would allow someone to use your instructions to
play this game correctly. Start with 1 and 100,
but ideally your instructions work with 1 and X
14
Analyzing the binary search
algorithm
• Is the algorithm correct?
– Try it, again, and again and …
– Reason about it: logically, informally, …
• How efficient is the algorithm?
– How many guesses will it take (roughly,
exactly)
– Should we care about efficiency?
15
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Anderson
Applegate
Bethune
Brooks
Carter
Edwards
Foggle
Griffin
Holhouser
Jefferson
Klatchy
Morgan
Munson
Narten
Oliven
Parken
Rivers
Roberts
Stevenson
Thomas
Wilson
Woodrow
Yarbrow
Find Narten
16
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Anderson
Applegate
Bethune
Brooks
Carter
Edwards
Foggle
Griffin
Holhouser
Jefferson
Klatchy
Morgan
Munson
Narten
Oliven
Parken
Rivers
Roberts
Stevenson
Thomas
Wilson
Woodrow
Yarbrow
X
X
Find Narten
Found!
How many
words did we
look at?
17
Searching for words
• If we had a million words in alphabetical
order, how many would we need to look at
worst case to find a word?
• 18
18
Searching for words
• If we had a million words in alphabetical
order, how many would we need to look at
worst case to find a word?
1,000,000
976.56
500,000
488.
• 20
250,000
244
125,000
122
62,500
61
If you are clever,
31,250
30
Cut the number of numbers to look
15,620
15
at in half over and over again,
7812.5
Then only 18 numbers to look
7.5
3906
at worst case
3.75
1953
1.875
19
Download