Determine offspring sex based on genes XX and XY chromosomes using Dart (2.14)

Determine offspring sex based on genes XX and XY chromosomes using Dart (2.14)

Hello!! After a brief hiatus, I’ve returned to my world of technical writing, and this time, I’m merging an intriguing topic with a coding challenge. Let’s dive right in! (Note that this article will be left open for discussion as I would like to know what everyone thinks.)

So let us take a look at the Problem Statement that we will be tackling today:

Determine offspring sex based on genes XX and XY chromosomes

The male gametes or sperm cells in humans and other mammals are heterogametic and contain one of two types of sex chromosomes. They are either X or Y. The female gametes or eggs however, contain only the X sex chromosome and are homogametic.

The sperm cell determines the sex of an individual in this case. If a sperm cell containing an X chromosome fertilizes an egg, the resulting zygote will be XX or female. If the sperm cell contains a Y chromosome, then the resulting zygote will be XY or male.

Determine if the sex of the offspring will be male or female based on the X or Y chromosome present in the male’s sperm.

If the sperm contains the X chromosome, return “Congratulations! You’re going to have a daughter.”; If the sperm contains the Y chromosome, return “Congratulations! You’re going to have a son.”;

Initial Thoughts

At first glance, one might be tempted to write a solution like this:

String chromosome_check(String sperm){
  if(sperm.contains("Y"){
    return "Congratulations, You're going to have a son.";
  }
  else {
    return "Congratulations! You're going to have a daughter.";
  }
}

While this isn’t incorrect (I’ve seen similar solutions), I pondered its efficiency and robustness. Is this the best we can do?

My Approach

Here’s the solution that I have crafted:

String chromosome_check(String sperm){
  if(sperm.split("").every((s) => s == "X")){
    return "Congratulations! You're going to have a daughter.";
  }
  else {
    return "Congratulations! You're going to have a son.";
  }
}

The rational behind writing that logic is because, the test cases is as such:

void main() {
  test('Basic tests', () {
    expect(chromosome_check('XY'), equals("Congratulations! You're going to have a son."));
    expect(chromosome_check('XX'), equals("Congratulations! You're going to have a daughter."));
  });
}

From the tests, it’s evident that the presence of “X” is consistent, regardless of the offspring’s sex. However, the presence of “Y” is the determining factor. While one could argue that simply checking for “Y” would suffice, what if a future developer overlooks this nuance?

Wrapping Up

There you have it! While both solutions are valid, the latter offers a more comprehensive check. But, as mentioned earlier, I’m eager to hear your thoughts. Do you have a different approach or feedback on mine? Drop your comments below, and let’s get the conversation started!

About Me

I am Zaahra, a Google Women Techmakers Ambassador who enjoy mentoring people and writing about technical contents that might help people in their developer journey. I also enjoy building stuffs to solve real life problems.

To reach me:

LinkedIn: https://www.linkedin.com/in/faatimah-iz-zaahra-m-0670881a1/

X (previously Twitter): _fz3hra

GitHub: https://github.com/fz3hra

Cheers,

Umme Faatimah-Iz-Zaahra Mujore | Google Women TechMakers Ambassador | Software Engineer