Comp Sci 101 Introduction to Computer Science Oct
- Slides: 22
Comp. Sci 101 Introduction to Computer Science Oct. 27, 2016 Prof. Rodger compsci 101 fall 16 1
Announcements • Next Reading and RQ due Nov 1 • Assignment 5 due today – Next Assignment out next week • APT 6 due Tues • Today: – Review nested loops, tuple generators – Focus on problem solving with sets compsci 101 fall 16 2
Review from last time: generator im. getdata(), accessing pixels • Returns something like a list – Use: for pix in im. getdata(): – Generates pixels on-the-fly, can't slice or index unless you use list(im. getdata()) – Structure is called a Python generator! – Saves on storing all pixels in memory if only accessed one-at-a-time compsci 101 fall 16 3
Review from last time Making Tuples and Generators • Overuse and abuse of parentheses – To create a tuple, use parentheses for pix in im. getdata(): (r, g, b) = pix npx = (255 -r, 255 -g, 255 -b) – To create a generator use parentheses as though creating a list comprehension! [2*n for n in range(10000)] (2*n for n in range(10000)) • See this in Py. Dev console 4
Set Operations from pictures bit. ly/101 f 16 -1027 -1 Question: Which operation does the red represent? A) C) D) B) E) compsci 101 fall 16 5
Problems – snarf set. Example. py • Given a list of strings that have the name of a course (one word), followed by last names (one word each) of people in the course: 1. Find total number of people taking any course 2. Find number of people taking just one course ["econ 101 Abroms Curtson Williams Smith”, "history 230 Black Wrigley Smith”, … ] Process data – create lists of strings of names for each course compsci 101 fall 16 6
Data for example [“compsci 101 Smith Ye Li Lin Abroms Black“, “math 101 Green Wei Lin Williams De. Long Noell Ye Smith”, “econ 101 Abroms Curtson Williams Smith”, “french 1 Wills Wrigley Olson Lee”, "history 230 Black Wrigley Smith” ] TO easier format to work with: [ [ ‘Smith’, ‘Ye’, ‘Lin’, ‘Abroms’, ‘Black’], [‘Green’, ‘Wei’, ‘Lin’, ‘Williams’, ‘De. Long’, ‘Noell’, ‘Ye’, ‘Smith’], [‘Abroms’, ‘Curtson’, ‘Williams’, ‘Smith’], …. ] compsci 101 fall 16 7
COMPSCI 101 ECON 101 Li Abroms Curtson Williams Ye Lin Smith MATH 101 Green Noell Wei Delong Yavatkar Black Wrigley HISTORY 230 Set Picture of Data FRENCH 1 Wills Lee Olson compsci 101 fall 16 8
COMPSCI 101 ECON 101 Li Abroms Curtson Williams Ye Lin Smith MATH 101 Green Noell Wei Delong Yavatkar Black Wrigley HISTORY 230 People in Comp. Sci 101 FRENCH 1 Wills Lee Olson compsci 101 fall 16 9
COMPSCI 101 ECON 101 Li Abroms Curtson Williams Intersection People Taking both Math And Comp. Sci Ye Lin Smith Green Noell Wei Delong Yavatkar Black Wrigley HISTORY 230 MATH 101 FRENCH 1 Wills Lee Olson compsci 101 fall 16 10
Part 1 – process. List bit. ly/101 f 16 -1027 -2 • Given a list of strings that have the name of a course (one word), followed by last names of people in the course: – Convert list into lists of strings of names for each course ["econ 101 Abroms Curtson Williams Smith", "history 230 Black Wrigley Smith", … ] [ [‘Abroms’, ‘Curtson’, ‘Williams’, ‘Smith’], [‘Black’, ‘Wrigley’, ‘Smith’, …] ] compsci 101 fall 16 11
Part 2 – people. Taking. Courses bit. ly/101 f 16 -1027 -3 • Given a list of lists of names, each list represents the people in one course: – Find total number of people taking any course – people. Taking. Courses should return unique list of names • Small Example [[‘Abroms’, ‘Curtson’, ‘Williams’, ‘Smith’], [‘Black’, ‘Wrigley’, ‘Smith’]] Answer is 6 unique names compsci 101 fall 16 12
COMPSCI 101 ECON 101 Li Abroms Curtson Williams Ye Lin Smith Wrigley HISTORY 230 MATH 101 Green Noell Wei Delong Yavatkar Black Total Number Is 17 unique names People taking Courses - Union FRENCH 1 Wills Lee Olson compsci 101 fall 16 13
Next, find the number of people taking just one course compsci 101 fall 16 14
COMPSCI 101 ECON 101 Li Abroms Curtson Williams Ye Lin Smith MATH 101 Green Noell Wei Delong Yavatkar Black Wrigley HISTORY 230 Union all sets But French 1 FRENCH 1 Wills Lee Olson compsci 101 fall 16 15
To solve this problem • First let’s write a helper function compsci 101 fall 16 16
Part 3 – union. All. Sets. But. Me bit. ly/101 f 16 -1027 -4 • Given example, a list of sets of strings, and the index of one of the sets, return the union of all the sets but that one example = [set(["a", "b", "c"]), set(["b", "c", "d", "g"]), set(["e", "d", "a"])] union. All. Sets. But. Me(example, 1) is set(["a", "b", "c", "e", "d" ]) compsci 101 fall 16 17
Part 4 – people. Taking. Only. One. Course bit. ly/101 f 16 -1027 -5 • Given a list of lists of strings of names representing people from courses – Find number of people taking just one course [[‘Abroms’, ‘Curtson’, ‘Williams’, ‘Smith’], [‘Black’, ‘Wrigley’, ‘Smith’, ‘Abroms’]] 4 compsci 101 fall 16 18
COMPSCI 101 ECON 101 Li Abroms Curtson Williams Ye Lin Smith MATH 101 Green Noell Wei Delong Yavatkar Black Wrigley HISTORY 230 People taking Only one course FRENCH 1 Wills Lee Olson compsci 101 fall 16 19
APT - Unique. Zoo • How do you solve this problem? • How is it similar to the problem we just solved compsci 101 fall 16 20
Example Data for Unique. Zoo ["zebra bear fox elephant", "bear crocodile fox", "rhino elephant crocodile kangaroo", "elephant bear"] fox zebra bear crocodile elephant rhino kangaroo compsci 101 fall 16 21
Unique. Zoo – two zoos have unique animals fox zebra bear crocodile elephant rhino kangaroo 22 compsci 101 fall 16
- Ap cs recursion
- Comp sci 110 northwestern
- Comp sci 1102
- Comp sci 1027
- Zjhz
- Comp 101 unc
- My favorite subject is
- Science fusion think central
- Introduction to computer science midterm exam
- Introduction to computer science midterm exam test
- Introduction to computer science quiz
- Python programming an introduction to computer science
- Tri quad pent hex
- October 2, 1869
- System czwórkowy
- October 3rd 1993
- Principle of oct
- Prop but pent
- Oct 31 sunset
- Premium sanitas
- Visante oct
- Cunyfirst
- Propiedades químicas del carbono