Docsity

CS 1301 Study Questions: Solving Problems and Writing Code, Study notes 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.
from myro import *
def showPic():
fileName = pickAFile()
print fileName[-4:]
if (fileName[-5:] != '.jpeg') and (fileName[-4:] != '.jpg'):
print "Not a JPEG image, try again!"
showPic()
else:
p = loadPicture(fileName)
show(p)
pf3
pf4

Partial preview of the text

Download CS 1301 Study Questions: Solving Problems and Writing Code and more Study notes 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. from myro import * def showPic(): fileName = pickAFile() print fileName[4:] if (fileName[5:] != '.jpeg') and (fileName[4:] != '.jpg'): print "Not a JPEG image, try again!" showPic() else: p = loadPicture(fileName) show(p)

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. def isPalindrome( aString): #Reverse the string the easy way #revString = aString[::1] #You could also reverse the string the hard way... revString = "" for i in range(0,len(aString)): revString = aString[i] + revString return ( revString == aString)

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]. def removeStrings(aList): result = filter(lambda x: type(x) != type(""), aList) return(result)