Home

Branches and Loops

Chapters 7, 8 and 11 of Hello World are the conceptual heart of programming. (We will skip Chapter 6 and 10, as they are about writing games. Chapter 9 on comments is important but very easy.)

Branching

Chapter 7 of Hello World brings us to if statements.

Gene mapping

In case a genome has not been sequenced, the distance between two genes can still be estimated based on the frequency of crossovers that occur during meiosis. The image below shows that a crossover results in a recombination frequency of 50%.

crossover

The map distance (d) can be estimated based on the recombination frequency (RF) using the Haldane’s mapping equation:

d=-1/2 * ln(1-2*RF)

The recombination frequency is never negative, nor higher than 0.5.

Write a program that asks for a recombination frequency as input and gives the map distance as output. In case a frequency lower than zero or higher than 0.5 is entered, the program should tell the user that the recombination frequency must be between 0 en 0.5. The natural logarithm can be calculated using log() from the numpy library.

For loops

Chapter 8 of Hello World introduces simple loops.

The range() function is very important.

Population Dynamics

The number of predators and prey in a region depend on each other. Let's make the following assumptions for a model describing these relationships: The prey population increases by 5% each day as the net result of birth and natural death. At the same time, the population is decreased by the presence of predators. The more predators there are, the more prey is being caught and the more prey there is, the easier it is for predators to catch the prey. Specifically, the prey population is reduced by 0.0002*number of predators*number of prey, which are present at the start of the day.

The predator population decreases by 10% per day. In addition, the presence of prey has a positive influence on the number of predators. This depends on how much prey is being caught and this is again dependent on the number of predators as well as the number of prey. The increase amounts to 0.0001*number of predators*number of prey each day. The numbers are again those at the start of the day.

You may wish to try some warmup problems before the following.

Evaluate how much predator and prey there will be after 200 days if there are 1000 prey and 100 predators present in the beginning. You don’t need to round the numbers.

If you evaluate different numbers of days, you will find that the populations fluctuate over time. In this example, the predator and prey populations change according to the Lotka-Volterra equation, except for that that equation describes the changes per infinitely small time step, instead of per day. The equation is further discussed in the dynamical systems part.

General loops

From Chapter 11 of Hello World, covers general loops. You may also find this video lecture on general loops useful.

Now we have if branches and while loops at our disposal, and we can put loops within branches within loops, to as many levels as we want. With this ability, plus the resources to read and write files as much as we want, we have a minimal but complete programming language, said to be Turing complete, or equivalent to a a Turing machine.

You may try more warmup problems before continuing below.

Turing and ESP

There is no getting away from Alan Turing in abstract discussions about computers. In 1950, Turing wrote a philosophical article on Computing Machinery and Intelligence. In it, Turing proposes what he calls an `imitation game' (now called the Turing test). A human `interrogator' is talking by teletype to two `witnesses', one human and one a computer. The interrogator's job is to figure out which is which. The human witness's job is to help the interrogator, the computer's job is to confuse the interrogator (that is, pretend to be the human). Turing proposes the ability, if achieved, to fool most human interrogators as an operational definition of intelligence.

The whole article is entertaining. Most of it consists of Turing considering and wittily rebutting nine different objections to his proposal. The last of these is that humans are allegedly capable of extra-sensory perception, but computers are not. Turing's discussion includes the following passage.

Let us play the imitation game, using as witnesses a man who is good as a telepathic receiver, and a digital computer. The interrogator can ask such questions as `What suit does the card in my right hand belong to?' The man by telepathy or clairvoyance gives the right answer 130 times out of 400 cards. The machine can only guess at random, and perhaps get 104 right, so the interrogator makes the right identification.

Getting the card suit right 130 times in 400 trials does not sound so impressive. How often would random guessing do at least that well?
Generate the following console output:
Random guessing yields at least 130 correct suits out of 400 trials in ...% of the cases.

If you wish, you can browse ahead at this point to Chapter 23 of Hello World, which is on random numbers. But it is not essential, as the simple function we saw in the number-guessing example in the first chapter is also adequate.

Submit the program

Text Files

File input/output is covered in detail in Chapter 22 of Hello World, but the essentials are in this short example containing a while-loop

inp = open('original.txt')
out = open('copy.txt','w')
while True:
    s = inp.readline()
    if not s:
        break
    out.write(s)
inp.close()
out.close()

Files like .txt and .csv can directly be opened in this way. For more specialized file formats special functions are required to read and write them. Many such functions are available in different libraries. The urllib.urlopen function from Chapter 5 is just one example.

Write a program that can read a FASTA text file and output just the lines containing the sequence.