Compsci 101 Files Lists of Lists Owen Astrachan

  • Slides: 21
Download presentation
Compsci 101, Files, Lists of Lists Owen Astrachan Kristin Stephens-Martinez February 8, 2018 2/8/2018

Compsci 101, Files, Lists of Lists Owen Astrachan Kristin Stephens-Martinez February 8, 2018 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 1

H is for … • HTTP • A Protocol we use every day, and

H is for … • HTTP • A Protocol we use every day, and HTTPS • Hello World • The quintessential first program: 40 years ago! • Hacker, Hacktivism, Hack Duke • Hashing • How Dictionaries work 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 2

PF Thursday • Files as sources of data • Reading … using open and

PF Thursday • Files as sources of data • Reading … using open and close • Processing … creating lists of lines • Lists and useful idioms: . index, in, slicing • May apply to sequences including strings • Looping using indexes and range • Why this is useful: on beyond turtles 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 3

Administrivia • APT Quiz is over, what does that mean • Data from students

Administrivia • APT Quiz is over, what does that mean • Data from students • APT 2 is ongoing, some help in class today • Assignment 2/Turtles is out: Partners allowed • Midterm next week, practice midterm out today 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 4

APT 2 Ideas • Is. Special: • https: //www 2. cs. duke. edu/csed/pythonapt/spe cial.

APT 2 Ideas • Is. Special: • https: //www 2. cs. duke. edu/csed/pythonapt/spe cial. html • How to do this by hand, how to translate to code • Score. It: • https: //www 2. cs. duke. edu/csed/pythonapt/yah tzee. html 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 5

How many items are special/edible? • lovely(ingredients, inedible) • returns 2, why? How to

How many items are special/edible? • lovely(ingredients, inedible) • returns 2, why? How to do by hand? ingredients = "pork shrimp marshmallow" inedible = "asparagus shrimp lobster crayfish bacon" • Write down ideas. Can you use for-each? Can you avoid for-each? Think abstractly! • Once we have ideas: translate to code • Why is split a good idea? • How do we know if element in a sequence? 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 6

Element in sequence • contains("doggeral", "d") ? • contains([1, 2, 3, 4], 5) ?

Element in sequence • contains("doggeral", "d") ? • contains([1, 2, 3, 4], 5) ? • Write helper function • Easier to read • Easier to reason def contains(seq, elt): for one in seq: if one == elt: return True return False • Use Python in idiom "ant" in ["ant", "flipper", "house"] 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 7

Getting to All Green • Which do you prefer? gredlist = ingredients. split() badlist

Getting to All Green • Which do you prefer? gredlist = ingredients. split() badlist = inedible. split() count = 0 for one in gredlist: if one not in badlist: count = count + 1; count = 0 for one in ingredients. split(): if one not in badlist. split(): count = count + 1; 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 8

Score. It • max. Point([4, 4, 3, 3, 3]) and why? • How do

Score. It • max. Point([4, 4, 3, 3, 3]) and why? • How do you do this by hand? [6, 1, 1, 1, 2] • Should we see how 5 scores? 1 scores? def max. Points(toss): best = 0 for d in [1, 2, 3, 4, 5, 6]: sc = getscore(toss, d) if sc > best: best = sc return best 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 9

Helper functions • Using the getscore function when writing solution • Arguably easier to

Helper functions • Using the getscore function when writing solution • Arguably easier to verify it's correct • We can write and verify getscore separately def getscore(rolls, die): pts = 0 for d in rolls: if d == die: pts = pts + die return pts 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 10

Knowing Built-in Functions • Can we calculate number of 5's in a list? •

Knowing Built-in Functions • Can we calculate number of 5's in a list? • Can we calculate number of z's in a file? def getscore(rolls, die): return rolls. count(die)* die • Can we simply embed expression in the code, rather than in a helper function? • Of course! But if you forget. count(. . )? 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 11

Slicing Ideas (for APTs) • What is lst[1: ] and lst[1: -1] • All

Slicing Ideas (for APTs) • What is lst[1: ] and lst[1: -1] • All but first, all but first and last • See APT Last. Name. First • Don't forget indexing lst[0], lst[-1] 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 12

WOTO http: //bit. ly/101 spring 18 -feb 8 -1 2/8/2018 Compsci 101, Spring 2018,

WOTO http: //bit. ly/101 spring 18 -feb 8 -1 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 13

Donald Knuth • Until recently … • Was "The Donald" • Turing, Hopper, more

Donald Knuth • Until recently … • Was "The Donald" • Turing, Hopper, more … • Author of many works Art of Computer Programming Mad Magazine when 19 (author of Te. X) “I can’t go to a restaurant and order food because I keep looking at the fonts on the menu. ” 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 14

Text File Processing Idioms • See module File. Stuff. py in download for Feb

Text File Processing Idioms • See module File. Stuff. py in download for Feb 6 • Newline ’n’ is read, call. strip() • Break line into “words”, call. split() • Process the list returned by. split() • May need to convert strings to int or float or … • The for line in f: idiom is efficient • Contrast list returned by f. readlines() 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 15

Lists of Data • String lists: ["ant", "fox", "cat", "dog"] • Lists of int/float

Lists of Data • String lists: ["ant", "fox", "cat", "dog"] • Lists of int/float numbers: [5, 3. 14159, -15] • What about lists of lists? Variable plist = [["Washington", 1789, 57], ["Clinton", 1993, 46]] • What is plist[0]? • What is plist[0][2]? • Can always use a variable: x = plist[0] 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 16

YALALA • Yet Another Look At Loop and Accumulate • Initialize, Update, Finalize •

YALALA • Yet Another Look At Loop and Accumulate • Initialize, Update, Finalize • Before loop, In Loop, After Loop • Do we care about efficiency with lists? It depends! • Quick look at List. Changer. py • Differences +[val]and. append(val) • Total Storage created in build. Plus? Why? • This is what we study in detail in Compsci 201 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 17

Does efficiency matter? • It depends • Try to indicate what it depends on!!

Does efficiency matter? • It depends • Try to indicate what it depends on!! • Should you call turt. hide. Turtle() ? • Runs faster! Downside? See turt. speed(0) • Should you use. append(x) instead of + [x]? • Yes, unless you want to make a copy 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 18

Range Examples • Access all the values in a list to print them •

Range Examples • Access all the values in a list to print them • Use the “for each” idiom • Use an index to access ith element • Names? xx = [“ant”, “bat”, “cat”, “dog”] for a in xx: print(a) xx = [“ant”, “bat”, “cat”, “dog”] for i in range(len(xx)) print(xx[i]) 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 19

Range idioms • When to loop using indexes aka indices • Parallel lists? words

Range idioms • When to loop using indexes aka indices • Parallel lists? words = ["dog", "cat", "elf"] counts = [2, 3, 1] word = input("what> ") if dex in words: for dex in range(len(words)): if words[dex] == word: counts[dex]=counts[dex] + 1 else: words. append(word) counts. append(10 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 20

WOTO http: //bit. ly/101 spring 18 -feb 6 -2 • Why doesn’t query Logo

WOTO http: //bit. ly/101 spring 18 -feb 6 -2 • Why doesn’t query Logo Turtle work? 2/8/2018 Compsci 101, Spring 2018, FIles, Lists of Lists 21