← Back to challenges

Which Generation Are You?

PythonMediumstringslogicconditionsobjects

Instructions

Try finding your ancestors and offspring with code.

Create a function that takes a number x and a character y ("m" for male, "f" for female), and returns the name of an ancestor (m/f) or descendant (m/f).

  • If the number is negative, return the related ancestor.
  • If positive, return the related descendant.
  • You are generation 0. In the case of 0 (male or female), return "me!".

Examples

generation(2, "f") ➞ "granddaughter"

generation(-3, "m") ➞ "great grandfather"

generation(1, "f") ➞ "daughter"

Notes

GenerationMaleFemale
-3great grandfathergreat grandmother
-2grandfathergrandmother
-1fathermother
0me!me!
1sondaughter
2grandsongranddaughter
3great grandsongreat granddaughter
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Less Than, Greater Than