Skip to content

Commit

Permalink
Problem 07 - Ch 06
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak-malik committed Feb 9, 2017
1 parent 3748746 commit 919efc7
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/com/deepak/ctci/Ch06_Math_And_Logic_Puzzles/Problem_07.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.deepak.ctci.Ch06_Math_And_Logic_Puzzles;

import java.util.Random;

/**
* <br> Problem Statement :
*
Expand All @@ -12,7 +14,7 @@
* families should ensure that they have one girl or else they face
* massive fines. If all families abide by this policy, that is,
* they continue to have children until they have one girl,
* at which point they immediately stop what will the gender ratio
* at which point they immediately stop. What will the gender ratio
* of the new generation be?. Assume that the odds of someone having
* a boy or girl is equal. Solve this logically then write a simulation of it.
*
Expand All @@ -21,5 +23,58 @@
* @author Deepak
*/
public class Problem_07 {


/**
* There are no unit tests for this because, every time
* ratio will be different since random generation is
* involved here
*
* @param args
*/
public static void main(String[] args) {
System.out.println("Ration for Girls v/s Boys => " + runFamilies(100) * 100);
}

/**
* Method to run the logic on n number of families
*
* @param numberOfFamilies
* @return {@link double}
*/
public static double runFamilies(int numberOfFamilies) {
int boys = 0;
int girls = 0;
/* Find the genders for each family and calculate percentage */
for (int i = 0; i < numberOfFamilies; i++) {
int[] genders = runOneFamily();
boys += genders[0];
girls += genders[1];
}
return girls / (double) (boys + girls);
}

/**
* Method to run one family
*
* @return {@link int[]}
*/
public static int[] runOneFamily() {
int boys = 0;
int girls = 0;
Random random = new Random();
/* Keep going until girl child is born */
while (girls == 0) {
/* If true, it's a boy else a girl */
boolean nextChild = random.nextBoolean();
if (nextChild) {
boys++;
} else {
girls++;
}
}
/* Create an integer for count of both */
int[] genders = {boys, girls};
return genders;
}

}

0 comments on commit 919efc7

Please sign in to comment.