Duke CPS 006 - What will you do in Compsci 6 Today

Unformatted text preview:

Compsci 06/101, Fall 2010 4.1 What will you do in Compsci 6 Today?  Learn about selection and if/else statements  In context of solving problems  Understanding how to alter flow-of-control in Python  Vastly increasing the kinds of problems we can solve  Learn about looping over sequences  See above! Sequences will be strings, lists, images  Learn about lists  Humongously powerful structure for storing data Compsci 06/101, Fall 2010 4.2 Language and Problems in Context  Convert Romeo and Juliet to Piglatin  What else could we do with Shakespeare's plays?  How do we convert HTML to text?  Remove all $$ from salary data in a file  How do we make an image larger, more red, …  What is an image? How do read it? Convert it? Access it?  How do get the parts of an IP address 152.3.250.1  What are the parts? How do we access them?  How would a router use these parts? Compsci 06/101, Fall 2010 4.3 How do you solve a problem like …?  Solution from lab last week always returns -1? def firstVowelIndex(word): ai = word.find('a') ei = word.find('e') ii = word.find('i') oi = word.find('o') ui = word.find('u') return min(ai,ei,ii,oi,ui)  There is more than one way to skin a cat, but we need at least one way Compsci 06/101, Fall 2010 4.4 Python if statements and Booleans  In python we have if: else: elif:  Used to guard or select block of code  If guard is True then, else other  What type of expression used in if/elif tests?  ==, <=, <, >, >=, !=, and, or, not, in  Value of expression must be either True or False  Type == bool, George Boole, Boolean,  Examples with if  String starts with vowel  Rock, paper, scissors (!aka Rochambeau) winnerCompsci 06/101, Fall 2010 4.5 Pragmatics of using if, else, elif  Indentation in Python determines how blocks of code execute  What's in a function, what's not  What's guarded by if/else statement  Preview: what's in control of for loop  Nested blocks are hard to develop and reason about, but powerful  Sometimes factor nested blocks into other functions, for example see Uppity.py  Complex boolean expressions difficult to deal with  When is a year a leap year? When divisible by 4, unless divisible by 100 except when divisible by 400 Compsci 06/101, Fall 2010 4.6 Grace Murray Hopper (1906-1992)  “third programmer on world’s first large-scale digital computer”  US Navy: Admiral “It’s better to show that something can be done and apologize for not asking permission, than to try to persuade the powers that be at the beginning”  ACM Hopper award given for contributions before 35 2004: Jennifer Rexford 2009: Tim Roughgarden Compsci 06/101, Fall 2010 4.7 READ THIS NOW!  How do we convert Romeo and Juliet to all uppercase: WHEREFORE ART THOU ROMEO?  See Uppity.py for details, note how many functions have loops  We start with loops over sequences for element in sequence: #Process element  Sequences: Strings, files, lists, (more in future)  Sequences queryable with boolean in operator Compsci 06/101, Fall 2010 4.8 Sequences: Strings, Files, Lists  String is a sequence of characters which are essentially strings of length 1  Python does not have a character type  In Python type("rocket"[0]) is ?  File is a sequence of lines, each line is a string  Can process files in other ways, e.g., read entire file or process a fixed number of bytes/chars at a time  List is a sequence of elements  For example, returned by line.split() in Uppity.py  Lists are powerful abstractions and they are mutable!Compsci 06/101, Fall 2010 4.9 Anatomy of a Python List  Lists are sequences and indexable/subscriptable  What does this mean in Python?  Lists created by using list(..) as a function, similar to str(..) and int(..)  What is list("apple") and what is list(123)?  Lists created and returned by other functions, e.g., str.split() or range(..)  We'll see range later, not a list in Python 3  See list idiom in Uppity.py, create [], then append  Grow the list when appropriate Compsci 06/101, Fall 2010 4.10 Python loops and strings  Smelly code and how to fix it: firstVowelIndex  If you're duplicating code it's smelly  If you think there's a better way, it could be smelly  Smelly code should NOT always be fixed, sometimes you can simply call it odorific! Running right is most important! def firstVowelIndex(word): mi = len(word)+1 for v in "aeiou": if v in word: mi = min(mi,word.find(v)) return mi Compsci 06/101, Fall 2010 4.11 Looping and other idioms  Initialize a variable before a loop, update in loop  See vowel indexing code on previous slide: min • What about maximal element in a list?  Create list of "long" words (from list, file, other source)  This is filtering: selecting according to some criteria  Could also remove elements from list, changing list  Create new list v. alter existing list: benefits, tradeoffs?  Can we add up the digits of a number?  How can we index elements of a number?  Can we add up divisors of a number?  How do we loop over possibilities? Compsci 06/101, Fall 2010 4.12 Donald Knuth (b. 1938, Hopper '71)  Scholar, practitioner, artisan  Art of Computer Programming  Began effort in 1962 to survey entire field, still going  Writes beautiful code  Developed TeX to help typeset his books, widely used scientific document processing program  Many, many publications  First was in Mad Magazine  On the Complexity of Songs  Surreal NumbersCompsci 06/101, Fall 2010 4.13 It’s all relative and it depends I make the best bread in the city I make the best bread in the world I make the best bread in the universe I make the best bread on the block Compsci 06/101, Fall 2010 4.14 Richard Stallman (b.1953, Hopper '90)  "World's Best Programmer"  Gnu/Linux: g++  Believes all software should be free, but like “free speech”, not “free beer”  Won MacArthur award for his efforts and contributions  League for Programming Freedom • It's about free, not


View Full Document

Duke CPS 006 - What will you do in Compsci 6 Today

Download What will you do in Compsci 6 Today
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view What will you do in Compsci 6 Today and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view What will you do in Compsci 6 Today 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?