[Note: this webpage last modified Tuesday, 04-Dec-2012 10:12:46 EST]
Homework assignments will be posted to this website. Homework assignments are due by 12:30pm one week after handed out (so you have one week to complete them). If you turn in the assignment late, I will grade it so you know how you did, but it will count as a 0.
You should email your homework solution to me at jkinne@cs.indstate.edu before it is due.
For each assignment, please download the template file by right-clicking on the the link, and choosing "Save target as" or "Save link as" (or whatever your browser calls it). You should put your answers into this file, and then turn in this file as an attachment to an email. If you do not turn in your homework in this way, I will take off points!
hw7a.py, hw7b.py, hw7c.py, hw7d.py in in class code, grading
HW 8: work on your project and place your project files in your CS account before class starts on April 12. Your "main" project file should be called project.py, and I should be able to run your project by just running that file. (If you are using something other than Python, obviously you will call it something different).
At the top of project.py, you should have ''' ''' with the name of the file, author(s), citations for anything you may have used online or from class, basic description of what the program does, and instructions on how to run the progrm (e.g., if there are other files that are needed like pictures, or other modules that need to be installed - like pygame).
Your programs should be reasonably commented so it is easy for me to understand how it works. At a minimum, include comments for each function saying what it does, what the parameters are, and what the return value is.
Your program should make use of the following at least somewhere:
For this homework, I'll grade it based on the "Grading Programs" paragraph in the syllabus. 5 points will be for good programming style as described there. 10 points will be for "correctness" of your program, including whether you have included the 4 required things listed above.
Final presentations will be on Tuesday Apr 24 and during the exam time on Tuesday May 1. The list of open slots and who is presenting is in the announcements in blackboard. For your presentation, you should (i) demonstrate your project (by running it), (ii) explain some parts of the code, and (iii) talk about anything that is still left to do. For explaining the code, you should not just read the code. Just explain the main parts, and if there is anything particularly interesting. If you had to learn something we did not do in class, then you probably want to focus on those parts when explaining your code. The grade for the presentation will count as 33% of your project grade, and will be graded on how well you explain your project. You do not need to have the project finished, but it must do something (should be crashing). If you are unsure, then check with me the day before your presentation.
Each person/group will have 6 minutes to present the project. You should plan on 1 minute of time for questions and comments from the audience. You should practice your presentation out loud in a normal pace of voice to make sure you know how long it is taking - I will not let you take more than your allotted time. On the day of the presentation, you should have your project ready to go on your laptop and know how to make your laptop display on a projector, or have the project ready on a USB drive to use on the computer in the front of the room. Time spent getting your computer/code ready on the computer counts as part of your 6 minutes.
The format will be the same as the previous exams. The exam will be cummulative but will focus more on what we have done since the last exam. You should use the previous exams, homeworks, and in-class code as sample questions to study from. Also, consult the list of topics covered each day on the schedule for what you ought to know.
Since the last exam, the new material you should know: reading/writing files, using lists and dictionaries in loops, pickling/unpickling to save variables to strings (that can then be saved/loaded from files). Mostly, you should be a lot more familiar with using lists and dictionaries than in the first exam.
The exam will have the same number and types of questions. Some of the questions will be very simlar to questions from the other exams. Others will focus more on the newer material. Below I just give some examples of questions focusing on the newer material.
Give code: given a dictionary where the keys are strings and the date/values are numbers, find the key whose value is the largest. For example, the keys could be names, and the values could be scores on a test, so you are finding the person who got the highest score.
Give code: You will store a diary in a file. Give two functions. The first function is called readDiary, and it opens up the diary.txt file and prints it to the screen. The second function is called diaryEntry, and it opens up the diary and writes a new diary entry to it (which you need to get from the user with raw_input). Note that if you open a file with 'a' instead of 'r' or 'w', it opens the file for "append" writing.
Give code: Make a while loop that repeatedly asks the user for numbers, and adds them all to a list (until they type 'q'). After the loop, all the numbers should be in the list.
Fix the errors. The following is supposed to multiply all the numbers in a list. There should be 3 errors.
def multList(L): mult = 1 for x in L: mult *= L[x] return mult L = [1, 2, 3, 4] print multList.L
Trace the variables. The following is supposed to make a list of Boolean values so that index i indicates whether i is prime.
def isPrimes(n): L = [True]*(n+1) L[0] = False L[1] = False for i in range(2, n+1): if L[i] == True: for j in range(i*2,n+1,i): L[j] = False return L L = isPrimes(5) print '4 is prime: ' + str(L[4])
Fix logical errors. The following is supposed to take the worldMap and convert it into a list of lists. There are at least 2 logical errors.
worldMap = ''' **************************** ******* *********** ******* ******** *********** *$ * ****** ************** ****** ****** M @****** ****************************''' mapList = worldMap[:1].split('/n') myMap = [] for row in myMap: myMap.append(list(row))
Fix logical errors. Assuming the myMap list of lists was set up correct, the following is supposed to find what row/col the M is at (if there is one). There are at least 2 logical errors.
for r in range(0, myMap): for c in range(0, len(myMap[r])): if myMap[r:c] == 'M': print 'Monster at (r,c) = ' + str((r,c))
True or False: The following code outputs 'hi'.
L = ['one','two','hi'] for i in range(0,len(L)): if L[i] == 'hi': print i
True or False: The following code outputs 'hi'.
L = ['one','hi','two'] for x in L: if x == 'hi': print x
True or False: the following has a syntax/runtime error.
D = {'couch':200, 'chair':50, 'lamp':20} for k in D: print D.k
Format will be the same as the first exam. The exam will be commulative, so everything you should have known from the first test you should still know. This means you can take the first exam and first sample exam as practice questions for this exam too.
New material that was not on the first test includes for loops, turtles, and lists/dictionaries/tuples. The questions on lists/dictionaries/tuples will mostly cover the correct syntax to use, and being able to say what happens when code with lists/dictionaries/tuples is run.
Sample exam1 questions
s = '1 6 3 4 5 2 1 3' i = s.find('6') print i
if (True and False) or (6 != 6 or 'b' < 'a'): print True else print False
x = 10 y = 1 + x * 2 z = y - 12 print z
x = 'This is good.' while len(x) > 0: x = x[0:len(x)-1]
L = [{'a':2, 'ab':3, ('cat',5):'dog'}, 5.0, True, None, [6, 7]] x = L[0][('cat',5)][1] print x
def degreesC(degF): return (degF-32.0) * 5.0/9.0 temp = raw_input('What is the temperature (in F)? ') if degreesC(temp) <= 0: print 'It could snow today' else: print 'Probably won't snow today'
def fun(x); return x+x def fun2(y,z): return y*z def fun3(y,z): return fun2(y,z)+fun(y) w = str(fun3(1,2)) + str(fun3('a',2))
y = 28 i = 1 x = 0 while i <= y: if y % i == 0: x += i
count = 0 x = 'why me?' for y in x: if y < 'm': count += 1 print 'count is ' + str(count)
def max(a, b, c): if a >= b and a >= c: return a elif b >= a and b >= c: return b else: return c def min(d, e, f): if d == max(d, e, f): if e == max(e, e, f): return f else: return e else: if e == max(d, d, e): return d else: return e x = 2 y = 3 z = 4 v = min(x,y,z)
L = [5, -1, 7, 10, -11] if L[0] < L[1]: small1, small2 = L[0], L[1] else: small1, small2 = L[1], L[0] for num in L: if num <= small1: small1, small2 = num, small1 elif num <= small2: small2 = num print 'small2 is ' + str(small2)
from turtle import * def clickFunction(x, y): setposition(x y) def turtleFun(): resetscreen() color(red) onscreenclick(clickFunction) mainloop() turtleFun()
def isPrime(n): i = 2 while i < n: if n mod i = 0: return False return True raw_input('Type a number. ') = num if isPrime(int(num)): print 'That is prime' else: print 'That is not prime'
doubleLetter('hello')would return True, but
doubleLetter('oh no you do not')would return False. If you want, you can also return True if a non-letter character (space, punctuation, etc.) appears twice in a row.
from turtle import * resetscreen() color('green') pensize(3) penup() i = 0 while i < 5: forward(20) right(90) mainloop()
def getQuestion(s, n): i = s.find(str(n)) j = s.find(str(n+1)) s[j:i] testText = '''Assume the text of the test is in this variable. Alright.''' q = getQuestion(testText, 5) print 'Question 5 is: ' + q
Exam0 Format
On paper. No computers, phones, calculators, etc.
You are allowed one piece of 8.5x11" paper with anything you want on it (front and back) - written by your or printed by a computer. But you should pretty much know everything on it anyway, if you are going to finish the test in time. People generally report that the "cheat sheet" did not help them much, so do not rely on it to "save you".
Some questions similar to homeworks, others different. See sample exam questions below.
Exam0 Topics
Everything on the class schedule up through last week. That includes the readings up through chapter 8 (strings) in the text and all the topics/information listed on the class schedule.
Sample Exam0 questions
This is a similar number and type of questions as what you can expect on the real exam. Here are the breakdowns on each type of question, how much they'll be worth, and how much time I hope you can finish each question in.
True/False: 10 questions, 1 pt each, 10 pts total, 1 minute per
question, 10 minutes total.
List the variables: 3 questions, 3 pts each, 9 pts total, 3 minutes
per question, 9 minutes total.
Fix syntax errors: 2 questions, 3 pts each, 6 pts total, 3 minutes
per question, 6 minutes total.
Give code/fix logical errors: 4 questions, 4 pts each, 16 pts total,
5 minutes per question, 20 minutes total.
That adds up to 41 points and 45 minutes. Some will be faster than this and some will be slower.
For True/False questions, circle the correct answer and give a very brief explanation (one sentence or less should be enough) of why.
For "trace the program", give a trace of what happens to the variables, and what functions are executing like we have done on the homeworks.
number = raw_input(Enter a number between 0 and 100)
x = 2 if x==3: print('blah blah')
name = raw_input('What is your name?') if name.lower()=='jeff': print('Hello, Jeff.')
def multiply(x,y): prod = x*y z = multiply(11,25) print('11 + 25 = ' + str(z))
x = 2.0 x = x + 2.0 x = x + 1.0
x = 10 y = 1 while x >= 0: y = y + 1 x = x - .1 print(str(y))
x=0 y = 0 z = 0 w = 0 if x < 0: y = 1 elif x == 0: z = 2 elif x > 0: w = 3 x = x + y + z + w if y == w: x = True else: x = False
msg = 'of' result = '' x=3 while x > 0: result = result + msg x -= 1
def fun(x,y,z): if x < y and x < z: return x elif y < x and y < z: return y else: return 0 blah = fun(3, 2, 1) blah = fun(blah, -1, 3)
def findSmallest(a, b, c, d) if a <= b and a <= c and <= d: return a elif b <= a and b <= c and b <= d: return b elseif c <= a and c <= b and c <= d: return c else: return d findSmallest('3,2,10,7')
import(random) def howMany(wantInARow): inARow = 0 count = 0 while inARow < wantinarow count = count + 1 if random.randint(0,1) = 1: inARow = inARow + 1 else: inARow = 0 return count howMany(3)
'''converts from miles to kilometers (1 mile = 1.609344 km) Arguments: miles - the number of miles to convert from. Returns: the number of kilometers '''
def grade(num): if grade >= 60: return 'D' elif grade >= 70: return 'C' elif grade >= 80: return 'B' elif grade >= 90: return 'A' else: return 'F' g = float(raw_input('What is your grade? ')) print grade(g)
def evenNumbers(n): count = 1 while count <= n: print count count = count + 1 evenNumbers('11')