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 2011David Evans Class 28:28 October 2011 Entropy
PlanStory so Far: Liberal Arts RecapReversing in PythonShannon’s Information Theory: EntropyReturning Quiz 3, PS5, etc. 2
Ch 1: Computing Ch 2: LanguageAnalysis 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
cs1120 Main TopicsLanguage: 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)
Grammar: study of meaning in BNF, RTN, rules of written expression evaluation for meaningTrivium 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 numbersQuadrivium 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!
Using Mutable Lists>>> p = [1, 2, 3] Scheme Application:>>> p (<verb> <operands>)1 Python procedure call:>>> p = 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]
Trick Question>>> p = (1, 2, 3)>>> p = 2Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> p = 2TypeError: tuple object does notsupport item assignment
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 (set-mcdr! p (mcdr rest)) sreverse(rest) (mlist-append! p = rest 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]
Python forStatement ::= (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
Reverse in Python p: 1 2 3 Note: there is a built-in list method, p.reverse().
int(len(p) / 2) len(p) >> 1def reverse(p): for i in range(0, len(p) >> 1): p[i], p[len(p) – 1 - i] = p[len(p) – 1 - i], p[i]
Are Randall Munroe’s entropy estimates correct?Left for course blog discussion. 27
0.9880135796356116381290864023063602427958811193759308857790... Class Sorting using “Odette” Assuming alphabet randomly distributed: O is 15th out of 26 letters in alphabet = 0.988… 28
0.9880135796356116381290864023063602427958811193759308857790... Class Sorting using “Odette” The real class: 41 out of 49 students have names before “Odette”! -((41/49) * log_2 (41/49) + (7/49) * log_2 (7/49)) = 0.6162235892609252 29
Better Class Sorting Front of Classcrm Caroline Hannah – Li- - Gregory Chang Anthony - mdc Michael Casey - Yuan 30
ChargeOffice hours: Peter – right now Sunday, 1-6pm Everyone should be making progress on PS6Monday: Trick-or-Treat Protocols! Inheritance