Assignments

[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!

Final Exam

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.

Exam 1

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

  1. True or False. The following code prints the value 2.
    s = '1 6 3 4 5 2 1 3'
    i = s.find('6')
    print i
    
  2. True or False. The following code prints the value True.
    if (True and False) or (6 != 6 or 'b' < 'a'):
      print True
    else
      print False
    
  3. True or False. The following code prints the value 10.
    x = 10
    y = 1 + x * 2
    z = y - 12
    print z
    
  4. True or False. The code inside the loop in the below code runs exactly 13 times.
    x = 'This is good.'
    while len(x) > 0:
      x = x[0:len(x)-1]
    
  5. True or False. Tuples are similar to lists, but they are immutable.
  6. True or False. Using the mainloop at the end of a turtle program is optional. Things still work fine without it.
  7. True or False. The following code will run without an error.
    L = [{'a':2, 'ab':3, ('cat',5):'dog'}, 5.0, True, None, [6, 7]]
    x = L[0][('cat',5)][1]
    print x
    
  8. True or False. The following code has an error. It is supposed to determine if it could snow today.
    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'
    
  9. True or False. The following code outputs the value '4aaaa'.
    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))
    
  10. True or False. The final value of the variable x is 28 after the code is run.
    y = 28
    i = 1
    x = 0
    while i <= y:
      if y % i == 0:
        x += i
    
  11. Give a trace of the program...
    count = 0
    x = 'why me?'
    for y in x:
      if y < 'm':
        count += 1
    print 'count is ' + str(count)
    
  12. Give a trace of the program...
    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)
    
  13. Give a trace of the program...
    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)
    
  14. Fix all the syntax/runtime errors. The code is supposed to move the turtle to the spot where they click. There are 3 syntax/runtime errors.
    from turtle import *
    
    def clickFunction(x, y):
      setposition(x y)
    
    def turtleFun():
      resetscreen()
      color(red)
      onscreenclick(clickFunction)
        mainloop()
    
    turtleFun()
    
  15. Fix all the syntax/runtime errors. The code is supposed to ask the user for a number and then test if the number is prime. There are 3 syntax/runtime errors.
    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'
    
  16. Give code that asks the user for a number (with raw_input) and then prints whether they entered a positive or negative number.
  17. Give code for a function that determines if a string anywhere has the same letter twice in a row. The function should return True if so and False if not. For example,
    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.
  18. Fix the logical errors. The code is supposed to draw a pentagon on the screen. There are two logical errors.
    from turtle import *
    resetscreen()
    color('green')
    pensize(3)
    penup()
    i = 0
    while i < 5:
      forward(20)
      right(90)
    mainloop()
    
  19. Fix the logical errors. Assume the text of this test is in a string called testText. The function is supposed to get and print a particular question from the test. There are two logical errors.
    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
    

Exam 0

Exam0 Format

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.

  1. True or False. The following code has an error.
    number = raw_input(Enter a number between 0 and 100)
  2. True or False. The following code has an error.
    x = 2
    if x==3: 
      print('blah blah')
  3. True or False. The following code has a logical error.
    name = raw_input('What is your name?')
    if name.lower()=='jeff': 
      print('Hello, Jeff.')
  4. True or False. The following code has a logical error.
    def multiply(x,y):
      prod = x*y
    z = multiply(11,25)
    print('11 + 25 = ' + str(z))
  5. True or False. 6 % 2 is equal to 3.
  6. True or False. not ((3<=2)==False) and True is equal to True.
  7. True or False. Functions in python must always return a value and cannot print to the screen.
  8. True or False. 'hello'[:2] is equal to 'he'.
  9. True or False. The final value of the variable x is '5.0' after the following code is executed.
    x = 2.0
    x = x + 2.0
    x = x + 1.0
  10. True or False. The code below prints the value '100'.
    x = 10
    y = 1
    while x >= 0:
      y = y + 1
      x = x - .1
    print(str(y))
  11. For the following code, give a trace of the execution of the program. List any time a variable is created or changed, and any time a function is executed. This should be like we have done on the homeworks.
    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
    
  12. Give a trace of the program ...
    msg = 'of'
    result = ''
    x=3
    while x > 0: 
      result = result + msg
      x -= 1
  13. Give a trace of the program ...
    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)
  14. The following is supposed to find the smallest number out of four different numbers. Fix all the syntax errors. There are 4 errors.
    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')
  15. The following is supposed to count how many coin flips until the given number of heads in a row. Fix all the syntax errors. The random.randint(0,1) returns either 0 or 1 at random; we think of 0 as meaning tails and 1 as meaning heads. There are 3 errors.
    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)
  16. Give code to repeatedly ask the user for their name until they type "Juno".
  17. Give code for a function that would have the following docstring describing it. Give the code for the function definition (including the function definition line) and for how to call the function to determine how many kilometers are in 80 miles.
    '''converts from miles to kilometers (1 mile = 1.609344 km)
    
    Arguments:
    miles - the number of miles to convert from.
    
    Returns: the number of kilometers
    '''
  18. Here is a function that is supposed to do a grading scale. It has logical errors. Fix the code.
    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)
  19. Here is a function that is supposed to print all the even numbers less or equal to a certain number. There are three logical errors. Fix them.
    def evenNumbers(n):
      count = 1
      while count <= n:
        print count
        count = count + 1
    evenNumbers('11')