In-class notes for 03/12/2014
CS 121B (CS1), Spring 2014
quiz returned
Problem 1, Problem 2Homework problems will be completely determined by 10am on day before that homework is due.
Other items on homework:
Due at 5pm on the due date
Full credit if submit some of it on time, and make up the rest within a few days.
You can send email to graders at
cs1b-graders@stolaf.edu
Will be talking to graders. Comments to convey?
Submitted questions on assignments and technology
String questions -- problem 1a
-
Note: No arguments, no return value -- it's all state change.
Starting the function definition:
def getWords(): printf("Enter lines of text, followed by an empty line") ______
We don't know how many lines there will be, so use a
while
loop.The guard of the
while
loop should be that an empty line was just read, so, read the first line before the loop and read the next line inside of the loop.line = input() # invar: word lists have been printed for all lines read so far while line != "": ______ line = input()
Note: If no argument is supplied for a call ofinput()
then no prompt is printed.Complete function definition
def getWords(): printf("Enter lines of text, followed by an empty line") line = input() # invar: word lists have been printed for all lines read so far while line != "": print(line.split()) # print list of words in line line = input()
-
Recursion problem 11d,
removeStrings
After cases and asserts:
def removeStrings(lis): if lis == []: ______ # assert: at least one element in lis elif type(lis[0]) == str: ______ # assert: first element of lis is not a string else: ______
Final solution
def removeStrings(lis): if lis == []: return [] # assert: at least one element in lis elif type(lis[0]) == str: return removeStrings(lis[1:]) # assert: first element of lis is not a string else: return [lis[0]] + removeStrings(lis[1:])
Checks:
Clipping -- checking steps
Upcoming
Will talk about dictionaries later, not today.
Quiz on Wednesday. See below for topics.
Summary of strings chapter
Recursion
Submitted questions on readings
To study for quiz
Functions with loops (
for
orwhile
),if
, conditional accumulators, state changes, and return valuesRecursion: recursive functions that return numbers or lists (no loops, top-level elements)
< >