Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Reversing a 1-D array. Write a Python function that given an array A of numbe

ID: 3531017 • Letter: 1

Question

1. Reversing a 1-D array. Write a Python function that given an array A of numbers returns an array B that has the elements of A in reverse order. YOU CANNOT USE THE BUILT IN PYTHON FUNCTION FOR REVERSING A LIST!!! YOU MUST USE A FOR LOOP. a. What should the output of the function be for A = [3, 2, 1, 5, 9]? b. Write the Python function def ReverseArray(A). the function gets an array A in input and it must return the reversed array B. Include detailed comments. c. Evaluate the Python function for arrays of length 1, 5, and 10. Report each input and output in the project3.txt file. 2. Fast searching in sorted array. Write a Python function which, given an array of numbers A sorted in ascending order and a number a, returns true if a appears in A and false otherwise. The running time of the algorithm should be log n time. a. Write the Python function def FastSearch(A, a). The function gets an array A in input and must return True or False . Include detailed comments. b. Evaluate the function for a case when a is not present in A, when a is present in A, and when there are multiple instances of a in A. 3. Compression/Decompression. Write a Python function which, given an array of numbers A, returns an array B that compresses array A by run length encoding and then decompresses it to return the original array. (Hint: Look at Data slides) EXAMPLE: A = [8, 8, 8, 8, 8, 8, 5, 5, 5, 3, 3, 3, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 7, 7, 7, 7, 7, 4, 6, 6] Compress(A) = [6, 8, 3, 5, 5, 3, 2, 2, 3, 1, 4, 0, 5, 7, 1, 4, 2, 6] Decompress(B) = [8, 8, 8, 8, 8, 8, 5, 5, 5, 3, 3, 3, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 7, 7, 7, 7, 7, 4, 6, 6] a. What should the output of the function be for: A = [2, 2, 2, 2, 2, 2, 1, 1, 3, 4, 4, 4, 4, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1]? b. What is the compression factor for the above input? (i.e. original size/compressed size) c. Write a Python function def Compress(A). The function get an array A in input and must return the compressed array. Include detailed comments. d. Write a Python function def Decompress(B). The function gets a compressed array B in input and returns a decompressed array. Include detailed comments. http://courses.cs.purdue.edu/cs17700:spring13:projects:project3-file

Explanation / Answer

THIS WILL BE HELPFUL FOR YOU his module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. The following type codes are defined: Type code C Type Python Type Minimum size in bytes 'c' char character 1 'b' signed char int 1 'B' unsigned char int 1 'u' Py_UNICODE Unicode character 2 (see note) 'h' signed short int 2 'H' unsigned short int 2 'i' signed int int 2 'I' unsigned int long 2 'l' signed long int 4 'L' unsigned long long 4 'f' float float 4 'd' double float 8 Note The 'u' typecode corresponds to Python