>>     < >




In-class notes for 03/12/2014

CS 121B (CS1), Spring 2014

  • Homework assignment

  • quiz returned

    Problem 1, Problem 2
  • Homework 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

    • Spec

      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 of input() 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

    • Spec

    • 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:

      • In empty list case, return empty list [], which satisfies the spec (which calls for a list to be returned).

      • In else: case, + must be list concatenation operator to satisfy the spec, so use [lis[0]] instead of lis[0].

  • Clipping -- checking steps

Upcoming

Summary of strings chapter

Recursion

Submitted questions on readings

To study for quiz

  • Functions with loops (for or while), if, conditional accumulators, state changes, and return values

  • Recursion: recursive functions that return numbers or lists (no loops, top-level elements)




< >