Compsci 101 Lists Loops Problems Owen Astrachan Kristin
Compsci 101, Lists, Loops, Problems Owen Astrachan Kristin Stephens-Martinez February 27, 2018 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 1
K is for … • Kernel • Core of the OS, Core for Machine Learning • Keyboard • Whether QWERTY or DVORAK, key • Key and (Key, Value) pair • Heart of a dictionary 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 2
PFLWo. F and FWo. M • Catching up with Last Class Examples • Iterating over files of lines • Iterating over files of characters • Tools for making programming easier • Sets: efficient alternative go list: unique • List Comprehensions: hidden accumulator 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 3
Administrivia • APTs due today • Reminder of required, what to fill in, how to help • Transform assignment • When it's due, when it's all out • Regrades on midterm and midterm grades • How to manage Gradescope and other grading Compsci 101, Spring 2018, Catchup, 2/27/2018 Sets, List Comprehensions 4
Review Code in Indexing. py • Running to test three solutions. Look up every possible key and keys that aren't in list • Timing is shown below • index 1 for 5000 = 1. 316 • index 2 for 5000 = 4. 474 • index 3 for 5000 = 13. 776 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 5
Lessons from Indexing Code? • The built-in Python functions are optimized • Execute using efficient low-level implementation • We use C-Python, there are other interpreters • Behind the scenes in Python • You do not need to know this, but … • Don't look behind the curtain! • https: //www. youtube. com/watch? v=ub. Ipo. Pj. BUds 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 6
WOTO http: //bit. ly/101 spring 18 -feb 20 -2 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 7
APT Tools and Concepts • These concepts transcend APT problems • Useful in many contexts • First immediate use? Solving APTs • Later? Other uses! • Indexed loops: counting instead of by element • Sets: avoid duplicates easily 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 8
Bag. Fitter • https: //www 2. cs. duke. edu/csed/pythonapt/bagfitter. html • How do you solve this problem by hand? ["x", "y", "z", "x", "z", "y"] • Strength = 2? Strength = 3? • If we have # occurrences… • Similar to cement trucks? 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 9
Pseudo code solution • Determine # occurrences of each unique item • Suppose these are in a list of int values • How many bags needed? Is it n//strength ? • Use numbers: n is 8, strength is 3 or 4 or 5 • Which should we tackle first? Need to debug • What problems have we seen before? 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 10
Testing in Eclipse • Using ideas from File. Frequency. py … • Create parallel lists: strings and ints • Strings in bags[] and # in counts[] • Maintain invariant that # occurrences of string in bags[k] is stored in counts[k] • When we see string seen before? • When we see new string? 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 11
From Pseudo to Code • Same idea we saw in File. Frequency. py • We can also change code below to use in bags rather than not in bags • Call. append(1) instead of. append(0) for item in food: if item not in bags: bags. append(item) # do something location = bags. index(item) counts[location] += 1 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 12
Testing the Module • Call function with example from description • Print different bags and their counts • Verify by hand/eye that this works • Debug and fix as needed, still not ready to test in online APT tester • More than one part? Test each separately. • After parts work? Integrate and test online 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 13
Helper Function • Given one number and strength, return # bags • Eerily similar to midterm trucks. Needed • More than one way to do this, ideas? • Suppose strength is 9, and we have 8 items? What about 9 or 10 items? • Can we use % and // • Can we modify value and just use // + 1? 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 14
Time to Do Another? • Eating Good vs. Eating Well? • https: //www 2. cs. duke. edu/csed/pythonapt/eati nggood. html • How do we guard against "double adding" • What else do we need to do to solve this? if name not in names: names. append(name) 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 15
TMTOWTSACat • For Eating. Good (or well) we had to avoid adding the same element more than once • Lists store duplicates • Sets do not store duplicates 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 16
List and Set, Similarities/Differences Function for List x. append(elt) Adding element len(x) Size of collection Combine collections x + y for elt in x: Iterate over elt in x Query element x. find(elt) Find element Function for Set x. add(elt) len(x) x | y for elt in x: elt in x CANNOT DO THIS • Lists are ordered and indexed, e. g. , first or last • Sets are not ordered, very fast, e. g. , if elt in x 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 17
Python Set Operators • Using sets and set operations often useful • Set intersection and set union • Other set operations • A | B, set union, A & B, set intersection • B – A, set difference, B ^ A, symmetric diff 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 18
Another Trip to the Sandwich. Bar • https: //www 2. cs. duke. edu/csed/pythonapt/sandw ichbar. html • What did this problem look like when first solved? for dex in range(len(orders)): if canmake(orders[dex], available): return dex 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 19
Given two lists A and B • Determine if all elements in A are also in B • Examine each element in A • If not in B? False • After examining all elements? True • What does this look like in code? Can we say "yes" or True? Before looking at all of A? 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 20
Given two sets A and B • Determine if all elements in A are also in B • if len(A & B) == len(A) • if len(A – B) == 0 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 21
WOTO http: //bit. ly/101 spring 18 -feb 27 -sets 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 22
Eugene (Gene) Myers • Lead computer scientist/engineer • Celera Genomics, and now Max-Planck • Lead team for: BLAST and WG-Shotgun "What really astounds me is the architecture of life. The system is extremely complex. It's like it was designed. … There's a huge intelligence there. " 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 23
Accumulator in one line • AKA List Comprehension • We will use a complete, but minimal version of list comprehensions, much more is possible def only. Pos(nums): ret = [] for n in nums: if n > 0: ret. append(n) return ret print(only. Pos([1, 2, 3, -1, -2, -3])) 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 24
List Comprehension • x = [1, 2, -1, -2, 3, 4, -3, -4] • y = [n for n in x if n > 0] def only. Pos(nums): ret = [] for n in nums: if n > 0: ret. append(n) return ret print(only. Pos([1, 2, 3, -1, -2, -3])) 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 25
General List Comprehension Syntax • Select certain elements from list [v_exp for v in list if bool_exp(v)] • Where v is any variable: all list elements in order • Where v_exp is any expression, could use v • Where bool_exp(v) is a boolean expression 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 26
List Comprehension Examples [w for w in words if w. count('e') == 0] • What does this represent? [v*2 for v in range(6) if v % 2 == 1] • What does this represent? sum([1 for x in words if len(x) > 4]) • What does this represent? 2/27/2018 Compsci 101, Spring 2018, Catchup, Sets, List Comprehensions 27
- Slides: 27