Assignment 29 — lists and datasets
Due Monday, November 9
Downloads
- Module tf.py
- Tester test_tf.py
Homework function tf.trio()
- Defines a function
trio()
. The function has one list parameterx
of numeric values. The function does not change its list parameter. The function returns a new three-element list whose values are respectively the number of negative, zero, and positive values inx
.
- Observation: three accumulators are needed to store the three wanted counts. Each element of the list needs to be examined to determine which count should be incremented based on its numeric property. Afterwards, the return is a list of three elements (i.e, the computed counts).
- The built-in testing makes use of the following lists.
x1 = [ 0, -3, 0, -4, -2 ]
x2 = [ -3, 1, -2, 1, -3, -3, -2, -4, -1, -4 ]
x3 = [ 2, -1, 0, 3, 0, 3, -2, -2, -1, -4, 3, -4, 3, -1, 3 ]
x4 = [ ]
- The output of its built-in testing should be
trio( x1 ) = [ 3, 2, 0 ]
trio( x2 ) = [ 8, 0, 2 ]
trio( x3 ) = [ 7, 2, 6 ]
trio( x4 ) = [ 0, 0, 0 ]
Homework function tf.flatten()
- Defines a function flatten(). The function has a single dataset parameter
d
.
- The function returns a simple list whose elements are the elements of first row of dataset
d
, followed by the elements of the second row of the dataset, followed by the elements of the third row of the dataset, and so on.
- The built-in tester makes use of the following definitions.
d1 = [ [ 0 ], [ 1, 2 ], [ 1, 2, 3 ], [ 0 ] ]
d2 = [ [ 1, 0, 1, 2, 2 ], [ 3, 0, 1, 1, 1, 0 ], [ 2 ], [ 0, 0, 1 ] ]
d3 = [ [ 3, 0, 3], [ 3, 0, 3, 0, 1], [ 1, 0, 2 ] ]
d4 = [ ]
- The output of its built-in testing should be
flatten( d1 ): [0, 1, 2, 1, 2, 3, 0]
flatten( d2 ): [1, 0, 1, 2, 2, 3, 0, 1, 1, 1, 0, 2, 0, 0, 1]
flatten( d3 ): [3, 0, 3, 3, 0, 3, 0, 1, 1, 0, 2]
flatten( d4 ): []
© 2020 Jim Cohoon | Resources from previous semesters are available. |