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




In-class notes for 03/17/2014

CS 121B (CS1), Spring 2014

Exercise

  • An atom in a list is an element of that list or one of its sublists that is not a non-empty list. Examples:

        ['ab', 3, [14, True]]     has four atoms (but 3 elements)
    
        [['x', -1.1, [6, 0], 2], 'y', []]  has 7 atoms (3 elements)
    
        [[[], -1.1, [6, 0], 2], 'y', []]   also has 7 atoms (3 elements)
    
        [[]]     has one atom, (and one element) an empty list
    

    Exercise: Write a function that counts the number of atoms in a list, using recursion

    countAtoms

    One argument: Any (possibly nested) Python3 list
    Return: A non-negative integer, the number of atoms in that list arg1
    Example calls:
        countAtoms(['ab', 3, [14, True]]) --> 4
        countAtoms([[14, True], 'ab', 3]) --> 4
        countAtoms([]) --> 0
    

    Hints:

    • Start by writing down the cases

    • Then write a "framework" function definition that includes guards for cases, but no code for what to compute in those cases. Include "assert" comments.

    • Then, fill in the code for computing those cases. Look for opportunities for recursive calls with smaller data, reasoning from the spec.

  • Solution

Submitted questions on assignments and technology

  • Files on the Link computers

Upcoming

Summary of strings chapter

Files

  • open()

  • Traversing line-by-line with a for loop

  • Methods close(), read(), write()

Dictionaries

Submitted questions on readings

To study for quiz

  • Trees: list representations; (non-recursive) functions such as problem 12

  • Strings, including Strings problem 1:

    • Methods: split(), lower(), strip()

    • Traversing a string ("approach 1" or "approach 2"), as in HW12A

    • Reading from input and/or argument string/list of strings




< >