Introduction to Computer Programming: Lab 5

Lists (and a bit of graphics)



  1. Warmup. Experiment with lists by typing these examples into the Spyder console:

     In []: seasons = ['Winter', 'Spring', 'Summer', 'Fall']   # a list literal
     In []: seasons[3] = 'Autumn'   # lists are mutable
     In []: seasons
     In []: ','.join(seasons)
     In []: cs_fac = []           # building a list manually with append
     In []: cs_fac.append('Jim')
     In []: cs_fac.append('Mike')
     In []: cs_fac
     In []: words = 'the quick brown fox'.split()
     In []: len(words)
     In []: words[1]
     In []: words
     In []: ns = [2, 11, 3, 7, 5]
     In []: max(ns)
     In []: sum(ns)
     In []: import lab5_graphics as l5g   # gives the module an abbreviated name
     In []: l5g.open_window()   # launch graphics window with default size, etc
     In []: l5g.paint((0, 0), 'orange')
     In []: l5g.paint((3, 1), 'purple')
     In []: l5g.paint((2, 5), 'green')
     In []: l5g.unpaint((3, 1))
     In []: l5g.clear()
     In []: l5g.paint((3, 1), 'yellow')
     In []: l5g.close_window()
     In []: l5g.open_window(columns=3, rows=7, scale=60, bgcolor='gray',
                            title='example', show_cell_borders=True)
     In []: colors = ['black', 'pink', 'blue']
     In []: for (i, c) in enumerate(colors):
                l5g.paint((i, i), c)
     In []: l5g.close_window()

    (You can choose from a large collection of colors to use with the underlying graphics system.)

Debugging exercises Each of the first few problems are syntactically valid but due not work as advertised. Your job is to fix them one by one.

  1. Fix make_random_int_list(n, start, stop) so that it correctly returns a list of n random integers each at least as large as start but less than stop.

  2. Fix flip_list(bools) so that it changes every element of bools (a list of Booleans) to be its logical opposite. Example:

     In []: bxs = [True, False, True, False, False]
     In []: flip_list(bxs)
     In []: bxs
     Out[]: [False, True, False, True, True]
  3. Fix fill_list(n, it) so that it returns a list of length n, with each item on the list as it. (Equivalent to [it] * n.) Examples:

     In []: fill_list(3, False)
     Out[]: [False, False, False]
     In []: fill_list(7, 'Hi!')
     Out[]: ['Hi!', 'Hi!', 'Hi!', 'Hi!', 'Hi!', 'Hi!', 'Hi!']
  4. Fix count_last_digit(ns) so that it returns a 10-element array of integers reflecting how many numbers in ns end in 0, 1, … up to 9. Examples:

     In []: count_last_digit([36, 24, 528, 26, 7, 60, 2017, 4, 127, 311])
     Out[]: [1, 1, 0, 0, 2, 0, 2, 3, 1, 0]
     In []: count_last_digit([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])
     Out[]: [0, 1, 3, 0, 3, 0, 2, 0, 2, 0]
  5. Fix prepend(items, it) so that it inserts it at the front of list items. Example:

     In []: a = [3, 5, 7, 11]
     In []: prepend(a, 2)
     In []: a
     Out[]: [2, 3, 5, 7, 11]

    Main exercises

  6. Complete make_random_bool_list(n) so that it returns a list of n random Boolean values.

  7. Complete string_to_bool_list(s) so that it returns a list of Boolean values corresponding to string s where * characters correspond to True and all other characters to False. Examples:

     In []: string_to_bool_list('*!!**')
     Out[]: [True, False, False, True, True]
     In []: string_to_bool_list('!@#$*-')
     Out[]: [False, False, False, False, True, False]
  8. Complete all_true(bools) so that it returns True if all items on list bools are True. Otherwise, returns False. You can assumes bools consists entirely of Boolean values. Examples:

     In []: all_true([])
     Out[]: True
     In []: all_true([True])
     Out[]: True
     In []: all_true([True, True, True])
     Out[]: True
     In []: all_true([True, True, False, True])
     Out[]: False
  9. Complete count_false(bools) so that it returns the number of False values on list bools. Assumes bools consists entirely of Boolean values. Examples:

     In []: count_false([])
     Out[]: 0
     In []: count_false([False])
     Out[]: 1
     In []: count_false([True, False, True, False, True])
     Out[]: 2
  10. Complete display_list(bools) so that it paints TRUE_COLOR in cells corresponding to the indexes in Boolean list bools that have value True. Use an for enumerate loop. Call l5g.paint. To test, run the supplied play function.

  11. Complete match_lists(bools0, bools1) so that it returns a list of Boolean values of the same length as bools0 such that True indicates that bools0 and bools1 have the same Boolean value at each corresponding index. If bools1 is shorter than bools0, remaining values in result should be False. Examples:

     In []: match_lists([True, False, True], [True, False, True])
     [True, True, True]
     In []: match_lists([True, False, True], [])
     [False, False, False]
     In []: match_lists([True, False, True, True], [False, False, True])
     [False, True, True, False]

    Upon completion, you should be able to play the visual guessing game.