Beta

Punnett Squares

17 of 22Katbow

Description:

In biology, a genotype is the genetic make up of an individual which determines a specific trait. There are three possible ways to describe a genotype:

  • Homozygous dominant. Represented by two upper case letters, e.g. BB.
  • Heterozygous. Represented by an upper and lower case letter, e.g. Bb.
  • Homozygous recessive. Represented by two lower case letters, e.g. bb.

Given the genotypes of each parent, a Punnett square can show the probablilty of the child's genotype. While this is a useful tool, it can be tedious to draw many times and calculate the probability of each genotype.

Your task is to write a function punnettSquare(father, mother), where father and mother are genotypes, for example punnettSquare("Bb", "BB"). It will return an object containing (string array for C#):

  • The Punnett square, including spaces and new lines for readability.
  • The probabilty for each genotype listed above (dominant, heterozygous, and recessive). This is written as a number representing a percent eg. 50 means 50%.

Rules for the Punnett Square

  • The square should always have the father genotype across the top of the table, and the mother genotype should be down the left side of the table.
  • The square should include spaces and new lines to ensure readability. The format is "--B--b\nB-BB-bB\nb-Bb-bb", where - represents a space.
  • The uppercase letter will always come before the lowercase letter in the square eg Bb instead of bB.

Example:

punnettSquare("Bb", "Bb")

should return

{ punnett: 
"  B  b
B BB Bb
b Bb bb", 
dominantProb: 25,
heterozygousProb: 50,
recessiveProb: 25 }

Example for C#:

PunnettSquare("Bb", "BB")

should return

string[] {"  B  b\nB BB Bb\nB BB Bb", "50", "50", "0"};

If both parents do not have exactly 2 letters, return false. For C# it should return null. Any letters may be used for the genotypes.

Fundamentals

More By Author:

Check out these other kata created by Katbow

Stats:

CreatedNov 22, 2015
PublishedNov 28, 2015
Warriors Trained171
Total Skips69
Total Code Submissions138
Total Times Completed22
JavaScript Completions17
C# Completions6
Total Stars7
% of votes with a positive feedback rating88% of 13
Total "Very Satisfied" Votes10
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes0
Total Rank Assessments13
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • Katbow Avatar
  • aryan-firouzian Avatar
  • hobovsky Avatar
Ad