30
CS21: INTRODUCTION TO COMPUTER SCIENCE Prof. Mathieson Fall 2017 Swarthmore College

CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

CS21: INTRODUCTION TO COMPUTER SCIENCEProf. MathiesonFall 2017Swarthmore College

Page 2: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Outline Oct 25:

• Recap reading files• String and List methods • TDD: Top Down Design

• word_guesser.py

Notes

•Lab 6 due Saturday night•Office Hours Friday 3-5pm

Page 3: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Recap reading files

Page 4: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Template for reading a file1) Use a for loop to read the sequence of lines (recommended)

Page 5: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Template for reading a file1) Use a for loop to read the sequence of lines (recommended)

2) Loop over the line indices (using readline() to get the next line)

Page 6: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

cs21

_stu

dent

s_file

.py

Page 7: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

cs21_students_file.py

Page 8: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

List and String Methods

Page 9: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common List methods

Page 10: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common List methods• Add a single element to a list: lst.append(item)

Page 11: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common List methods• Add a single element to a list: lst.append(item)

• Add a list to the end of a list: lst.extend(another_lst)

Page 12: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common List methods• Add a single element to a list: lst.append(item)

• Add a list to the end of a list: lst.extend(another_lst)

• Return the index of an element: idx = lst.index(item)

Page 13: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common List methods• Add a single element to a list: lst.append(item)

• Add a list to the end of a list: lst.extend(another_lst)

• Return the index of an element: idx = lst.index(item)

• Return the count of an element: c = lst.count(item)

Page 14: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common List methods• Add a single element to a list: lst.append(item)

• Add a list to the end of a list: lst.extend(another_lst)

• Return the index of an element: idx = lst.index(item)

• Return the count of an element: c = lst.count(item)

• List concatenation (not a method): lst + another_lst

Page 15: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common String Methods:they all return something!

• string.index(smaller_string)• string.count(smaller_string)• string.isalpha()• string.lower()• string.upper()• string.split(smaller_string)• string.strip()

Page 16: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Common String Methods:they all return something!

• string.index(smaller_string) int• string.count(smaller_string) int• string.isalpha() bool• string.lower() string• string.upper() string• string.split(smaller_string) list• string.strip() string

Page 17: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Converting between lists and strings• Convert a list to a string: <string separator>.join(lst)

• Convert a string to a list: list(<string>)

Page 18: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

TDDTop Down Design

Page 19: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Structure of main and “helper” functions

Main (driver)

Page 20: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Structure of main and “helper” functions

Main (driver)

Helper Function

1 Helper Function

2

HelperFunction

3

Page 21: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Structure of main and “helper” functions

Main (driver)

Helper Function

1 Helper Function

2

HelperFunction

3

Sub-helper

A

Sub-helper

B

Sub-helper

C

Page 22: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Structure of main and “helper” functions

Page 23: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Structure of main and “helper” functions

Page 24: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Structure of main and “helper” functions

Page 25: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Steps of TDD

Page 26: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Steps of TDD1) Design a high-level main function that captures the

basic idea of the program. Often this involves some initial variables, an outer loop, and some ending/output.

Page 27: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Steps of TDD1) Design a high-level main function that captures the

basic idea of the program. Often this involves some initial variables, an outer loop, and some ending/output.

2) As you're writing/designing main, think about which details can be abstracted into small tasks. Make names for these functions and write their signatures below main.

Page 28: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Steps of TDD1) Design a high-level main function that captures the

basic idea of the program. Often this involves some initial variables, an outer loop, and some ending/output.

2) As you're writing/designing main, think about which details can be abstracted into small tasks. Make names for these functions and write their signatures below main.

3) "Stub" out the functions. This means that they should work and return the correct type so that your code runs, but they don’t do the correct task yet. For example, if a function should return a list, you can return []. Or if it returns a boolean, you can return False.

Page 29: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Steps of TDD1) Design a high-level main function that captures the

basic idea of the program. Often this involves some initial variables, an outer loop, and some ending/output.

2) As you're writing/designing main, think about which details can be abstracted into small tasks. Make names for these functions and write their signatures below main.

3) "Stub" out the functions. This means that they should work and return the correct type so that your code runs, but they don’t do the correct task yet. For example, if a function should return a list, you can return []. Or if it returns a boolean, you can return False.

4) Iterate on your design until you have a working main and stubbed out functions. Then start implementing the functions, starting from the “bottom up”.

Page 30: CS21: INTRODUCTION TO COMPUTER SCIENCEcs.haverford.edu/faculty/smathieson/swarthmore/f17/lecs/lec20/lec20.pdfList and String Methods. Common List methods. ... these functions and write

Reasons to use TDD

• Creates code that is easier to implement, debug, modify, and extend

• Avoids going off in the wrong direction (i.e. implementing functions that are not useful or don’t serve the program)

• Creates code that is easier for you or someone else to read and understand later on