Homework 5: CodingBat String and Loop Practice

Consider the following exercises in CodingBat.

Specifications

To be considered complete you must finish at least five CodingBat exercises and complete the excercises below:

  1. Predict: First, complete the exercise without using the Python interpreter. (You are welcome to refer to your notes or textbook, read Python documentation, look at examples from class, etc.; just don’t actually run any code.) Trace the execution of the code, keeping track of the function stack, all variables, and any output produced.

  2. Check: Run the code. Does the actual output agree with what you wrote down in step 1?

  3. Evaluate: If your answer to part 1 was different than the actual output, keep experimenting with it, consult the textbook or Python documentation, ask a friend or TA or professor, etc. until you can explain why the code works the way it does and what your misunderstanding(s) were in part 1. (You do not need to do anything for step 3 if the output agrees exactly with what you wrote in step 1.)

You should consider the code in each exercise separately from the other exercises.

  1. What is printed by the following code?

     def f(n: int) -> str:
         n = 2 * n + 1
         return str(n)
    
     def g(n: int):
         s = f(n) + f(n+2)
         print(s)
         print("n is " + str(n))
    
     def main():
         g(7)
         g(2)
    
     main()
    

    The above code contains one trap for the unwary; what is it?

  2. What is printed by the following code?

     def q(n: int) -> str:
         s = 'TIPNR'
         return s[n % 5]
    
     def m():
         i = 2
         count = 0
         s = ''
         while count < 5:
             s += q(i)
             i += 2
             count += 1
         print(s)
    
     m()