Docsity

CS 1301 Study Questions: Solving Problems and Writing Code - Prof. Jay Summet, Assignments of Computer Science

Toggle sidebar
bg1
CS 1301 Study Questions
These are sample test problems. (Most actual test problems will be just a bit easier). To
learn the most, solve these problems using only a pencil or pen and a piece of blank
paper. Once you think you have them solved, move to a computer and see if you were
right. If not, learn from your mistakes!
Write Code ( 8 points)
Write a function showPic() which prompts the user to pick a picture using the
pickAFile() file chooser, and then displays it. If the user picks a file that does not end in
“.jpg” or “.jpeg” the showPic() function should keep prompting the user to pick a file
until they pick a valid jpeg image. Remember, in myro the pickAFile() function returns a
filename as a string, the loadPicture(filename) function loads a picture, and the show(p)
function displays a picture.
Write Code (15 points)
Write a function isPalindrome( aString) that takes a string as a parameter and returns a
boolean value. The function should return True if the string parameter is a palindrome
(i.e. when read backwards, it is the same, like "racecar", or
“AManAPlanACAnalPAnaMA”) and False otherwise.
Write Code ( 10 points)
Write your own version of the getPixels( picture ) function. It will return a list
containing all of the pixels in a picture. You may NOT use the built in getPixels function,
but you can use the getWidth(picture), getHeight(picture), and getPixel(picture, x,y)
functions.
pf2

Partial preview of the text

Download CS 1301 Study Questions: Solving Problems and Writing Code - Prof. Jay Summet and more Assignments Computer Science in PDF only on Docsity!

CS 1301 Study Questions

These are sample test problems. (Most actual test problems will be just a bit easier). To learn the most, solve these problems using only a pencil or pen and a piece of blank paper. Once you think you have them solved, move to a computer and see if you were right. If not, learn from your mistakes! Write Code ( 8 points) Write a function showPic() which prompts the user to pick a picture using the pickAFile() file chooser, and then displays it. If the user picks a file that does not end in “.jpg” or “.jpeg” the showPic() function should keep prompting the user to pick a file until they pick a valid jpeg image. Remember, in myro the pickAFile() function returns a filename as a string, the loadPicture(filename) function loads a picture, and the show(p) function displays a picture. Write Code (15 points) Write a function isPalindrome( aString) that takes a string as a parameter and returns a boolean value. The function should return True if the string parameter is a palindrome (i.e. when read backwards, it is the same, like "racecar", or “AManAPlanACAnalPAnaMA”) and False otherwise. Write Code ( 10 points) Write your own version of the getPixels( picture ) function. It will return a list containing all of the pixels in a picture. You may NOT use the built in getPixels function, but you can use the getWidth(picture), getHeight(picture), and getPixel(picture, x,y) functions.

Namespaces (8 points): Review the following code: aVariable = 8 myVariable = 15 def function1(i,q): global aVariable myVariable = 20 print i + aVariable aVariable = i+q return q + myVariable Answer = function1(aVariable, myVariable) 5a. After the function call (at the end of the code) what is the value of: Answer = _________ aVariable = ___________ 5b. What is printed to the screen when this code runs? _______________ 5c. Which variable or variables are "overshadowed" when execution is inside of function1? ___________________________________ Write Code: Write a function removeStrings( aList) that will take the aList parameter and return a copy with all elements that are of type 'str' removed using the filter(function,list) function. You may write a helper function, or use a lambda expression. For example, removeStrings( ['test', 3, 4, 'hi']) would return [3,4].