Rosalind — Mendel’s First Law

Another Rosalind problem, where an easy formula is hidden under a huge background.

Let’s try to understand the problem by illustrating the example case

Example case: 2 2 2 produces 0.78333


k = number of homozygous dominant = number of YY = 2

m = number of heterozygous = number of Yy = 2

n = number of homozygous recessive = number of yy = 2

So, there are 2 + 2 + 2 = 6 organisms. Since they can all mate each other, it gives us 15 pairs (6 * (6–1) /2 = 15). We have to fiund out:

(number of productions where dominant allele “Y” is present) / (total number of productions)

You could actually generate all possible pairs, collect and their production, and calculate the result.


The key here to generate productions for all cases. There are 6 possible cases:

1. Homozygous dominant-Homozygous dominant(YY — YY)

Produces all homozygous dominant

2. Homozygous Recessive-Homozygous Recessive (yy-yy)

Produces all homozygous recessive

3. Heterozygous-Heterozygous (Yy-Yy)

Produces 2 heterozygous, 1 homozygous dominant, and 1 homozygous recessive

4. Homozygous Dominant-Heterozygous (YY-Yy)

Produces 2 heterozygous, 2 homozygous dominant

5. Homozygous Recessive-Heterozygous (yy-Yy)

Produces 2 heterozygous, 2 homozygous receissive

5. Homozygous Dominant-Homozygous Recessive(YY-yy)

Produces 4 heterozygous

Since each pair gives you 4 possible productions, for 15 possible pairs, there are total 15 X 4 = 60 possible productions. You can count and find out that among them, dominant allele (Y) is present in 47 cases, and 47/60 = 0.78333, which is the result.


However, I found other people solved this with even much shorter code. One guy even solved it with a one-line formula. You might want to check them too.

Any questions — please leave a comment. Glad to help!

Jack of a few trades, master of none.