see p3_solution.py

advertisement
Project 3: First Python Programs
Assigned: Friday, March 8, 2013
Due: Friday, March 22, 2013, at 11:00pm.
This project contains 3 problems. Each problem is weighted equally. Each problem has multiple
questions, and each question is weighted equally.
Note: The problems will be graded automatically by running code to call your functions. So, please do
follow the instructions or you may get 0 grade for that problem:
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 user id 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 it.
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.
1
Turnin instructions
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 project2, enter the following commands on your lore account:
$ cd
$ cd CS177
$ turnin –v -c cs177=COMMON -p project3 project3
IMPORTANT – WORKING FROM HOME:
You can also work at home and then transfer remotely your project1 (i.e. THIS file with your
answers) 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.
Please note that this document describes how to turn-in a python file. However you can transfer and
turn-in a MS Word file too.
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!!! MUST USE A FOR LOOP.
a. What should the output of the function be for A = [3, 2, 1, 5, 9]?
B = [9, 5, 1, 2, 3]
b. Write the Python function. Include detailed comments. The name of the function should
be def ReverseArray(A). B should be returned.
see p3_solution.py
c. Evaluate the Python function for arrays of length 1, 5, and 10. Report each input and
output.
2
Input: [10]
Input:[1, 30, 54, 21, 13]
Input:[1, 3, 2, 5, 6, 9, 8, 7, 0, 4]
Output: [10]
Output:[13, 21, 54, 30, 1]
Output:[4, 0, 7, 8, 9, 6, 5, 2, 3, 1]
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 logn time.
a. Write the Python function. Include detailed comments. The name of the function should
be def FastSearch(A, a). True or False should be returned.
see p3_solution.py
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.
Input:[1, 3, 2, 5, 6, 9, 8, 7, 0, 4] a = 1 Output: True
Input:[1, 3, 2, 5, 6, 9, 8, 7, 0, 4] a = 10 Output: False
Input:[1, 3, 2, 5, 1, 9, 8, 1, 0, 4] a = 1
Output: True
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,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]?
[6, 2, 2, 1, 1, 3, 4, 4, 3, 6, 8, 5, 7, 1]
b. What is the compression factor for the above input? (i.e. original size/compressed size)
31/14
c. Write a Python function. Include detailed comments. The name of the function should
be def Compress(A). A compressed array should be returned. Note: You can do part d
without doing part c.
see p3_solution.py
d. Write a Python function. Include detailed comments. The name of the function should
be def Decompress(B). A decompressed array should be returned.
see p3_solution.py
3
Download