CIT 590 Assignment 5: Array Operations
Spring 2010, David Matuszek
Purposes of this assignment:
- To give you practice with unit testing
- To help you understand APIs (Application Programmer Interfaces)
General idea of the assignment:
Write a number of methods for manipulating arrays. Unit test
all your methods thoroughly.
The methods below are all very short. Most of your
work will be in writing the unit tests for them. That's what this assignment is really about.
Note 1: There is no need for a main function
anywhere in this assignment. You are not writing a program, you are writing
functions that could be used by other programs.
Note 2: You will not be assigned a partner for this program. Please do it
alone.
Specific Methods:
is_2d(array)
- Returns
True if the parameter is a two-dimensional array (a list of lists) and False otherwise.
fill(length)
- Creates an array of
length integers, and fills it with the numbers 1 to length.
Example: fill(12) returns |
|
rotateRight(array)
- Takes a two-dimensional input array and returns a new array which is "rotated"
a quarter-turn clockwise.
Example: rotateRight( |
|
) returns |
|
rotateLeft(array)
- Takes a two-dimensional input array and returns a new array which is "rotated"
a quarter-turn counterclockwise.
Example: rotateLeft( |
|
) returns |
|
transpose(array)
- Takes an input array of
m rows and n columns, and
transposes it to form an array of n rows and m columns.
The value in location [i][j] of the input array is copied into
location [j][i] of the new array.
Example: transpose( |
|
) returns |
|
ravel(array, n)
- Takes a one-dimensional input array of
m × n
numbers and returns a two-dimensional array of m rows and
n columns. The first n numbers of the given array are
copied into the first row of the new array, the second n numbers
into the second row, and so on. This method raises a
RuntimeError if the length of the input array
is not evenly divisible by n.
Note: To raise a runtime error, execute the statement raise RuntimeError(message).
Example: ravel( |
|
, 4) returns |
|
unravel(array)
- Takes a
m by n two dimensional array and returns
a one-dimensional array of size m × n containing the
same numbers. The first n numbers of the new array are copied
from the first row of the given array, the second n numbers
from the second row, and so on.
Example: unravel( |
|
) returns |
|
reshape(array, n)
- Takes a two-dimensional array of
r rows and c columns
and reshapes it to have (r*c)/n by n columns.
This method raises a
RuntimeError if r*c
is not evenly divisible by n.
Example: reshape( |
|
, 6) returns |
|
join(array1, array2)
- Takes two two-dimensional arrays, one with
n rows and
m1 columns, and the other with n rows and
m2 columns, and adjoins them to form a new array with
n rows and m1+m2 columns. This method raises a
RuntimeError if the input arrays do not have the
same number of rows.
Example: join( |
|
, |
|
) returns |
|
randomize(array)
- Given a two-dimensional array, randomly rearranges all the values in the array.
Note: Python already has random.shuffle(array) for one-dimensional sequences.
Example: randomize( |
|
) might return |
|
pick(array)
- Given a two-dimensional array, randomly returns one of the values in the array.
Example: pick( |
|
) might return |
4. |
to_chars(array)
- Given an array of integers in the range 0 to 127, return an array of characters
with those ASCII values. This method raises a
RuntimeError
if any of the integers are outside the range 0 to 127.
Note: You should know how to use the ord and chr functions.
Example: to_chars( |
|
) returns |
|
to_string(array)
- Given an array of integers in the range 0 to 127, return an String of characters
with those ASCII values. This method raises a
RuntimeError
if any of the integers are outside the range 0 to 127.
Example: to_string( |
|
) returns "Hello". |
to_ints(array)
- Given an array of characters, return an array of their ASCII values.
Example: to_ints( |
|
) returns |
|
string_to_ints(string)
- Given a String, return an array of the ASCII values of the characters
in that String.
Example: string_to_ints("Hello") returns |
|
I have not asked you to write functions to (1) sort an array, (2) convert a string to an array of characters, or (3) convert an array of characters to a string. It seems like those would fit right in with the above functions. You should be able to figure out why these functions are missing from the above list.
How to do TDD:
For each method, write a stub (a method that does nothing, and returns a result that is unlikely to be correct, such as None.)
Next, repeat the following steps:
- Write a test for one of the methods.
- Run the tests to make sure that your new test fails.
- Now write the method, replacing the stub code.
- Test whether your new method is correct.
- Debug as necessary. (Remember, it's possible that the test itself,
rather than the method being tested, is wrong.)
Do the above for each of the methods, until you are thoroughly familiar
with the pattern.
Things to remember:
- I gave you a fairly substantial writeup on unit testing.
- Build the problems into your unit test functions, don't read them in from a file. In order to write a test, you need to know the correct answer, and that's easier if the problem and its solution are together in your code.
- Unit tests are intended to find anything that could be wrong
with your methods, not to "prove" that they are correct. As
you write them, you are the prosecuting attorney, trying to expose any
flaws in the defendant's (the method's) story. For example, do the methods
handle zero-length arrays correctly?
- For those functions which I said should raise a
RuntimeError, your unit tests should check that the RuntimeError is raised appropriately.
Grading:
My unit tests will be applied to your methods. They will test everything that I can
think of that your methods might possibly get wrong.
Your unit tests will be applied to my methods. There will be errors in some of the
methods, which I expect your unit tests to find.
In order for this to work, you must use the method signatures exactly as listed.If your
methods can't be called from my unit tests, or vice versa, they will be marked wrong.
Due date:
Thursday, February 18, before midnight. Create two files, named array_ops.py and array_ops_test.py, zip them together, and submit via Blackboard.