CIT 590 Assignment 5: Array Operations
Spring 2010, David Matuszek

Purposes of this assignment:

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
123456 789101112
rotateRight(array)
Takes a two-dimensional input array and returns a new array which is "rotated" a quarter-turn clockwise.
Example: rotateRight(
1234
5678
9101112
) returns
951
1062
1173
1284
rotateLeft(array)
Takes a two-dimensional input array and returns a new array which is "rotated" a quarter-turn counterclockwise.
Example: rotateLeft(
1234
5678
9101112
) returns
4812
3711
2610
159
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(
1234
5678
9101112
) returns
159
2610
3711
4812
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(
123456 789101112
, 4) returns
1234
5678
9101112
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(
1234
5678
9101112
) returns
123456 789101112
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(
1234
5678
9101112
, 6) returns
123456
789101112
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(
123
456
,
10203040
50607080
) returns
12310203040
45650607080
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(
123
456
) might return
6 1 3
2 5 4
pick(array)
Given a two-dimensional array, randomly returns one of the values in the array.
Example: pick(
123
456
) 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(
72101108108111
) returns
'H''e''l' 'l''o'
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(
72101108108111
) returns "Hello".
to_ints(array)
Given an array of characters, return an array of their ASCII values.
Example: to_ints(
'H''e''l' 'l''o'
) returns
72101108108111
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
72101108108111

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:

  1. Write a test for one of the methods.
  2. Run the tests to make sure that your new test fails.
  3. Now write the method, replacing the stub code.
  4. Test whether your new method is correct.
  5. 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:

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.