CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
Dictionaries: Keeping track of pairs
By taking the PSAT and the PLAN, you have already taken your first steps toward college. Both tests show you the kinds of reading, math and writing skills.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
HW 1: Problems 3 & 4 EC 1 & 2.
Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions.
Need your MyMathLab card with your access code Need a Valid Address Need to know Purdue’s zip code is and your course ID for your Class You.
Main task -write me a program
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
CSCI 200 Introduction To Programming with Visual Basic Bob Bradley.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Better time management NQT Day 11/10/11 Andy Fitt – Assistant Principal, Itchen 6 th Form College (with thanks to Liz Petheram for ideas)
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
If statements while loop for loop
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Built-in Data Structures in Python An Introduction.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Loops.  (No Quiz)  Hand in Assignment #1  Last chance for Q+A on the midterm  Loops 2.
Python Let’s get started!.
Welcome back to Life Science! Please sit in the same seat as yesterday! Materials Check: 1.Notebook 2.Dry erase marker (Please get them out and be ready.
Ionut Trestian Northwestern University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Please feel free to chat until the seminar begins at the top of the hour!
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
CISC105 – General Computer Science Class 4 – 06/14/2006.
IS 313 Today Schedule Week 0: files dictionaries You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where we've been… Where.
EECS 110: Lec 13: Dictionaries Aleksandar Kuzmanovic Northwestern University
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
CS100 - PYTHON – EXAM 2 REVIEW -ONLY THE VITAL STUFF- PYTHON STRING METHODS, LOOPS, FILES, AND DICTIONARIES.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Reading Files >>> f = file( 'a.txt' ) >>> text = f.read() >>> text 'This is a file.\nLine 2\nLast line!\n' >>> f.close() In Python reading files is no.
EECS 110: Lec 14: Classes and Objects
CS this week 4 Building classes vs. using the library hw10pr2
EECS 110: Lec 13: Dictionaries
EECS 110: Lec 12: Mutable Data
EECS 110: Lec 17: Review for the Final Exam
Python Let’s get started!.
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Markov Model a language processing model
Files and Dictionaries
Engineering Innovation Center
CS 240 – Lecture 11 Pseudocode.
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Files and Dictionaries
Ionut Trestian Northwestern University
I210 review.
Introduction to TouchDevelop
IST256 : Applications Programming for Information Systems
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
CS 1111 Introduction to Programming Fall 2018
CS this week 4 Building classes vs. using the library hw10pr2
Northwestern University
EECS 110: Lec 12: Mutable Data
Files and Dictionaries
Presentation transcript:

CS 5 Today Today: Midterm Exam #2 M/T 11/16-17/09, in class Life in 3d? filesdictionaries David Miller ! but there is an optional, ex. cr. circuit-building lab that day! hw9pr3 ! Looking ahead: No class W, 11/25/09 Want more 3d Life?

Visuospatial Skills We use visuospatial skills in everyday life. Most importantly, visuospatial skills are a significant predictor of success in technical fields. Imagine graphics software for instance. Power to predict grades in technical courses equals that of math SAT scores (Sorby, 2009). Bundled multimedia software and workbook developed at Michigan Tech teaches these skills. Multimedia is being implemented at 6 other institutions nationwide including Purdue, Virginia Tech, Penn State – Behrend using a $200,000 NSF grant awarded in Three dimensions are better than two – I should know!

Visuospatial skills can be taught and learned. Improving these skills leads to improved grades I have $5,000 to (1) give to you, (2) help you learn these skills, and (3) improve grades…

MandelGallery!

Infernalicious! also:

Files >>> f = file( 'a.txt' ) >>> text = f.read() >>> text 'This is a file.\nLine 2\nLast line!\n' >>> f.close() In Python reading files is no problem… opens the file and calls it f reads the whole file and calls it f use text.split() for a list of each raw word closes the file (closing Python does the same) use split as needed… for example, text.split( '\n' )

Files >>> f = file( 'a.txt', 'w' ) >>> print >> f, "New stuff!" >>> f.close() In Python writing files is also concise… opens the file f for writing… outputs new stuff to the file f closes the file to be sure it's been saved to the filesystem… New stuff! You'll need to close and re-open the file to see the changes…

Lists vs. Dictionaries If I had a dictionary, I guess I could look up what it was! Lists are not perfect… L L[0]L[1] reference 5 42

Lists vs. Dictionaries Lists are not perfect… L[1990] = 'horse' You can't choose what to name data. You have to start at 0. L[0], L[1], … L[1991] = 'ram' L L[0]L[1] reference 5 42 If I had a dictionary, I guess I could look up what it was! Some operations can be slow for big lists … if 'rooster' in L:

Dictionaries In Python a dictionary is a set of key - value pairs. It's a list where the index can be any immutable key. >>> d = {} >>> d[1990] = 'horse' >>> d[1991] = 'ram' >>> d {1990: 'horse', 1991: 'ram} >>> d[1991] 'ram' >>> d[1969] key error This seems like the key to dictionaries' value… creates an empty dictionary, d 1990 is the key 'horse' is the value 1991 is the key 'ram' is the value Retrieve data as with lists… Almost ! Curly! And colony!

More on dictionaries Dictionaries have lots of built-in methods, or functions: >>> d = {1990: 'horse', 1991: ram'} >>> d.keys() [ 1990, 1991 ] >>> d.has_key( 1991 ) True >>> d.has_key( 1969 ) False >>> d.pop( 1990 ) 'horse' They don't seem moronic to me! pop deletes a key and returns its value has_key checks if a key is present keys returns a list of all keys

There's madness in this method ! Methods Functions d.has_key( 1991 ) are functions that are called by the data itself! has_key( 1991, d ) all data must be passed in as function inputs… are called on their own… Warning: this has_key function is for example purposes only. It does not exist! This has_key method is built-in to all objects of type dictionary.

Word count. def wordCount( filename = None ): """ creates and returns a list of words """ text = '' if filename == None: print "Enter lots o' text. End with a plain '42' line." while True: nextline = raw_input() if nextline == '42': break text += nextline + ' ‘ else: f = file( filename, 'r' ) text = f.read() # text is now a bunch of space-separated words list_of_words = text.split() # split splits a string print "The list of words is", list_of_words num_words = len( list_of_words ) print "There are", num_words, "words." return list_of_words Keyboard File split Default inputs…

Vocabulary count! # see wordCount for the set-up stuff D = {} # an empty dictionary for w in list_of_words: # check each word # How could/should we “clean up” w ? if D.has_key( w ) == False: # w was not there D[w] = 1 else: # d.has_key( w ) == True, so w IS there D[w] += 1 print “There are ", len( D ), "distinct words." return D NEW word - set to 1 OLD word - add by 1

Vocabularists? Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works) Active vocabulary estimates range from 10,000-60,000. Passive vocabulary estimates are much higher.

Vocabularists? Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works) Many Shakespearean contributions: Shakespeare Active vocabulary estimates range from 10,000-60,000. Passive vocabulary estimates are much higher. Contemporary author in the OED… which word… Any guesses?

Vocabularists? Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works) Shakespeare J. K. Rowling Active vocabulary estimates range from 10,000-60,000. Passive vocabulary estimates are much higher. Many Shakespearean contributions:

A challenge… def provinceChallenge( prov ): """ prov is a dictionary of Canada's provinces -- the challenge is to name them all! """ while 0 in prov.values(): guess = raw_input("Name a province: ") if prov.has_key( guess ) == False: print 'Try again...' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...' print 'Phew!' prov = { 'BC': 0, 'AB': 0, … } help?! All of Canada's provinces are in this dictionary…

A family dictionary?

A family dictionary… T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']} keys can be any immutable type values can be any type at all… T['abe'] How to get 'selma' from T ?

A functional family? def favChild( person, Tree ): """ person is a name (a string) Tree is a dictionary of children returns person's "favorite" child """ if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] return None Who is favored ? sort has side effects !

A functional family? def addChild( person, Tree, jr ): """ adds person's new child to Tree """ if Tree.has_key( person ) == False: else: # already in the Tree! For example, >>> addChild( 'lisa', T, 'abejr' )

“Quiz” Change this code so that it counts how many times you've guessed each item - as the dictionary value - both for real provinces and for incorrect guesses… Based on favChild (above), write favGChild to return the first grandchild alphabetically - or return None if there are none. To consider: Is the fav. child of the fav. child necessarily the fav. grandchild by this definition? def provinceChallenge( prov ): while 0 in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again...' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...' def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] return None def favGChild( person, Tree ): Name(s):

Change this code so that it tells you how many times you've guessed the same province… def provinceChallenge( prov ): while 0 in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again...' prov[guess] = 1 elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...' prov[guess] += 1 = += new item? existing item?

Based on favChild, write favGChild to return the first grandchild alphabetically - or return None if there are none. def favGChild( person, Tree ): GC = [] if Tree.has_key( person ): for ch in Tree[person]: if Tree.has_key( ch ): GC += Tree[ ch ] if GC != []: GC.sort() return GC[0] return None def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] return None our list of GChildren Accumulate !

Name that author… !

Markov Model The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model: 1st-order Markov Model

Markov Model { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], … 'ben' : The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model: 1st-order Markov Model

Markov Model { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model:

Markov Model { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat'] The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model: How to get to I?

Markov Model { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat'], '$' : ['I', 'I', 'I'], The text file: I like spam. I like toast and spam. I eat ben and jerry's ice cream too. Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. The Model: sentence-starting string

Generative Markov Model Technique for modeling any sequence of natural data Each item depends on only the item immediately before it. Original text: A key benefit is that the model can generate feasible data! I like spam. I like spam. I like toast and jerry's ice cream too. I like spam. I like toast and spam. I eat ben and jerry's ice cream too.

How to generate: 1) start with the '$' string 2) choose a word following '$', at random. Call it w 3) choose a word following w, at random. And so on… 4) If w ends a sentence, '$' becomes the next word. I like spam. I like spam. I like toast and jerry's ice cream too. { 'toast': ['and'], 'and' : ['spam.', "jerry's"], 'like' : ['spam.', 'toast'], 'ben' : ['and'], 'I' : ['like', 'like', 'eat'], '$' : ['I', 'I', 'I'], … } The Model:

WM-SCI

WMSCI 2005 Markov-generated submission accepted to WMSCI

WMSCI 2005

And a Markov presentation! ( in costume… )

Have a great weekend! Perhaps a strategy for your next Hum paper? Farewell video…CS 5 (part 3) + CS 5 (part 2)

Life, revisited Golly!

Representation & Speed finding if a value is in a list of 10,000 elements… looking up a key in a dictionary of 10,000 entries vs. listdictionary L L[0]L[1] reference 5 0 L[9999] 42 … {1988: 'dragon', 1989: 'snake'}

value-added tts() options…

Problem 1 -- to  and beyond! Are there stable life configurations? Are there oscillating life configurations? Are there self-propagating life configurations? "rocks" "plants" "animals" period 3 period 2

Problem 1 -- to  and beyond! Are there life configurations that expand forever? What is the largest amount of the life universe that can be filled with cells? How sophisticated can the structures in the life universe be? Are all feasible configurations reachable?

This is but ourselves. No, faith, My uncle! O royal bed of confession Of your rue for leave to nature; to this time I should weep for thy life is rotten before he is. have sworn 't. Or my blood. I have closely sent for nine; and unprofitable, 'Cause somethin' like he left knee and a harp," said he had to the whole school? The shouting and then some strange and Mrs. "Well, I know Hagrid; they spotted handkerchief and get him get rid of course, had a gigantic beet with her," he knew what to all he's The Senators and the date of a written declaration that Purpose, they shall consist of nine States, shall not, when he shall have such Vacancies. The President pro tempore, in the Desire of a Qualification to the Speaker of the Senate. Article 6. When vacancies by the office upon probable Name that author… ? All the sky with the sun in the sun in the church where you're gone Lucy in my eyes. There beneath the girl with an hourglass And then the banker never wears a lot to hold your hand. Can't buy me tight, tight Owww! Love is love I can't hide, Who is the author?What is the work?What is going on?

Exam on Mon. Exam topics Design chapter: 1d loops 2d loops Hmmm for & while for row… for col… add r2 r2 r1 Basics variables, functions, etc. You may bring a page of notes, double-sided with anything you would like on it. I'd suggest (1) reminding yourself about the HW problems and (2) looking at the class "quizzes"…

Change this code so that it tells you how many times you've guessed the same province… def provinceChallenge( prov ): while '0' in prov.values(): guess = raw_input("Guess: ") if prov.has_key( guess ) == False: print 'Try again...' elif prov[guess] == 0: print 'Yes!' prov[guess] += 1 else: print 'Already guessed...' first, for real provinces then, for incorrect guesses…

Based on favChild, write favGChild to return the first grandchild alphabetically - or return 'no one' if there are none. def favGChild( person, Tree ): GC = [] def favChild( person, Tree ): if Tree.has_key( person ): Kids = Tree[person] Kids.sort() return Kids[0] else: return 'no children' our list of GChildren

Name that author… ?