Solutions

Exploring the Math Module

  1. Using help(math) we see that we’ve got pow(x,y) in addition to sqrt(x), so we could use pow(x, 0.5) to find a square root.
  2. The sqrt(x) function is arguably more readable than pow(x, 0.5) when implementing equations. Readability is a cornerstone of good programming, so it makes sense to provide a special function for this specific common case.

Also the math libraries are likely to be written in an optimsed way. Using the specific function sqrt is therefore likely to be more efficient than pow.

Locating the Right Module

The random module seems like it could help you.

The string has 11 characters, each having a positional index from 0 to 10. You could use random.randrange function (or the alias random.randint if you find that easier to remember) to get a random integer between 0 and 10, and then pick out the character at that position:

In [6]:
from random import randrange
random_index = randrange(len(bases))
print(bases[random_index])
C

This can be compacted into just

In [7]:
from random import randrange
print(bases[randrange(len(bases))])
G

Perhaps you found the random.sample function? It allows for slightly less typing:

In [8]:
from random import sample
print(sample(bases, 1)[0])
C

Note that this function returns a list of values. There are also other functions you could use, but with more convoluted code as a result.

Jigsaw Puzzle (Parson’s Problem) Programming Example

What we need to do is figure out the length of the string bases, which we will call n_bases. Then, we just need to pick a random index from the choices we have, which are [0, 1, 2, ..., n_bases].

In [9]:
import math 
import random
bases = "ACTTGCTTGAC" 
n_bases = len(bases)
idx = random.randrange(n_bases)
print("random base", bases[idx], "base index", idx)
random base T base index 3

You may have used different variable names in your successful solution!

When Is Help Available?

They have forgotten to import the module import math, the module still needs to be imported before you can get help for it! The following code will do the job just fine:

import math
help(math)

Importing Specific Items

Filling in the blanks should get you

In [10]:
from math import degrees, pi
angle = degrees(pi / 2)
print(angle)
90.0

Most likely you find this version easier to read since it’s less dense. The main reason not to use this form of import is to avoid name clashes. For instance, you wouldn’t import degrees this way if you also wanted to use the name degrees for a variable or function of your own. Or if you were to also going to import a function named degrees from another library.