In-class notes for 02/28/2014 (CS 121B (CS1), Spring 2014)
>>     < >




In-class notes for 02/28/2014

CS 121B (CS1), Spring 2014

  • Homework assignment

  • Syllabus document.

  • Gone Wed afternoon-Sun next week. Quiz will be on Friday 3/7 instead of Wendesday 3/5.

    Guest speaker on Friday: Prof. Richey, machine learning

  • Reading assignments are part of some homework assignments, and submitting a reading question is the way you submit your work. Please read it thoughtfully enough to come up with one genuine question.

    Daily questions (optional) are for other questions you may have on assignments and technology that you would like to see discussed in class.

    Piazza is for questions that may arise between classes. Use this instead of email or something to get potentially quicker response, or see what other folks are saying.

  • Changed

        import cImage
    
    to
        import cImage as image
    
    in notes and homework

Submitted questions on assignments and technology

Upcoming

Submitted questions on readings

About lists

  • Concatenation operator for lists

    +   (operator)

    2 arguments: Any two lists
    Return: A single list that consists of all elements in the list arg1 followed by the characters in arg2
    Example:
        "hello" + "world" --> "helloworld"
    
    Can be used to "reassemble lists". Example:
    lis = [5, 5, 0, 5, 7]
    [ lis[0]*2 ]             --> [10]
    [ lis[0]*2 ] + lis[1:]   --> [10, 5, 0, 5, 7]
    
  • Tracing a list concatenation:

Recursion

  • Recursive code for drawing sprig, from last time

  • A function is recursive if it calls itself within its own definition. The effect is to accomplish the same computational steps (presumably with modified arguments).

  • Exercises: Answer as many as you can in the time allotted, working together, in order.


    1. Exercise: Write a function that counts the number of elements in a list, using recursion

      countElements

      One argument: A Python3 list
      Return: A non-negative integer, the number of elements in arg1
      Example call:
          countElements([1, 'a', 2]) --> 3
          countElements([]) --> 0
      

      Hints:

      • Use an if statement to handle the empty-list case separately from the case when the argument is a non-empty list. Here's the start of an if statement that checks whether a list lis is empty:

                if lis == []:
        	    ....
        
        After this you can use else to isolate the case of a non-empty list.

      • Only one recursive call is needed; it will be in the non-empty list case. Think of options for what you might compute with that recursive call. (The spec describes what it would compute.)

      • No accumulator is needed!

      • Use a slice.


    2. Exercise: Write a function that produces a list of doubles of elements in a list of numbers, using recursion

      double

      One argument: A list of numbers
      Return: A list of numbers, whose elements are twice the corresponding elements of the list arg1
      Example call:
          double([4, 7, 1]) --> [8, 14, 2]
          double([]) --> []
      

      Hints:

      • Use the concatenation operator + assemble the answer.

      • As before: use if to handle the empty-list case separately; no accumulator; use a slice.


    3. Exercise: Write a function whose return value replaces particular value in a list with another value, using recursion

      replace

      3 arguments: Any two Python3 values and any Python3 list
      Return: A list, whose elements are the same as the elements of the list arg3, except with each occurrence of arg1 replaced arg2.
      Example calls:
          replace ('a', 77, [4, 'a', 1]) --> [4, 77, 1]
          replace (5, 8, [5, 5, 0, 5, 7]) --> [8, 8, 0, 8, 7]
          replace (5, 8, []) --> []
      

      Hints:

      • This problem needs three cases:
        • empty list
        • first element matches arg1
        • first element doesn't match arg1
        Use if/elif/else (or nested if/else to program for each case separately
      • As before, use concatenation operator, slice, etc.

      • Be sure to include the first two arguments in your recursive calls


Submitted questions on readings




< >