Python Numbers and 1-D Arrays of Numbers

advertisement
CS 17700 Laboratory:
Python Numbers and 1-D Arrays of
Numbers
© Popescu 2012
Lab type: individual
Files to turnin: 3
 This one, with your answers to questions 1a, and 2a.
 Lab8Part1.py
 Lab8Part2.py
1. SETUP
Under your cs177 folder, create the folder lab08. Download there this file and the file
Lab8Part1.py.
1|8
3. GRADING RUBRIC
EXERCISES
Points
Exercise 1
45
1a
1b
5
40
-
the 3 syntax error were
corrected
- the runtime error was corrected
- the logical error was corrected
Exercise 2
15 points
(5 points for each syntax error corrected)
10 points
15 points
55
2a
2b Write Python function IsSorted
-
5
20
The code does not have any
syntax error
The code does not raise any
runtime error
10
10
2c
TOT
2|8
30
Test case output is correct
(10 points for each correct test case)
100
3. Turn-in instructions
To submit your assignment, you must turn-in the files Lab8Part1.py, Lab8Part2.py and this file
(Lab8-assign.docx) you created under the lab08 folder.
Open a terminal window and login in your UNIX account by performing the Steps 1 through 4
described here:
http://courses.cs.purdue.edu/_media/cs17700:howtodo:unix-login-instructions.pdf
Then, in your UNIX terminal window, launch the following commands:
$ cd
$ cd CS177
$ turnin -v -c cs177=COMMON -p lab08 lab08
3|8
Exercise 1
Counting multiples.
Consider the following pseudo-code function that gets in input an array of integers A and an
integer k, and returns the number of multiples of k in the array:
INPUT:
A //Array of Integers
n //Length of A
k //Number to find a multiple of
OUTPUT:
count //Number of times a value in A is divisible by k (AKA value is a
multiple of k)
CountingMultiples(A, n, k)
count = 0 //By default, no values of A are a multiple of k
for i = 0 to n – 1
if A[i] % k == 0 // test if A[i] is divisible by k
count = count + 1
endif
endfor
return count
endCountingMultiples
Question 1a
What is the output of the function for A = [9, 4, 5, 6, 12, 20] and k = 3?
WRITE YOUR ANSWER HERE
4|8
1b
In Lab2part1.py, CountingMultiples pseudo-code has been turned into Python code for you.
However, this conversion was done by a friend of yours in CS177 and she made 3 mistakes –
one Syntactic mistake, one Runtime mistake, and one Logical mistake. Your task is to find each
mistake and correct it; use the output you're expecting from question a above to test the Python
code and make sure it works. Turn in your modified Lab2part1.py when you have finished the
lab.
NOTES
The function CountingMultiples contains three syntax errors, one runtime error, and one
logical error.
Syntax, runtime, and logical errors
Syntactic errors are caused by grammatically incorrect statements. Programming languages
have a very precise syntax that must be adhered to when writing the code. In Python most
Syntactic errors are due to programmer mistyping. Examples of mistyping are:

Typing “;” instead of “:“ (or forgetting the “:” altogether)

Forgetting to close every “(“ with a matching “)”, a “{“ with a matching “}”, or “[“
with a matching ”]”, or other simple problems that are easy to miss when writing
a very long script.
IDLE detects syntactic mistakes as soon as you hit the Check Module button in the Run menu
to test your program. IDLE warns you about a syntactic error with an error message and by
putting a visual marker (a red bar) where it found the error:
Figure 1 – Syntactic error
5|8
Note however that the red bar is where IDLE *thinks* the mistake is – this is not necessarily
always where you made your mistake! (Although, in this particular picture, the script is missing a
“:” after the for loop, so IDLE correctly pointed out where the mistake was.) Syntactic errors
are the easiest mistakes to make, but also usually the easiest to fix.
A Runtime mistake is an error in the program that is not detected until you run it, as the name
would suggest. To detect Runtime mistakes it is important to try your code several times with
several different test cases! A Runtime mistake looks something like this:
Figure 2 – Runtime error
IDLE tries its best to give you information to fix a runtime error – the block of red text is called a
“Stack Trace” and shows the programmer the exact line of code where the runtime error
occurred. It is up to the programmer to reason about *why* - IDLE tries to give hints by
describing what the error was (In this case, IDLE couldn't find the file “n.txt” when I ran my script
and threw an IOError – we will talk more about all of this later in the course), but sometimes
these descriptions are cryptic and confusing. It helps to start at the line numbers suggested by
IDLE, but you may have to look around further than that to find the problem with your code!
6|8
A Logical mistake is when the program runs and doesn't generate any errors, but doesn't give
the result the programmer is expecting! The best way to show a Logical mistake is with an
example:
Figure 3 – A Logical mistake
When this code was run, no error message appeared – but the programmer expected to find the
minimum number in A, which is -10. Instead, she got the maximum number back, which was 3!
IDLE cannot tell you anything about Logical mistakes, which is why it's important to test your
code even if it runs to make sure the result matches expectations! Logical mistakes often occur
because there is something wrong with your loops or conditional statements (in this case, my
conditional should read “if A[i] < Min:”, instead of “if A[i] > Min:”). Logical mistakes
can be the most difficult ones to find in a large program.
7|8
Exercise 2
Deciding whether an array is sorted or not. Write a Python function IsSorted that gets an
array A of integers in input and returns true if the array A is sorted in ascending order and
false otherwise.
a. What is the output of the algorithm for A = [1, 10, 10, 11, 20]?
WRITE YOUR ANSWER HERE
b. Provide the Python code for the Python function described above (with comments).
NOTE: write your code in a new file Lab8Part2.py.
c. Run the code of the function using the following three test cases:
8|8
1.
The input array A is sorted and does not have duplicate elements
2.
The input array A is sorted and has duplicate elements,
3.
The input array A that is not sorted.
Download