The last CS lecture you - PowerPoint PPT Presentation

About This Presentation
Title:

The last CS lecture you

Description:

Title: PowerPoint Presentation Last modified by: dodds Document presentation format: On-screen Show (4:3) Company: Zachary Dodds Other titles: Arial MS P ... – PowerPoint PPT presentation

Number of Views:158
Avg rating:3.0/5.0
Slides: 114
Provided by: hmc64
Learn more at: https://www.cs.hmc.edu
Category:
Tags: last | lecture | team | vowel

less

Transcript and Presenter's Notes

Title: The last CS lecture you


1
IS 313 Today!
Aye! Aye! Aye! mateys
CS ? This is just like magic!
Hw 0 due this Tuesday, 9/14, at 1159 pm

The last CS lecture youll ever need!
Hw 1 due next Tuesday, 9/21, at 1159 pm
Choose 2 out of 3 problems
Run Apache!
Our legal counsel requires us to include these
footnotes
On Warner Brothers' insistence, we affirm that
this 'C' does not stand for 'Chamber' and 'S'
does not stand for 'Secrets.'

Caution do not take this statement too literally
or it is possible find yourself in twice as many
CS lectures as you need!
2
What is it?
What type is it?
3
Again, this is really data.
wisdom
knowledge
information
data
4
Python Data Types
Numeric
Name
Example
What is it?
values with a fractional part
float
3.14
long
10100
integers gt 2147483647
int
42
integers lt 2147483647
the results from a comparison , !, lt, gt, lt,
gt
bool
"Boolean value"
5
Datatypes as genes
Dominant
What will these results be?
float
1.0 / 5
long
10100 - 10100
int
1 / 5
bool
41 True
Recessive
6
Precedence
Caution Level

( )
set equal to
Highest
/
divide

Python Operators

remainder
-

power

is equal to


gt
as usual

lt
Lowest
-
It's not worth remembering all these / things!
Id go with parentheses over precedence
( )
7
the "mod" operator
xy returns the remainder when x is divided by y
7 3
8 3
9 3
16 7
x2 0
For what values of x are these True?
x2 1
x4 0
What happens on these years?
8
the "equals" operators
!
What about ?
9
Names Data
gtgt x 41 gtgt y x 1
Aiden, Braden, Kaden?Ava, Abigail, Caylin?
Choosing the right name is more importantthan I
thought.
10
Inside the machine
What's happening in python
x 41 y x 1
What is happening behind the scenes
"variables as containers"
42
41
name y type int LOC 312
name x type int LOC 300
memory location 300
memory location 312
Computation
Data Storage
id, del
11
Computer Memory
Random Access Memory (RAM)
is a long list of memory locations
on or off
bit 1 "bucket" of charge
byte 8 bits
word 4 bytes 32 bits
is it really a coincidence that this looks like
the string theory picture??
42
name y type int LOC 312
plus overhead
4 bytes for an int
12
Re-naming!
gtgt x 41 gtgt y x 1 gtgt x 41 gtgt y 42 gtgt x x
y gtgt x ?? gtgt y ??
Aiden, Aiden, Aiden, Aiden, Aiden,
It's possible to lose your name in Python!
13
Are numbers enough?
No!
You need lists of numbers, as well!
list
and strings are helpful, too.
str
These are Python sequences
14
string functions
str(42) returns '42'
converts input to a string
str len
len('42') returns 2
returns the strings length
'XL' 'II' returns 'XLII'
concatenates strings
'VI'7 returns 'VIVIVIVIVIVIVI'
repeats strings
s1 "ha"
Given these strings
s2 "t"
What are
s1 s2
What did you say!?!
2s1 s2 2(s1s2)
15
String surgery
s 'harvey mudd college'
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s
indexes into the string, returning a
one-character string
index
s0 returns 'h'
Read "s-of-zero" or "s-zero"
s6 returns
s returns 'e'
Which index returns 'e'?
slen(s) returns
python ! English
16
Negative indices
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s 'harvey mudd college'
-1
-3
-5
-7
-9
-11
-13
-15
-17
-19
-2
-4
-6
-8
-10
-12
-14
-16
-18
Negative indices count backwards from the end!
s-1 returns 'e'
s-11 returns
s-0 returns
Python can suit any mood
17
Slicing
what to do if you want a bigger piece of the pie!
s 'harvey mudd college'
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s
slices the string, returning a substring
What's going on here?
s06 returns 'harvey'
s1218 returns 'colleg'
s17 returns 'ge'
s returns 'harvey mudd college'
18
Slicing
what to do if you want a bigger piece of the pie!
s 'harvey mudd college'
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s
slices the string, returning a substring
the first index is the first character of the
slice
the second index is ONE AFTER the last character
a missing index means the end of the string
s06 returns 'harvey'
s1218 returns 'colleg'
s17 returns 'ge'
s returns 'harvey mudd college'
19
Slicing
what to do if you want a bigger piece of the pie!
s 'harvey mudd college'
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s
slices the string, returning a substring
s15-1
What are these slices?
s13
'mud'
How would you get
'e'
Don't 'wor' 'e'- Be 'hap' 'e' !
20
Skip-Slicing
if you don't want your neighbor to get any
s 'harvey mudd college'
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s
skip-slices, returning a subsequence
the third index is the "stride" length
it defaults to 1
s082 returns 'hre '
'doe'
What skip-slice returns
I love this one.
s16
What does this return?
21
Commas separate elements.
Lists Strings of anything
L 3.14, 2,40, 'third', 42
Square brackets tell python you want a list.
len(L)
L0
Indexing could return a different type
Slicing always returns the same type
L01
How could you extract from L
'hi'
22
c 'claremont'
Try it!
0 1 2 3 4 5 6 7 8
g 'graduate'
0 1 2 3 4 5 6 7
pi 3,1,4,1,5,9
u 'university'
0 1 2 3
4 5
0 1 2 3 4 5 6 7 8
9
Part 2
Part 1
What is len(pi)
What is c-1
What is pi-1
What is g04
What slice of pi is 3,1,4
What is c3-2
What slice of pi is 3,4,5
What is c0 u7
What slice of pi is 9,1,1
Add parts of c, g, and u to create 'monty'
Add two slices of pi to get 3,1,1,5
What are pi0(pi1pi2) and
pi0(pi12pi23) ?
Extra!!
23
Python slices It dices...
but wait, there's more!
24
Computational Models
Picobot!?
Python?!
25
Python testimonials
Guido van Rossum
26
20 years ago
27
9/2010
C
Python
http//www.tiobe.com/index.php/content/paperinfo/t
pci/index.html
based on the number of skilled engineers
world-wide, courses and third party vendors
28
Computation's Double Identity
"variables as containers"
42
41
name y type int LOC 304
name x type int LOC 300
memory location 300
memory location 304
Computation
Data
29
Computation's Double Identity
"variables as containers"
42
41
name y type int LOC 304
name x type int LOC 300
memory location 300
memory location 304
Computation
Data
the monomers are functions
30
Look good to me!
How functions look
my own function! def dbl( x ) """ returns
the double of x """ return 2x
Some of Python's baggage
keywords
def starts the function return stops it
immediately and sends back the return value
Docstrings
They become part of python's built-in help
system! With each function be sure to include one
that
Comments
  1. describes overall what the function does, and
  2. explains what the inputs mean/are

They begin with
31
return ! print
def dbl(x) """ doubles x """ return 2x
def dblPR(x) """ doubles x """ print 2x
gtgtgt dbl(21) 42
gtgtgt dbl(21) 42
gtgtgt 2 dbl(21) 44
gtgtgt 2 dbl(21) crash!
print just prints
return provides the function call's value
32
How functions act
My brain has just hit recursophobic collapse!
def undo(s) """ undo undoes its input!
input s, any string """ return 'de'
s
gtgtgt yours undo('caf')
gtgtgt mine undo(undo('caf'))
strings, lists, numbers all data are fair game
33
How functions work
What is demo(-4) ?
def demo(x) return x f(x)
def f(x) return 11g(x) g(x/2)
def g(x) return -1 x
This puts the fun in function!
34
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
def g(x) return -1 x
gtgtgt demo(-4) ?
35
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1 x
return 11g(x) g(x/2)
gtgtgt demo(-4) ?
36
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1 x
return 11g(x) g(x/2)
These are different x's !
gtgtgt demo(-4) ?
37
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1 x
return 11g(-4) g(-4/2)
g
x -4
gtgtgt demo(-4) ?
return -1.0 x
38
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1 x
return 11 4 g(-4/2)
g
x -4
gtgtgt demo(-4) ?
return -1 -4
4
39
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1.0 x
return 11 4 g(-4/2)
gtgtgt demo(-4) ?
40
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1 x
return 11 4 g(-4/2)
g
x -2
gtgtgt demo(-4) ?
return -1 -2
2
41
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1.0 x
return 11 4 2
gtgtgt demo(-4) ?
42
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1.0 x
46
return 11 4 2
gtgtgt demo(-4) ?
43
How functions work
def demo(x) return x f(x)
demo
x -4
return -4 46
def f(x) return 11g(x) g(x/2)
def g(x) return -1.0 x
42
gtgtgt demo(-4)
42
44
Douglas Adams's 42 puzzle
answer 42 question unknown
45
Function stacking
def demo(x) return x f(x)
demo
x -4
return -4 f(-4)
def f(x) return 11g(x) g(x/2)
f
x -4
def g(x) return -1 x
return 11 4 g(-4/2)
g
x -2
gtgtgt demo(-4)
return -1 -2
2
42
"The stack"
(1) remembers separate functions' values for
variables
(2) remembers where to send results back to
46
Function design
47
Thinking sequentially
factorial
5! 120
5! 5 4 3 2 1
N! N (N-1) (N-2) 3 2 1
48
Thinking sequentially
factorial
5! 120
5! 5 4 3 2 1
Next time beyond
N! N (N-1) (N-2) 3 2 1
49
Thinking recursively
Recursion self-reference!
factorial
5! 120
5! 5 4 3 2 1
5!
N! N (N-1) (N-2) 3 2 1
N!
50
Warning
This is legal code!
def fac(N) return N fac(N-1)
I wonder how this code will STACK up!?
51
Warning
def fac(N) return N fac(N-1)
No base case -- the calls to fac will never stop!
Make sure you have a base case, then worry about
the recursive step...
52
def fac(N) return fac(N)
Roadsigns and recursion examples of
self-fulfilling danger
53
(No Transcript)
54
Thinking recursively !
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Base Case
Recursive Step
Human Base case and 1 step
Computer Everything else
55
Behind the curtain
def fac(N) if N lt 1 return 1
else return N fac(N-1)
gtgtgt fac(1)
Result 1
The base case is No Problem!
56
Behind the curtain
def fac(N) if N lt 1 return 1
else return N fac(N-1)
fac(5)
57
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
58
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
4 fac(3)
59
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
4 fac(3)
3 fac(2)
60
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
4 fac(3)
3 fac(2)
2 fac(1)
61
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
"The Stack"
5 fac(4)
4 fac(3)
3 fac(2)
Remembers all of the individual calls to fac
2 fac(1)
1
62
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
4 fac(3)
3 fac(2)
2 1
63
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
4 fac(3)
3 2
64
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 fac(4)
4 6
65
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
5 24
66
def fac(N) if N lt 1 return 1
else return N fac(N-1)
Behind the curtain
fac(5)
Result 120
0 N -gt X 1 0 x -gt N 0
Base Case
Look familiar?
Recursive Step
67
Let recursion do the work for you.
Exploit self-similarity
Less work !
Produce short, elegant code
def fac(N) if N lt 1 return 1
else return N fac(N-1)
You handle the base case the easiest possible
case to think of!
Recursion does almost all of the rest of the
problem!
68
But you do need to do one step yourself
def fac(N) if N lt 1 return 1
else return fac(N)
You handle the base case the easiest possible
case to think of!
This will not work
69
Recursion's Big Picture?
Need to see BOTH the self-similar piece AND the
whole simultaneously!
70
(No Transcript)
71
Breaking Up
is easy to do with Python
s "This has 2 T's"
How do we get at the initial character of s?
How do we get at ALL THE REST of s?
L 42, 21, 7
How do we get at the initial element of L?
How do we get at ALL the REST of L?
72
Recursion Examples
def mylen(s) """ input any string, s
output the number of characters in s """
if else
base case test!
73
Recursion Examples
def mylen(s) """ input any string, s
output the number of characters in s """
if s '' return 0 else
rest s1 len_of_rest mylen( rest )
total_len 1 len_of_rest
return total_len
74
Recursion Examples
def mylen(s) """ input any string, s
output the number of characters in s """
if s '' return 0 else
rest s1 len_of_rest mylen( rest )
return 1 len_of_rest
75
Recursion Examples
def mylen(s) """ input any string, s
output the number of characters in s """
if s '' return 0 else
rest s1 return 1 mylen( rest )
76
Recursion Examples
def mylen(s) """ input any string, s
output the number of characters in s """
if s '' return 0 else
return 1 mylen(s1)
There's not much len left here!
77
Recursion Examples
def mymax(L) """ input a NONEMPTY list, L
output L's maximum element """ if
elif
else
base case test!
another case
78
Recursion Examples
def mymax(L) """ input a NONEMPTY list, L
output L's maximum element """ if
len(L) 1 return L0 elif L0 lt
L1 return mymax( L1 ) else
return mymax( L01 L2 )
Hey - do I get a slice?!
79
Try it!
def power(b,p)
def sajak(s)
""" returns b to the p power using recursion,
not inputs int b, int p
output a float """
""" returns the number of vowels in the
input string, s """
if
Base case test
if
Base case test
elif
else
else
power(5,2) 25.0
sajak('wheel of fortune') 6
What about y? You decide
Remember bp b bp-1
What seven-letter English word w maximizes
sajak(w) ?
Want more Pat?
80
The in thing
python is badly confused here
gtgtgt 3'i' in 'alien' False
gtgtgt 'i' in 'team' False
but otherwise it seems pretty perceptive!
gtgtgt 'cs' in 'physics' True
Whats out here?
gtgtgt 'sleep' not in 'CS 5' True
gtgtgt 42 in 41,42,43 True
gtgtgt 42 in 42, '42' ??
a little bit different for lists
81
def sajak(s) if len(s) 0 return 0
elif s0 in 'aeiou' return 1
sajak(s1) else return sajak(s1)
Base Case
Rec. Steps
if it IS a vowel, the answer is 1 the number of
vowels in the rest of s
if it is NOT a vowel, the answer is just the
number of vowels in the rest of s
82
Good luck with Homeworks 0 and 1 !
The key to understanding recursion is to first
understand recursion
- advice from one of last year's students
83
def power(b,p) """ inputs base b and power
p (an int) implements bp """
if p 0 return else
return
Recursion is power!
84
behind the curtain
power(2,3)
85
def sajak(s)
when there are no letters, there are ZERO vowels
Base case?
Look at the initial character.
if it is NOT a vowel, the answer is
Rec. step?
if it IS a vowel, the answer is
86
def sajak(s)
when there are no letters, there are ZERO vowels
Base case?
Look at the initial character.
if it is NOT a vowel, the answer is just the
number of vowels in the rest of s
Rec. step?
if it IS a vowel, the answer is 1 the number of
vowels in the rest of s
87
def sajak(s) if s '' return 0
elif s0'a' or s0'e' or
Base Case
Checking for a vowel Try 1
88
behind the curtain
sajak('eerier')
89
Recursion not just numbers
Self-similarity elsewhere...
Natural phenomena
Relationships
how much here is leaf vs. stem?
What is an ancestor ?
Names / Acronyms
GNU "GNUs Not Unix"
90
Recursion not just numbers
Self-similarity elsewhere...
Natural phenomena
Relationships
What is an ancestor ?
An ancestor is a parent OR an ancestor of a
parent
Names / Acronyms
GNU
GNUs Not Unix
all stem!
91
Data and lists?
"sequences"
Many years later, as he faced the firing squad,
Colonel Aureliano Buendia was to remember that
distant afternoon when his father took him to
discover ice.
2, 3, 5, 7, 11
Sets
Text
Can all information be represented using lists ?
Sounds/Speech
Networks
Images/Video
Ideas?
92
crazy recursion
93
Recursion is the root of all computation since it
trades description for time.
- Alan Perlis
Recursion is the secret to all happiness in
writing short, powerful functional programs!
94
Monty Hall
Lets make a deal 63-86
Sept. 1990
inspiring the Monty Hall paradox
95
Problem 2 montyhall()
Write a program that plays the part of Monty
96
while we're here
import random comp random.choice( range(0,100)
) while True user input('Your choice ')
print user, '?' if user comp
print 'Yes!' break else
print 'No...'
97
"Quiz" on recursion
Names
def power(b,p)
def sajak(s)
""" returns b to the p power using recursion
and not inputs b and p are ints
output should be a float """
""" returns the number of vowels in the
input string, s """
sajak('wheel of fortune') 6
power(5,2) 25.0
What about y? You decide
Handle negative values of p, as well.
Want more power?
What five-letter English word w maximizes
sajak(w) ?
For example, power(5,-1) 0.2 (or so)
Want more Pat?
98
Any real computing with strings/lists?
Some strings are more equal than others
99
DNA
Our DNA's nucleotides
http//genome.ucsc.edu/cgi-bin/hgGateway
AGCT
100
DNA
Genetic disease expression
Myotonic Dystrophy
neuromuscular weakness in the face/torso
"Christmas Tree cataracts"
www.neuro.wustl.edu/neuromuscular/musdist/pe-eom.h
tmlmyd
CTG repeats
Its computational basis
in the 3' UTR (three prime untranslated region),
a particular section of messenger RNA
101
One sequence-checking algorithm
def check(geneseq) """ seeks evidence of
Myo. Dys. """ if 'ctg'50 in geneseq
return True else return False
checks if one string is contained in another
  • Can we make this algorithm more concise?
  • Why problems might this algorithm run into?
  • What information might we prefer to know?
  • What if we hadn't discovered the CTG-repeats yet?

102
One sequence-checking algorithm
def check(geneseq) """ seeks evidence of
Myo. Dys. """ if 'ctg'50 in geneseq
return 'Trouble' elif 'ctg'500 in
geneseq return 'BIG Trouble' else
return "You're OK!"
  • Will this work as a two-diagnosis system? Or is
    something awry?

103
One sequence-checking algorithm
def check(geneseq) """ seeks evidence of
Myo. Dys. """ if 'ctg'500 in geneseq
return 'BIG Trouble' elif 'ctg'50 in
geneseq return 'Trouble' else
return "You're OK!"
most constrained
least constrained
Feeling much better!
104
Any real computing with strings/lists?
Some strings are more equal than others
105
DNA
Our DNA's nucleotides
http//genome.ucsc.edu/cgi-bin/hgGateway
AGCT
106
DNA
Genetic disease expression
Myotonic Dystrophy
neuromuscular weakness in the face/torso
"Christmas Tree cataracts"
www.neuro.wustl.edu/neuromuscular/musdist/pe-eom.h
tmlmyd
CTG repeats
Its computational basis
in the 3' UTR (three prime untranslated region),
a particular section of messenger RNA
107
One sequence-checking algorithm
def check(geneseq) """ seeks evidence of
Myo. Dys. """ if 'ctg'50 in geneseq
return True else return False
checks if one string is contained in another
  • Can we make this algorithm more concise?
  • Why problems might this algorithm run into?
  • What information might we prefer to know?
  • What if we hadn't discovered the CTG-repeats yet?

108
Functioning in Python
Some basic, built-in functions
bool float int long list str
abs max min sum range round
absolute value
of lists
these change data from one type to another
creates lists
only as accurately as it can!
You call that a language?!
dir
help
These are the most important
109
Functioning in Python
Far more are available in separate files, or
modules
import math math.sqrt( 1764 ) dir(math)
accesses math.py's functions
lists all of math.py's functions
same, but without typing math. all of the time
from math import pi sin( pi/2 )
help() help modules
110
Functioning in Python
my own function! def dbl( x ) """ returns
double its input, x """ return 2x
This doesn't look quite right
111
Functioning in Python
my own function! def dbl( x ) """ returns
double its input, x """ return 2x
112
Functioning in Python
my own function! def dbl( x ) """ returns
double its input, x """ return 2x
keywords
Some of Python's baggage
def starts the function return stops it
immediately and sends back the return value
Docstrings
They become part of python's built-in help
system! With each function be sure to include one
that
Comments
  1. describes overall what the function does, and
  2. explains what the inputs mean/are

They begin with
113
Functioning in Python
def undo(s) """ this "undoes" its input, s
""" return 'de' s
gtgtgt yours undo('caf')
gtgtgt mine undo(undo('caf'))
strings, lists, numbers all data are fair game
Write a Comment
User Comments (0)
About PowerShow.com