CSc 2010, Spring 2010
Practice Problems for Exam 1
----------------------------

1. Assume that the following two functions are defined in myro package:

  def degreeTurn(direction,amount):
  # Given direction = "left" or "right" and amount between 0 and 360
  # degrees, the functions makes Scribbler turn in the direction and
  # angle specified.

  def travel(direction,distance):
  # Given direction = "forward" or "backward" and distance in inches
  # the functions makes Scribbler move in the direction specified
  # for the distance specified.

  Using these two functions write Python code that makes Scribbler
  travel in a hexagon pattern where each side is 6 inches in length.
  Note: The interior angle of a regular polygon is given by the
  formula: (180*(n-2))/n, where n is the number of sides. You may assume
  that the Scribbler starts at any of the vertices.

Assume robot facing north; starting at lower left vertex.

degreeTurn("left",30)
travel("forward",6)
degreeTurn("right",60)
travel("forward",6)
degreeTurn("right",60)
travel("forward",6)
degreeTurn("right",60)
travel("forward",6)
degreeTurn("right",60)
travel("forward",6)
degreeTurn("right",60)
travel("forward",6)

alternate solution

degreeTurn("left",30)
travel("forward",6)
for i in range(5):
  degreeTurn("right",60)
  travel("forward",6)


2. What are the output of the following Python programs when main()
   function is called:

  def main():
    x = 20
    for i in range(10,30,5):
      x = x + i
    print x

90

  def main():
    x = 1
    for i in range(6):
      x = x * i
    print x

0

  def main():
    x = 1
    y = 1
    for i in range(5):
      y = x
      x = x + y
    print x 

32

  def main():
    x = 0
    for i in range(1,20,2):
      x = x + (i%2)
    print x

10

3. Write the code for the following function:

   def sumOfEvenNumbers(N):
   # This function takes as input a positive integer N and returns the
   # sum of all even numbers between 1 and N (including N)
   sum = 0
   for i in range(2,N+1,2):
     sum = sum + i
   return sum

4. What are the possible values for the following expression:

   3*randint(2,5) - 2*randint(0,1)

Possible values for randint(2,5): 2, 3, 4, 5
Possible values for randint(0,1): 0, 1

Possible values for 3*randint(2,5): 6, 9, 12, 15
Possible values for 2*randint(0,1): 0, 2

ANSWER:
Possible values for 3*randint(2,5) - 2*randint(0,1): 
  6, 4, 9, 7, 12, 10, 15, 13

5. What are the output of the following Python programs when main()
   function is called:

  def main():
    s = [25,36,18,90,44,54,60]
    x = 0
    for i in s:
      if i%3==0:
        x = x + 1
    print x

5

  def main():
    s = ["Atlanta","Chicago","Toronto","Bangalore","Beijing"]
    s.reverse()
    for i in s:
      print len(i)
      
7
9
7
7
7

Note: in the original problem statement we had

  for i in s.reverse():

This is an error - because the reverse() function applied to a list
has to be applied first befor ethe for-loop and then the new value of
s is used in the for-loop.

Apparently, in Python the list functions such as reverse() change the 
list on which they are applied and cannot be used in the for-loop
directly!

6. Evaluate the following Boolean expressions:

  (60 > 70) or (40 < 20)
FALSE

  ("atlanta">"boston") and (20 < 30)
FALSE

  not ((6 != 4) or (8 != 8))
FALSE


7. Consider the following function:

  def mysteryFunction(L):
    count = 0
    for x in L:
      if x in ['a','e','i','o','u']:
        count = count + 1 
    return count

  What are the values of the following function calls?

  	mysteryFunction("This is a dog")
        4

  	mysteryFunction("Good Morning")
        4

  What does the function do in general?
    returns the number of (lower case) vowels in the input string L.