15110 SUMMER SESSION TWO - 2014

Problem Set 2 - due Monday, July 7 at 9:00AM in class

Reading Assignment

Read pages 19-42 of chapter 2 of the book Blown To Bits.

Instructions

Exercises

  1. (1.5 pts) For each of the following Python expressions, show how they are evaluated by evaluating one operation at a time and showing the reduced expression until you have a final answer. For example, if the Python expression is

    3 * 4 + 5

    then your answer would look like this:

    3 * 4 + 5
    12 + 5
    17
    

    1. 25-6 * 5/2 + 4

    2. 4 ** 3 ** 2

    3. 12345 // 100 + 12345 % 100

  2. (2 pts) A simple pendulum consists of a mass suspended by a cord with a specific length. The period of this simple pendulum can be computed using the following Python method (function) which requires the length of the cord in meters and the acceleration due to gravity in meters per second per second :

    import math
    def compute_period(length, gravity_accel):
        # computes period of simple pendulum given the cord length in meters
        # and the acceleration due to gravity in meters/sec/sec (e.g. 9.8)
        return 2 * math.pi * math.sqrt(length / gravity_accel)
    
    1. Suppose we use this function to compute the period of a pendulum with a cord length of 10 meters and an acceleration due to gravity of 9.8 meters/second/second:

      compute_period(10, 9.8)

      Will we get an integer result or a floating point result? Why?

    2. Suppose we want to do the computation in part (a) but we call our function with the length of the cord only:

      compute_period(10)

      Does the method use a default value for the acceleration due to gravity or does Python complain about this function call? Explain.

    3. Suppose we want to do the computation in part (a) but we call our function with the arguments in reverse order:

      compute_period(9.8, 10)

      Does Python report an error? Why or why not?

    4. Suppose we replace the return statement with a print statement as shown below:

      import math
      def compute_period(length, gravity_accel):
          # computes period of simple pendulum given the string length in meters
          # and the acceleration due to gravity in meters/sec/sec (e.g. 9.8)
          print(2 * math.pi * math.sqrt(length / gravity_accel))
      

      What value is stored in the variable period if we execute the following instruction? Why?

      period = compute_period(10, 9.8)

  3. (2 pts) For each of the following invalid Python expressions, run them in python3 and explain the errors that you see.

    1. math.sqrt(-16)

    2. 32 % 0

    3. 15 + "110"

    4. return = 100

  4. (2.5 pts) Consider the following Python method definition that uses a loop:

    def mystery(n):
        value = 1
        for i in range(1, n+1):
            value = value * 2
            print(value)
    

    1. What does this method display if we call it as follows:

      mystery(7)

      Show your work by tracing the loop as we did in class.

    2. What common mathematical function is this Python method computing in general if n > 0?

    3. Suppose we replace the first instruction inside the loop with the following:

      value = value * i

      What does this revised method display if we call it as follows:

      mystery(7)

      Again, show your work by tracing the loop as we did in class.

    4. What common mathematical function is the revised Python method computing if n > 0?

    5. Using python3, see what happens in the original function if we replace the print statement with a return statement instead:

      def mystery(n):
          value = 1
          for i in range(1,n+1):
              value = value * 2
              return value
      

      Store the function in a file, then load it in python3 and call it with different positive integers and observe the results. What do your observations suggest about how the return statement works?

  5. (2 pts) Besides learning how to use computational devices to solve a problem, we should understand what happens once the device solves the problem for us. Based on your reading of Chapter 2 (pages 19-42) of Blown To Bits, answer the following questions about the digital data you generate and how all of this digital data affects your privacy.

    1. In your own words, describe one example of how you might leave "digital footprints" while driving your vehicle, based on what you read in the chapter. (Write 2-3 sentences for the example you cite.)

    2. In your own words, briefly describe one example from the reading where a person's confidential medical records were discovered by linking the anonymized records with another data set.