Use these problems to get an idea about the format for the actual exam and the level of difficulty of the exam questions. The actual exam may or may not have problems that look like these problems, but the level of difficulty of the problems will be similar.
For these problems, you may need the graphics.py Python library. Download a copy and store this in the same folder with your Python programs.
1. Write a Python program named picture.py that creates a 200 X 200 window and draws the following picture using the graphics.py library:
The colors of the circles are blue (top left), green (top right), red (bottom left) and yellow (bottom right). The two diagonal lines are magenta with a width of 5 pixels.
2. Write a Python program named clickme.py that creates a 300 X 300 window titled "Click Me", draws an orange square in the center of the window with a side length equal to 100 pixels. Then wait for the user to click the mouse in the window. If the user clicks inside the square, print EXCELLENT in the middle of the window, otherwise print SORRY in the middle of the window. You may use any font face, style, size you wish. Use the default black color for the text message.
HINT: You might need the logical operation and in this solution.
3. Write a Python program named shift.py that creates an array with the following data values inside in the order given:
5,2,8,3,9,4,6,1,7
Shift the numbers in the array one position to the left, moving the first value to the last position to get:
2,8,3,9,4,6,1,7,5
Use the following algorithm:
1. Store the first value in the array into a temporary variable. 2. For i in the range from 0 to the length of the array - 2, do the following: a. Copy the value at position i+1 in the array to position i. 3. Store the value in the temporary variable in the last position of the array. 4. Print the resulting array.
Note that the algorithm above should work for any array, not just the one you use to test your program.
4. Complete the following Python program named sortcheck.py so that it prints out WIN if the given array is sorted in increasing order or LOSE if not. The array can have duplicate values, as long as the overall data increases from the first value to the last value.
def main(): A = [5, 6, 2, 8, 4] if sorted(A) == True: print "WIN" else: print "LOSE" B = [3, 6, 8, 9, 10, 14] if sorted(B) == True: print "WIN" else: print "LOSE" def sorted(array): # returns True if array is sorted in increasing order, False otherwise # you complete this function: main()
Algorithm for sorted function:
For each value in the array (except the last value), compare it to the value that immediately follows it in the array. If a value is greater than the value in the next cell, return False immediately since the array can't be sorted in increasing order in this case. If all pairs of adjacent values in the array pass the test above, then return True instead.