cse 111 bio: program design i lecture 17: software ... design i lecture 17: software development,...

40
CSE 111 Bio: Program Design I Lecture 17: software development, list methods Robert H. Sloan(CS) & Rachel Poretsky(Bio) University of Illinois, Chicago October 19, 2017

Upload: phamnhan

Post on 28-Mar-2018

221 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

CSE111Bio:ProgramDesignILecture17:software

development,listmethods

RobertH.Sloan(CS)&RachelPoretsky(Bio)UniversityofIllinois,Chicago

October19,2017

Page 2: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

NESTEDLOOPS:REVIEW

Page 3: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Generatetimestableforanyn

1234246836912481216

n=4

1 for i in range(1,n+1):2 for j in range(1,n+1):3 print(i*j,end=' ')4 print()

To start printing column only at some k > 1 (because I know my 1's time tables):

A. Edit one number in the range statement in line 1 (for i)B. Edit one number in the range statement in line 2 (for j)C. Both A and BD. Edit both numbers in the range statement in line 1 (for i)E. Edit both numbers in the range statement in line 2 (for j)

Page 4: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

INCREMENTAL DEVELOPMENT, ETC.

Page 5: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

How to get hired by Google

n From the Google HSI CS Faculty Summitn Have coding ability in one of Python, Java, or

C++ and be able to demonstrate in solving interview question liveq Don't go directly to writing code: First discuss with

interviewer to show you can think about it, and to confirm you understood the question

Page 6: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Incremental Programming

n Extension of our general philosophy:q Design first!q Design top downq Implement bottom up

n Implement bottom up often means building working partial program of, say, at most 20 lines (less if in CS 111!) and getting it implemented and tested first

Page 7: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Read in CSV and plot 3 species

n Maybe first:q Read in CSV and get x's and y's and just print out

those two listsq Next might be read in CSV and just do one x-y

plot of all the points, without splitting out the 3 species

q Finally do the assigned problem

Page 8: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Watch the master (Prof. Sloan, of course)

def plot_increment(fname, x, y):"""Incremental start on Iris lab; grabs and prints x and y"""xlist = []ylist = []with open(fname, 'r') as csvfile:

reader = csv.reader(csvfile)for row in reader:

xlist.append(float(row[x]))ylist.append(float(row[y]))

print(xlist)print(ylist)

plot_increment("bezdekIris.csv", 0, 1)Traceback (most recent call last):

File "<ipython-input-31-c83ba8cc3347>", line 1, in <module> plot_increment("bezdekIris.csv", 0, 1) File "/Users/robertsloan/Google Drive/CS Plus Bio Course 111 Green/CS111Green-FA2017/Programming/slides.plotIncrement.py", line 18, in plot_increment xlist.append(float(row[x]))

IndexError: list index out of range

Page 9: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

@#$%%!!

n Oh, can I not say that in class?

n First thing to check: The error message, the last bit: what's it telling us?

n IndexError: list index out of range

Page 10: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

So, index to list out of range???

def plot_increment(fname, x, y):"""Incremental start on Iris lab; grabs and prints x and y"""xlist = []ylist = []with open(fname, 'r') as csvfile:

reader = csv.reader(csvfile)for row in reader:

xlist.append(float(row[x]))ylist.append(float(row[y]))

print(xlist)print(ylist)

Page 11: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

When in doubt

n Start printing out variables to see what is going on

n (Or use upper right panel of Spyder to do the same)

Page 12: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

So, printingdef plot_increment(fname, x, y):

"""Incremental start on Iris lab; grabs and prints x and y"""xlist = []ylist = []with open(fname, 'r') as csvfile:

reader = csv.reader(csvfile)for row in reader:

print(row)xlist.append(float(row[x]))ylist.append(float(row[y]))

print(xlist)print(ylist)

Page 13: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

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-virginica'] ['5.9', '3.0', '5.1', '1.8', 'Iris-virginica'] []

Page 14: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What happened?

n Probably a last line that had a blank, or maybe that's just how it ends

n Want to stop that loop from processing an empty list

n break or probably better continue

Page 15: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

break & continue

n break: Causes immediate termination of the loop

n Use carefully! It can make code very hard to read

n continue: causes this iteration to be skippedq Need a reason to use it, but it can be helpful in

some situations

Page 16: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Fixing our problem with continue

with open(fname, 'r') as csvfile:reader = csv.reader(csvfile)for row in reader:

if len(row) == 0: # empty? continue

#regular processing here

Page 17: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

x = input ("Enter a number: ")while (x%2 == 1 and x%3 == 0):

x = input ("Enter a number: ")

x = input ("Enter a number: ")while True:

if (x%2 == 1 and x%3 == 0):break

x = input ("Enter a number: ")

Which of these will exit on 9?

A

B

C. Both D. Neither E. I don’t know

Page 18: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

LIST FUNCTIONS AND SOME REVIEW

Page 19: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Next big project: Population Genetics

n We will see how a population evolves by simulation, hugely successful and powerful method for doing modern science

n Classical Mendelian geneticsn We'll start with a small population

represented as hmm, something, in a listq And keep building that list

n Today: We will build our list-fu!

Page 20: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Population genetics simulation

n List of containing initial populationn while not at some end conditin concerning list:

q Take (1? a mating pair? all?) elements from the listq Using random and biology, determine offspringq (Maybe?) remove breeders from list (generation dies)q Add newly bred items to listq Get all the links in that page

n Compute and return (or print out?) appropriate stats on final population–results of "dry lab" experiment

Page 21: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

And besides

n Lists in Python are just awesome

Page 22: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Lists versus Strings

List StringElements can be any type

Elements are characters

Mutable ImmutableHeterogeneous elements

Homogenous elements

Can be nested in other lists

Page 23: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Recall

n Lists are data structures that let us store collections of data in sequence with indices

Page 24: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Remember the smallest

n "" # Empty string, could also be written ''

n [] # empty list

Page 25: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

A = [2,3,5]B = ["Mendel"]C = A + B print(C) will result

in:

A. [2,3,5]B. [2,3,5,"Mendel"]C. ["2","3","5","Mendel"]D. This will cause an errorE. I don’t know

Page 26: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

List Functions

n These are not methods; there are also list methods, and we'll do those in a few minutes (have already seen and used .append())

n len: length of a list (i.e., number of elements)

n + will concatenate lists

n min, max: minimum or maximum of list

n sum: sum of the elements in the list (if all numbers)q E.g., sum([2, 3, 5]) à 10

Page 27: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Important: Lists are mutable

>>> years= [1788, 1800, 1860, 1932]>>> years[0] = years[0] + 4>>> print(years)[1792, 1800, 1860, 1932]

Page 28: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is printed

lst = ['abc.com', 'cnn.com', 'msnbc.com']lst[1] = 'fox.com'print(len(lst))

A. 3B. 4C. 23D. 30E. No output; error in 2nd line of code

Page 29: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is printed

lst = ['abc.com', 'cnn.com', 'msnbc.com']lst[1] = 'fox.com'now lst has become ['abc.com', 'fox.com', 'msnbc.com']print(len(lst))

A. 3

Page 30: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

LIST METHODS

Page 31: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Source of list methods material

n Much of this material based on but modified from "CS1 in Python Peer Instruction Materials" by Daniel Zingaro, in the repository http://www.peerinstruction4cs.org/ licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 UnportedLicense. (License at: https://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US)

Page 32: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Methods: Getting info

n You can learn about methods for standard built-in types, e.g., string, list, inq Python documentation

https://docs.python.org/3/library/index.htmln For many list functions and methods, see Mutable

sequence typesq At IPython console prompt In [1]: dir(list), dir(str),

etc. tells names of all methodsn For CS 111, ignore all methods with names starting with

underscore; we won't use

Page 33: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

Key list methods

n ls.append(item): add item to end of lsn ls.pop(): remove and return element at end of lsn ls.pop(i): remove and return element at index i in lsn ls.remove(item): remove first occurrence of item from lsn ls.insert(i, item): insert item into ls at position i

q sliding elements of ls[i:] all one position right to make roomn ls.sort(): Move elements of ls so that ls is in sorted order

q Requires all elements to be comparable (all numbers or all strings)

n All those methods modify lsn Only pop among those methods has a return value

Page 34: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is value of a after code runs?

A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code

generates an error

a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop()

Page 35: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is value of a after code runs?

A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code

generates an error

a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop()

Page 36: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is value of a after code runs?

A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code

generates an error

a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2)

# The 2 is what's new

Page 37: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is value of a after code runs?

A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code

generates an error

a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2)

# The 2 is what's new

Page 38: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is value of a after code runs?

A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code

generates an error

a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b')

Page 39: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

What is value of a after code runs?

A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code

generates an error

a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b')

Page 40: CSE 111 Bio: Program Design I Lecture 17: software ... Design I Lecture 17: software development, list methods Robert H. Sloan ... Population Genetics ... n Classical Mendelian genetics

a == b?

a. Yesb. No

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