Practice Problems 4 Complete before class on 2021-10-08

Complete the following problems on paper. Try to solve each problem on paper first before using Thonny to confirm your answers.

  1. [PP Problem 5.1] What value does each expression produce?

    a. True and not False
    b. True and not false (Notice the capitalization.)
    c. True or True and False
    d. not True or not False
    e. True and not 0
    f. 52 < 52.3
    g. 1+52<52.3
    h. 4!=4.0

  2. [PP Problem 5.6] Write a function named different that has two parameters, a and b. The function should return True if a and b refer to different values and should return False otherwise.

  3. [PP Problem 5.9] Assume we want to print a strong warning message if a pH value is below 3.0 and otherwise simply report on the acidity. We try this if statement:

     >>> ph = 2
     >>> if ph < 7.0:
     ...     print(ph, "is acidic.")
     ... elif ph < 3.0:
     ...     print(ph, "is VERY acidic! Be careful.")
     2 is acidic.

    This prints the wrong message when a pH of 2 is entered. What is the problem, and how can you fix it?

    Note that the print function also allows multiple parameters, which are simply printed on the same line with a space in between. That’s why it is not necessary to use str(ph) here.

  4. Your colleague wrote a program to describe the temperature, specifically it reports if the temperature is below freezing (in degrees fahrenheit), at freezing or above freezing. Their code is not working when the temperature is exactly 32 degrees. What is wrong and how could you fix it?

    if temp == 32:
       print("At freezing")
    if temp <= 32:
       print("Below freezing")
    if temp >= 32:
       print("Above freezing")
  5. Modify the firstprimes function (included below) to print out all the prime numbers less than or equal to the parameter, num. You can assume the isprime function is already available in your program. It will return True if its argument is a prime number and False otherwise.

    def firstprimes(num):
        """Prints out the first num primes"""
        count = 0  # the number of primes we've printed out
        current = 1  # the current number we're checking
        while count < num:
            if isprime(current):
                count += 1
            current += 1
  6. Write two functions, one using a for loop and one using a while loop that print all the numbers from 1 to 20, inclusive, that are evenly divisible by 3. Your function should print one number per line.

  7. Extend both of your functions from the previous problem to accept the inclusive start and stop as parameters, i.e. instead of the fixed range of 1 to 20, allow the caller to specify the range.

  8. [Computing for Biologists] DNA is composed of four nucleotides, A, C, G, and T. Although we might expect the four nucleotides to be equally common in the genome that is not the case. On average, the GC fraction, the percentage of nucleotides that are ‘G’ or ‘C’, of the human genome is less than 40%. The GC fraction is also not evenly distributed. Some regions of the genome, including where genes are found, have a higher GC fraction. Thus regions of high GC content can be used as a signal to detect genes or other features of interest. Write a function named gc_fraction that takes a DNA string as a parameter and returns the GC fraction as a floating point number (that is the fraction of all the nucleotides in the string that are ‘G’ or ‘C’). Your function should work for upper or lower case strings, i.e. ‘acgt’ and ‘AGTC’, should both generate a result of 0.5.