Unblocking The Main Thread Solving ANRs and Frozen Frames
Class 28: Entropy
1. A giant python swallows an alligator in Everglades National Park, Fla..
Paul Zenk/PBS Nature “Invasion of the Giant Pythons” (yesterday’s New York Times)
cs1120 Fall 2011
David Evans
Class 28:
28 October 2011 Entropy
2. Plan
Story so Far: Liberal Arts Recap
Reversing in Python
Shannon’s Information Theory: Entropy
Returning Quiz 3, PS5, etc.
2
3. Ch 1: Computing
Ch 2: Language
Analysis
Synthesis
Ch 3: Programming
Ch 4: Procedures
Ch 5: Data
Ch 6: Machines
Ch 7: Cost
Ch 8: Sorting and Searching
PS5, Ch 9: State
You are PS6, Ch 10: Objects
Course Roadmap
here
PS7, Ch 11: Interpreters
Ch 12: Computability
PS8, 9
Intractability
4. cs1120 Main Topics
Language: How to describe information processes by
defining procedures (Chapters 3, 4, 5, 9, 10)
Programming with procedures, lists, recursion (PS1-4)
Programming with state (PS5), objects (PS6), languages (PS7)
Logic: How to predict properties about information
processes (Chapter 6, 7, 8)
Predicting how running time grows with input size
Are there problems which can’t be solved by algorithms? (Ch 12)
Machines: How to efficiently implement information
processes (Chapter 6) (not much on this)
Machines, Digital Logic
How to implement a Scheme interpreter (Ch 11, PS7)
6. Grammar: study of meaning in BNF, RTN, rules of
written expression evaluation for meaning
Trivium
Rhetoric: comprehension of verbal Interfaces between
components
and written discourse (PS6), evaluation rules
Logic: argumentative discourse for Rules of evaluation, if,
recursive definitions
discovering truth
Arithmetic: understanding adding with logic,
representing numbers
numbers
Quadrivium
Curves as procedures,
Geometry: quantification of space fractals (PS3)
Music: number in time Ada, GEB Chapter
Astronomy Nothing!
But…read the Neil DeGrasse Tyson essay!
10. Using Mutable Lists
>>> p = [1, 2, 3] Scheme Application:
>>> p[0] (<verb> <operands>)
1 Python procedure call:
>>> p[0] = 2 <verb>(<operand>)
>>> p Method call:
[2, 2, 3] <object>.<verb>(<parameters>)
>>> p.append(4) Note: this is different from mappend!
>>> p It take a single element, not a list.
[2, 2, 3, 4]
11. Trick Question
>>> p = (1, 2, 3)
>>> p[0] = 2
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
p[0] = 2
TypeError: 'tuple' object does not
support item assignment
15. Literal Translation
(define (mlist-reverse! p) def sreverse(p):
(if (null? (mcdr p)) if len(p) == 1:
(void)
return
(let ((rest (mcdr p))
(carp (mcar p))) else:
(mlist-reverse! rest) rest = p[1:]
(set-mcar! p (mcar rest)) first = p[0]
(set-mcdr! p (mcdr rest)) sreverse(rest)
(mlist-append!
p[0] = rest[0]
p
(mcons carp null))))) p[1:] = rest[1:]
p.append(first)
>>> p=[1,2,3]
This is correct, but no sane Python >>> sreverse(p)
programmer would do it this way! >>> p
[3, 2, 1]
16. Python for
Statement ::= (define (loop index result test update proc)
for Varible in Expression: (if (test index)
(loop (update index)
Block (proc index result)
test update proc)
result))
def gaussSum (n):
sum = 0 (define (gauss-sum n)
(loop 1 0 (lambda (i) (<= i n))
for i in range(1, n+1): (lambda (i) (+ i 1))
(lambda (i sum) (+ i sum))))
sum = sum + i
return sum
17. Reverse in Python
p:
1 2 3
Note: there is a built-in list method, p.reverse().
18. int(len(p) / 2) len(p) >> 1
def reverse(p):
for i in range(0, len(p) >> 1):
p[i], p[len(p) – 1 - i] = p[len(p) – 1 - i], p[i]