The numpy Module ISTA 350 Hw5

Introduction. This homework is intended to acquaint you with the numpy module and its version of the array data structure. As you will learn, arrays are like lists, but less abstract. Instructions. This is a two-part assignment. For the first part, create a module named hw5.py. Below is the spec for ten methods. Implement them and upload your module to the D2L dropbox. Second, copy your hw4 solution and rename it hw5_bin.py. Replace your list instance variable with a numpy array. Alter your methods as necessary to get them all working. You may only have the one instance variable, but inside methods, you can make a corresponding list variable to reduce the amount of reimplementation that you have to do if you would like. Some methods do need to be altered. The pad method is no longer required and will not be tested for. When you complete this correctly, you can run bincalc with the new module by changing only the import statement. If your hw5_bin module is correct, it will run exactly like it did with hw4. This is an example of the API concept: the bincalc application doesn't care about the implementation details behind the binary interface. Note that you will be testing your modules using the ipython command instead of the python command. Testing. Download hw5_test.py and put it in the same folder as your hw5.py and hw5_bin.py modules. Run it from the command line (with the ipython command) to see your current correctness score. Each of the 10 functions in hw5.py is worth 5% of your correctness score. Each of the 9 methods in hw5_bin.py is worth 5.56% of your correctness score. You can examine the test module in a text editor to understand better what your code should do. The test module is part of the spec. The test file we will use to grade your program will be different and may uncover failings in your work not evident upon testing with the provided file. Add any necessary tests to make sure your code works in all cases.
Documentation. Your modules must contain a header docstring containing your name, your section leader’s name, the date, ISTA 350 Hw5, and a brief summary of the module. Each method/function must contain a docstring. Each docstring should include a description of the function’s purpose, the name, type, and purpose of each parameter, and the type and meaning of the function’s return value. Grading. Your module will be graded on correctness, documentation, and coding style. Code should be clear and concise. You will only lose style points if your code is a real mess. Include inline comments to explain tricky lines and summarize sections of code (not necessary on this assignment). Collaboration. Collaboration is allowed. You are responsible for your learning. Depending too much on others will hurt you on the tests. “Helping” others too much harms them in reality. Cite any sources/collaborators in your header docstring. Leaving this out is dishonest. Resources. http://docs.scipy.org/doc/numpy/index.html http://docs.scipy.org/doc/numpy/reference/ http://docs.scipy.org/doc/numpy/reference/arrays.html http://wiki.scipy.org/Numpy_Example_List hw5.py: reverse: this function takes an array as an argument and returns a new array that has the same elements in reverse order. odd_even_mask: this function takes an array as an argument and returns a new array of the same length that has zeros corresponding to even numbers in the argument and ones corresponding to odd arguments. cycle: this function takes an array as its first argument and an integer n as its second. It returns a new array that has the same elements n places to the right. When an element shifts of the right end of the array, it wraps around to position 0. Examples: cycle([1, 2, 3, 4], 0) → [1, 2, 3, 4] cycle([1, 2, 3, 4], 1) → [4, 1, 2, 3] cycle([1, 2, 3, 4], 2) → [3, 4, 1, 2] cycle([1, 2, 3, 4], 6) → [3, 4, 1, 2] double: this function takes an array as an argument and returns a new array that has each element doubled. double_ip: this function takes an array as an argument doubles each element in place. square_evens: this function takes an array as an argument replaces each even element with its square. binary_search: the first argument this function takes is a key and the second is an array. If the key is in the array, it returns the index of the key. If the key is in the array more than once, it doesn't matter which index is returned, as long as it is for a position containing the key. If the key is not in the array, return -1. Here is some pseudocode for this function: initialize variables containing your low and high indices while low is <= high: check the position midway between them if it's the key, return the index if the item in the array is larger than the key: change high appropriately else: change low appropriately return -1 insert(arr, index, value, overwrite): Assume that index is a nonnegative integer. If it is too large, raise an IndexError. If overwrite is False, shift all values from index and higher one spot to the right, eliminating the last item. Set the element at position index to value. swap: this function takes three variables, an array and two integers. Swap the values at the indices given by the two integers. Don't do any error checking. add_arrays: this function takes two array arguments. Return a new array that is the length of the longer of the two arguments. The element at each position of the new array is the sum of the elements in the corresponding positions in the two arguments. If one array is longer, fill the remaining positions in the new array with the values in the corresponding positions in the longer array. hw5_bin.py: init: init has one string parameter with a default argument of '0'. This string can be the empty string. Otherwise, it should consist only of 0’s and 1’s and should be 16 or less characters long. If the argument does not meet these requirements, raise a RuntimeError. Each Binary object has one instance variable, a numpy array called num_array. num_array has integers 0 or 1 in the same order as the corresponding characters in the argument. If the string is less than 16 characters long, num_array should be filled appropriately so that it represents the same signed binary number as the argument. repr: Return a 16-character string representing the fixed-width binary number, such as: '00000000000000000'. add: Takes a Binary object as an argument. Return a new Binary instance that represents the sum of self and the argument. If the sum requires more than 16 digits, raise a RuntimeError. neg: Return a new Binary instance that equals -self. sub: Takes a Binary object as an argument. Return a new Binary instance that represents self – the argument. int: Return the decimal value of the Binary object. This method should never raise a RuntimeError due to overflow. eq: Takes a Binary object as an argument. Return True if self == the argument, False otherwise. lt: Takes a Binary object as an argument. Return True if self < the argument, False otherwise. This method should never raise a RuntimeError due to overflow. abs: Return a new instance that is the absolute value of self.
Powered by