CSE 111 Bio: Program Design I Lecture 17: software development, list methods

Similar documents
CS 111: Program Design I Lecture 16: Module Review, Encodings, Lists

Python Programming: An Introduction to Computer Science

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

CS 111: Program Design I Lecture 15: Objects, Pandas, Modules. Robert H. Sloan & Richard Warner University of Illinois at Chicago October 13, 2016

CS 111: Program Design I Lecture 21: Network Analysis. Robert H. Sloan & Richard Warner University of Illinois at Chicago April 10, 2018

CS 111: Program Design I Lecture 15: Modules, Pandas again. Robert H. Sloan & Richard Warner University of Illinois at Chicago March 8, 2018

CS 111: Program Design I Lecture 20: Web crawling, HTML, Copyright

CSE 111 Bio: Program Design I Class 11: loops

Python Programming: An Introduction to Computer Science

Lecture 9: Exam I Review

CS 111: Program Design I Lecture # 7: Web Crawler, Functions; Open Access

CS 11 C track: lecture 1

CS 111: Program Design I Lecture #26: Heat maps, Nothing, Predictive Policing

One advantage that SONAR has over any other music-sequencing product I ve worked

. Written in factored form it is easy to see that the roots are 2, 2, i,

CS 111: Program Design I Lecture 5: US Law when others have encryption keys; if, for

CS 111 Green: Program Design I Lecture 27: Speed (cont.); parting thoughts

The number n of subintervals times the length h of subintervals gives length of interval (b-a).

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

How do we evaluate algorithms?

Ones Assignment Method for Solving Traveling Salesman Problem

CS 111: Program Design I Lecture 14: Encodings & Files concluded; Pandas, Modules, legal data analytics

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Exercise 6 (Week 42) For the foreign students only.

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 111: Program Design I Lecture 20: Web crawling, HTML, Copyright

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

CMPT 125 Assignment 2 Solutions

Normal Distributions

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Computers and Scientific Thinking

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

CSE 417: Algorithms and Computational Complexity

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

Administrative UNSUPERVISED LEARNING. Unsupervised learning. Supervised learning 11/25/13. Final project. No office hours today

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Package RcppRoll. December 22, 2014

Exceptions. Your computer takes exception. The Exception Class. Causes of Exceptions

condition w i B i S maximum u i

quality/quantity peak time/ratio

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

Arithmetic Sequences

IMP: Superposer Integrated Morphometrics Package Superposition Tool

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 4 The Datapath

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

CS 111: Program Design I Lecture 19: Networks, the Web, and getting text from the Web in Python

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CMSC Computer Architecture Lecture 12: Virtual Memory. Prof. Yanjing Li University of Chicago

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

The isoperimetric problem on the hypercube

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Data Structures and Algorithms Part 1.4

CIS 121. Introduction to Trees

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

UNIT 4C Iteration: Scalability & Big O. Efficiency

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

NTH, GEOMETRIC, AND TELESCOPING TEST

Lecture 1: Introduction and Strassen s Algorithm

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5

Lecture 5. Counting Sort / Radix Sort

IS-IS in Detail. ISP Workshops

ISSN (Print) Research Article. *Corresponding author Nengfa Hu

3. b. Present a combinatorial argument that for all positive integers n : : 2 n

Lower Bounds for Sorting

SAMPLE VERSUS POPULATION. Population - consists of all possible measurements that can be made on a particular item or procedure.

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

The golden search method: Question 1

S I S I * AMS2 REPORTS MANUAL * RELEASE 10.0

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Threads and Concurrency in Java: Part 1

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

Threads and Concurrency in Java: Part 1

Numerical Methods Lecture 6 - Curve Fitting Techniques

Mathematics. Programming

SD vs. SD + One of the most important uses of sample statistics is to estimate the corresponding population parameters.

Counting the Number of Minimum Roman Dominating Functions of a Graph

CS 683: Advanced Design and Analysis of Algorithms

GE FUNDAMENTALS OF COMPUTING AND PROGRAMMING UNIT III

The Open University, Walton Hall, Milton Keynes, MK7 6AA First published 2004

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Transcription:

CSE 111 Bio: Program Desig I Lecture 17: software developmet, list methods Robert H. Sloa(CS) & Rachel Poretsky(Bio) Uiversity of Illiois, Chicago October 19, 2017

NESTED LOOPS: REVIEW

Geerate times table for ay 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 =4 1 for i i rage(1,+1): 2 for j i rage(1,+1): 3 prit(i*j,ed=' ') 4 prit() To start pritig colum oly at some k > 1 (because I kow my 1's time tables): A. Edit oe umber i the rage statemet i lie 1 (for i) B. Edit oe umber i the rage statemet i lie 2 (for j) C. Both A ad B D. Edit both umbers i the rage statemet i lie 1 (for i) E. Edit both umbers i the rage statemet i lie 2 (for j)

INCREMENTAL DEVELOPMENT, ETC.

How to get hired by Google From the Google HSI CS Faculty Summit Have codig ability i oe of Pytho, Java, or C++ ad be able to demostrate i solvig iterview questio live q Do't go directly to writig code: First discuss with iterviewer to show you ca thik about it, ad to cofirm you uderstood the questio

Icremetal Programmig Extesio of our geeral philosophy: q q q Desig first! Desig top dow Implemet bottom up Implemet bottom up ofte meas buildig workig partial program of, say, at most 20 lies (less if i CS 111!) ad gettig it implemeted ad tested first

Read i CSV ad plot 3 species Maybe first: q q q Read i CSV ad get x's ad y's ad just prit out those two lists Next might be read i CSV ad just do oe x-y plot of all the poits, without splittig out the 3 species Fially do the assiged problem

Watch the master (Prof. Sloa, of course) def plot_icremet(fame, x, y): """Icremetal start o Iris lab; grabs ad prits x ad y""" xlist = [] ylist = [] with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: xlist.apped(float(row[x])) ylist.apped(float(row[y])) prit(xlist) prit(ylist) plot_icremet("bezdekiris.csv", 0, 1) Traceback (most recet call last): File "<ipytho-iput-31-c83ba8cc3347>", lie 1, i <module> plot_icremet("bezdekiris.csv", 0, 1) File "/Users/robertsloa/Google Drive/CS Plus Bio Course 111 Gree/CS111Gree-FA2017/Programmig/slides.plotIcremet.py", lie 18, i plot_icremet xlist.apped(float(row[x])) IdexError: list idex out of rage

@#$%%!! Oh, ca I ot say that i class? First thig to check: The error message, the last bit: what's it tellig us? IdexError: list idex out of rage

So, idex to list out of rage??? def plot_icremet(fame, x, y): """Icremetal start o Iris lab; grabs ad prits x ad y""" xlist = [] ylist = [] with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: xlist.apped(float(row[x])) ylist.apped(float(row[y])) prit(xlist) prit(ylist)

Whe i doubt Start pritig out variables to see what is goig o (Or use upper right pael of Spyder to do the same)

So, pritig def plot_icremet(fame, x, y): """Icremetal start o Iris lab; grabs ad prits x ad y""" xlist = [] ylist = [] with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: prit(row) xlist.apped(float(row[x])) ylist.apped(float(row[y])) prit(xlist) prit(ylist)

We get ['5.1', '3.5', '1.4', '0.2', 'Iris-setosa'] ['4.9', '3.0', '1.4', '0.2', 'Iris-setosa'] ['4.7', '3.2', '1.3', '0.2', 'Iris-setosa'] ['6.2', '3.4', '5.4', '2.3', 'Iris-virgiica'] ['5.9', '3.0', '5.1', '1.8', 'Iris-virgiica'] []

What happeed? Probably a last lie that had a blak, or maybe that's just how it eds Wat to stop that loop from processig a empty list break or probably better cotiue

break & cotiue break: Causes immediate termiatio of the loop Use carefully! It ca make code very hard to read cotiue: causes this iteratio to be skipped q Need a reaso to use it, but it ca be helpful i some situatios

Fixig our problem with cotiue with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: if le(row) == 0: # empty? cotiue #regular processig here

Which of these will exit o 9? A x = iput ("Eter a umber: ") while (x%2 == 1 ad x%3 == 0): x = iput ("Eter a umber: ") B x = iput ("Eter a umber: ") while True: if (x%2 == 1 ad x%3 == 0): break x = iput ("Eter a umber: ") C. Both D. Neither E. I do t kow

LIST FUNCTIONS AND SOME REVIEW

Next big project: Populatio Geetics We will see how a populatio evolves by simulatio, hugely successful ad powerful method for doig moder sciece Classical Medelia geetics We'll start with a small populatio represeted as hmm, somethig, i a list q Ad keep buildig that list Today: We will build our list-fu!

Populatio geetics simulatio List of cotaiig iitial populatio while ot at some ed coditi cocerig list: q q q q q Take (1? a matig pair? all?) elemets from the list Usig radom ad biology, determie offsprig (Maybe?) remove breeders from list (geeratio dies) Add ewly bred items to list Get all the liks i that page Compute ad retur (or prit out?) appropriate stats o fial populatio results of "dry lab" experimet

Ad besides Lists i Pytho are just awesome

Lists versus Strigs List Elemets ca be ay type Mutable Heterogeeous elemets Ca be ested i other lists Strig Elemets are characters Immutable Homogeous elemets

Recall Lists are data structures that let us store collectios of data i sequece with idices

Remember the smallest "" # Empty strig, could also be writte '' [] # empty list

A = [2,3,5] B = ["Medel"] C = A + B prit(c) will result i: A. [2,3,5] B. [2,3,5,"Medel"] C. ["2","3","5","Medel"] D. This will cause a error E. I do t kow

List Fuctios These are ot methods; there are also list methods, ad we'll do those i a few miutes (have already see ad used.apped()) le: legth of a list (i.e., umber of elemets) + will cocateate lists mi, max: miimum or maximum of list sum: sum of the elemets i the list (if all umbers) q E.g., sum([2, 3, 5]) à 10

Importat: Lists are mutable >>> years= [1788, 1800, 1860, 1932] >>> years[0] = years[0] + 4 >>> prit(years) [1792, 1800, 1860, 1932]

What is prited lst = ['abc.com', 'c.com', 'msbc.com'] lst[1] = 'fox.com' prit(le(lst)) A. 3 B. 4 C. 23 D. 30 E. No output; error i 2 d lie of code

What is prited lst = ['abc.com', 'c.com', 'msbc.com'] lst[1] = 'fox.com' ow lst has become ['abc.com', 'fox.com', 'msbc.com'] prit(le(lst)) A. 3

LIST METHODS

Source of list methods material Much of this material based o but modified from "CS1 i Pytho Peer Istructio Materials" by Daiel Zigaro, i the repository http://www.peeristructio4cs.org/ licesed uder a Creative Commos Attributio- NoCommercial-ShareAlike 3.0 Uported Licese. (Licese at: https://creativecommos.org/liceses/by-csa/3.0/deed.e_us)

Methods: Gettig ifo You ca lear about methods for stadard built-i types, e.g., strig, list, i q q Pytho documetatio https://docs.pytho.org/3/library/idex.html For may list fuctios ad methods, see Mutable sequece types At IPytho cosole prompt I [1]: dir(list), dir(str), etc. tells ames of all methods For CS 111, igore all methods with ames startig with uderscore; we wo't use

Key list methods ls.apped(item): add item to ed of ls ls.pop(): remove ad retur elemet at ed of ls ls.pop(i): remove ad retur elemet at idex i i ls ls.remove(item): remove first occurrece of item from ls ls.isert(i, item): isert item ito ls at positio i q slidig elemets of ls[i:] all oe positio right to make room ls.sort(): Move elemets of ls so that ls is i sorted order q Requires all elemets to be comparable (all umbers or all strigs) All those methods modify ls Oly pop amog those methods has a retur value

What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop() A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error

What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop() A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error

What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2) # The 2 is what's ew A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error

What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2) # The 2 is what's ew A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error

What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b') A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error

What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b') A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error

a == b? a = ['a', 'b', 'c', 'd'] b = a.pop() a. Yes b. No