Assigned: Monday, March 4, 2013
Due: Friday, March 22, 2013, at 11:00pm.
Project type: individual
This project contains 3 problems.
Note: The Python code will be graded automatically by running code to call your functions. So, please do follow the instructions or you may get 0 grade for these problems:
1.
All the functions you write for this project should be in a single python file, whose name should be project3.py. All the letters should be in lower case.
2.
At the head of the project3.py file, do put Purdue mail account and your name (as
Python comments).
EXAMPLE:
# lmartino
# Martino Lorenzo
3.
The names of the functions are specified in each of the problems. Do NOT change them.
4.
You also MUST create (and turn-in) a file project3.txt containing the answers to questions 1a, 1c, 3a and 3c. For questions 1c and 3c, every time you are asked to evaluate a function you write, provide the input and output in the text file project3.txt.
Download the file main.py in your project3 folder.
This file contains a test case for each function.
It also contains a main function that you can paste at the bottom of your code to test the
Python functions you wrote.
1 | 6
Problems
Problem 1 -
Question 1a answer is correct
1b – Python code of ReverseArray is correct (no syntax error, no runtime error, no logical error)
1c – evaluation of the function for the different inputs is correct
2a – Python code of def FastSearch is correct (no syntax error, no runtime error, no logical error)
2b – the function performs correctly in the three cases (a is not present in A, a is present in A, and when there are multiple instances of a in A)
Question 3a answer is correct
Question 3b answer is correct
3c – Python code of function Compress(A) is correct (no syntax error, no runtime error, no logical error)
3d - Python code of function Decompress (B) is correct (no syntax error, no runtime error, no logical error)
Python code of all the functions is properly commented
TOT
Points
50
5
5
20
20
140
40
5
25
10
45
30
15
2 | 6
You will turn-in your project using the turn-in command you used in the labs.
Hence, on your UNIX account on the lore machine, create a directory project3 using the following commands:
$ cd
$ cd CS177
$ mkdir project3
To submit your project2 from the LWSN lab, refer to the instructions you got in Lab2.
For project3, enter the following commands on your lore account:
$ cd
$ cd CS177
$ turnin –v -c cs177=COMMON -p project3 project3
You can also work at home and then transfer remotely your project3.py and the file project3.txt) to your UNIX account, and then turn-in it using the instructions above. Please read the document http://courses.cs.purdue.edu/_media/cs17700:spring13:remoteturnin-s13.pdf
.
Comments are required! Look at grading rubric at the top.
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.
3 | 6
4 | 6
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.
5 | 6
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] B
Decompress(B) = [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] 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.
6 | 6