Mendelian Inheritance Probability

Foundational Computational Biology Probability Mendelian Genetics Combinatorics Formatting
Significance:

Mendel's laws are the oldest quantitative model in biology, and they are still the
first thing a genetic counsellor reaches for when a couple asks about the risk of passing on a
recessive condition. Computing the probability that two randomly chosen individuals from a population
produce offspring with a dominant phenotype is a small exercise in conditional probability, but it is
exactly the reasoning that underpins pedigree analysis and carrier screening.

Statement

A population contains k homozygous dominant individuals (AA), m heterozygous
individuals (Aa), and n homozygous recessive individuals (aa).

Two individuals are selected uniformly at random without replacement and mate.

Compute the probability that their offspring displays the dominant phenotype — that is, has at least
one A allele.

Print the probability rounded to five decimal places.

Input — read from standard input
Variable Type Description
k
line 1
int Number of homozygous dominant (AA) individuals
0 <= k <= 100
m
line 1
int Number of heterozygous (Aa) individuals
0 <= m <= 100
n
line 1
int Number of homozygous recessive (aa) individuals
0 <= n <= 100

These variables are already read for you in the starter code on the right.

Output

str probability of dominant phenotype in the offspring, to five decimal places

Sample Cases
Sample 1
Input
2 2 2
Expected Output
0.78333
Two of each genotype; the classic textbook example.
Sample 2
Input
5 0 0
Expected Output
1.00000
Every individual is homozygous dominant, so the offspring must show the dominant phenotype.

Submit also runs your code against 5 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • The three integers are given on one line, separated by single spaces
  • 0 <= k, m, n <= 100 and k + m + n >= 2
  • Selection is without replacement, so the second draw depends on the first
  • Output exactly five decimal places
Further Reading
  • It is easier to compute the probability of a recessive offspring and subtract from 1.
  • Only three pairings can produce aa offspring: Aa x Aa (1/4), Aa x aa (1/2), aa x aa (1).
  • The number of unordered pairs from a total of t individuals is t*(t-1)/2.

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: k (int), m (int), n (int)
Next Problem
Edit Distance