The Challenge

Everybody loves genetics, right? In this challenge, you will be given the genotypes of two parents. You must find each possible genotype of a child produced by the parents. You must account for dominant and recessive alleles as well as incomplete dominance and codominance.

An example input for the parents could be:


Parent one (on the left) has a incompletely dominant R allele and a dominant R allele. Parent two (on the right) has a dominant R allele and a recessive allele.

Each parent is separated from the other by an x. The input will not contain an x allele.

An incompletely dominant allele will be followed by an apostrophe ('). Codominant alleles just refers to multiple different types of alleles combined together (A and W for example).


  • Shortest code wins (measured in bytes).
  • Each possibly genotype must be separated from the other genotypes by a comma.
  • Phenotypes don't matter.
  • Any letter can be used in the input except for x (which differentiates the parents).
  • Duplicate genotypes should only be printed once.
    • Duplicates contain the exact same letters of the same case and type of allele. IF you have WY and YW in your answer, they are considered duplicates.
  • No more than three different letters will be used in one input.
  • The order of the letters in the output does not matter.
  • Answer can be in the form of a string or an array.


Input 1 (the previous example):


Output 1:


Alternate output 1 (array):


Input 2:


Output 2:

//Or an array, whichever you want

Input 3:


Output 3:

//Once again, an array is fine
  • \$\begingroup\$ Can the output be an array? \$\endgroup\$
    – Doorknob
    Mar 31, 2016 at 1:11
  • \$\begingroup\$ Sure. I'll add that to the rules. \$\endgroup\$
    – NuffsaidM8
    Mar 31, 2016 at 1:13
  • \$\begingroup\$ codegolf.stackexchange.com/questions/76428/… duplicate, flagged as \$\endgroup\$
    – Leaky Nun
    Mar 31, 2016 at 1:26
  • \$\begingroup\$ @KennyLau Are you sure? I don't see how they're all that similar. \$\endgroup\$
    – Alex A.
    Mar 31, 2016 at 1:53
  • 3
    \$\begingroup\$ @NuffsaidM8 Can you add some examples of dominant/recessive allele test cases which can demonstrate that it's not just a simple case of "removing duplicates" as Kenny alludes to? It would also allow people to more easily verify that their code meets all the corner cases. \$\endgroup\$ Mar 31, 2016 at 1:58

4 Answers 4


Pyth, 20 18 bytes


Test suite.

         z          get input
        : ".'?"1    regex match for any character followed by an optional '
       c        \x  chop on the element "x", resulting in [["R'","R"],["R","r"]]
     *F             splat over Cartesian product, resulting in pairs of "genes"
                      from the first "parent" and from the second
 m  d               map over each resulting pair...
   S                sort (so that we can dedup later)
  s                 concatenate
{                   remove duplicates

JavaScript (ES6), 114 112 104 90 88 bytes

f=>(c=new Set,[z,y]=f.split`x`.map(b=>b.match(/.'?/g)),z.map(e=>y.map(g=>c.add(e+g))),c)

Thanks to Doorknob, Downgoat, Kenny Lau and Neil for helping me golf this down a bit :)

  • \$\begingroup\$ The \w can be replaced by . (because there will not be ψ') \$\endgroup\$
    – Leaky Nun
    Mar 31, 2016 at 2:10
  • 1
    \$\begingroup\$ ... which means you can shorten the regex to /.'?/g (note the removed i flag, which was unnecessary in the first place), the one I used in my answer. \$\endgroup\$
    – Doorknob
    Mar 31, 2016 at 2:11
  • \$\begingroup\$ The i (case-insensitive) tag can be removed as well (as well as the g global tag?). \$\endgroup\$
    – Leaky Nun
    Mar 31, 2016 at 2:11
  • 1
    \$\begingroup\$ Thanks guys! @KennyLau, you can't take the g flag off, otherwise it results in undefined \$\endgroup\$
    – Quill
    Mar 31, 2016 at 2:21
  • \$\begingroup\$ This fails for ABxAB, since AB and BA are identical.for the purposes of the challenge. \$\endgroup\$
    – Doorknob
    Mar 31, 2016 at 2:26

Ruby, 82 77 bytes

Anonymous function, takes the input string as its argument.

Edited to properly account for ABxAB.

-5 bytes from @Doorknob.

->g{a,b=g.split(?x).map{|s|s.scan /.'?/}
  • 3
    \$\begingroup\$ .map(&:sort).uniq.map &:join is shorter as .map{|x|x.sort*''}.uniq. \$\endgroup\$
    – Doorknob
    Mar 31, 2016 at 2:31
  • \$\begingroup\$ Nice! I learned a new Ruby shorthand today! \$\endgroup\$
    – Value Ink
    Mar 31, 2016 at 2:40

Python 3.5 - 196 bytes:

def b(r):import itertools,re;r=[];[r.append(h)for h in itertools.permutations(re.sub("(?<=[a-zA-Z'])(?=[a-zA-Z])",' ',r.replace('x','')).split(' '),2)if h[::-1]not in r and h not in r];return r

A nice one liner, using some regular expressions.

Note: Around each letter with an apostrophe are double quotes, and I hope that's okay (e.g., in the output, R' would come out as "R'"). Also, for the last test case, it returns [('W', "Y'"), ('W', 'y'), ('Y', 'W'), ('Y', "Y'"), ('Y', 'y'), ('y', "Y'")] instead of just WY',Wy,YY',Yy. I also hope that is okay. If any of these conditions are an issue, just let me know, and I will fix them.

  • \$\begingroup\$ YW and WY are considered duplicates for this challenge, and it is therefor against the rules to print both of them. I have added a bit of clarification to the rules to avoid this in the future. \$\endgroup\$
    – NuffsaidM8
    Apr 1, 2016 at 12:25
  • \$\begingroup\$ @NuffsaidM8 Actually. If you look closer, one of them is W,"Y'", whereas the other is just Y,W. Like I said in my answer, those letters with an apostrophe in front of them will have a pair of quotes around them, so the "duplicates" you are talking about are not really duplicates. One of them is incomplete dominance, whereas the other signifies codominance. \$\endgroup\$
    – R. Kap
    Apr 1, 2016 at 17:04
  • \$\begingroup\$ I apologize, I was looking at the second and third output yet completely ignored the fact that one is a dominant y and one is recessive! \$\endgroup\$
    – NuffsaidM8
    Apr 1, 2016 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.