forked from RodneyShag/HackerRank_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 25 solutions. Changed folder hierarchy
- Loading branch information
1 parent
0670409
commit bcf11f2
Showing
59 changed files
with
736 additions
and
46 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Answer: 5/6 | ||
|
||
There are 6 possibilities on each die. On 2 dice, there are 6 * 6 = 36 possibilities | ||
|
||
There are 6 cases where sum >= 10: (4,6), (5,6), (6,6), (5,5), (5,6), (6,6) | ||
|
||
This gives us probability(sum >= 10) = 6/36 = 1/6 | ||
|
||
That means probability(sum <= 9) = 1 - 1/6 = 5/6 |
15 changes: 15 additions & 0 deletions
15
10 Days of Statistics/Day 2 - Compound Event Probability/Solution.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Answer: 17/42 | ||
|
||
Urn X has a 4/7 probability of giving a red ball | ||
Urn Y has a 5/9 probability of giving a red ball | ||
Urn Z has a 1/2 probability of giving a red ball | ||
|
||
Urn X has a 3/7 probability of giving a black ball | ||
Urn Y has a 4/9 probability of giving a black ball | ||
Urn Z has a 1/2 probability of giving a black ball | ||
|
||
P(2 red, 1 black) = P(Red Red Black) + P(Red Black Red) + P(Black Red Red) | ||
= (4/7)(5/9)(1/2) + (4/7)(4/9)(1/2) + (3/7)(5/9)(1/2) | ||
= 20/126 + 16/126 + 15/126 | ||
= 51/126 | ||
= 17/42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Answer: 1/9 | ||
|
||
There are 6 possibilities on each die. On 2 dice, there are 6 * 6 = 36 possibilities | ||
|
||
There are 4 cases that match the desired criteria: (1,5) (5,1) (2,4) (4,2) | ||
|
||
This gives us a probability of 4/36 = 1/9 |
5 changes: 5 additions & 0 deletions
5
10 Days of Statistics/Day 3 - Cards of the Same Suit/Solution.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Answer: 12/51 | ||
|
||
There are 13 of each suit in a deck. | ||
|
||
We can view this as 2 separate events. First we draw 1 card and see what suit it is. Whatever suit it is, there are 12 of the same matching suit remaining in the 51-card deck. Therefore, when we draw the 2nd card, there is a 12/51 chance that it is the same suit. |
29 changes: 29 additions & 0 deletions
29
10 Days of Statistics/Day 3 - Conditional Probability/Solution.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Answer: 1/3 | ||
|
||
****** 2 valid answers: 1/3 or 1/2 | ||
|
||
Reference: https://en.wikipedia.org/wiki/Boy_or_Girl_paradox | ||
|
||
This question is known as the "Boy or Girl Paradox". The actual answer is either 1/3 or 1/2 depending on how the question is interpreted. Here are 2 ways to interpret the question: | ||
|
||
1) From all families with two children, at least one of whom is a boy, a family is chosen at random. This would yield the answer of 1/3. | ||
2) From all families with two children, one child is selected at random, and the sex of that child is specified to be a boy. This would yield an answer of 1/2. | ||
|
||
|
||
******* Interpretation (1) gives an answer of 1/3 | ||
|
||
There are 4 possible scenarios: (B, B), (B, G), (G, B), (G, G) | ||
|
||
We know that 1 child is a boy, so now we have 3 scenarios: (B, B), (B, G), (G, B) | ||
|
||
When asked, "what is the probability that both children are boys", the only scenario that matches this is: (B, B). That is, only 1 of the 3 scenarios satisfies the critera, so there is a 1/3 chance that both children are boys. | ||
|
||
|
||
******* Interpretation (2) gives an answer of 1/2 | ||
|
||
In this scenario, a child was selected at random from a set of all children that have exactly 1 sibling. Knowing the fact that the child is a boy does not give us any information on whether his sibling is a boy or a girl. | ||
|
||
|
||
******* HackerRank's correct answer | ||
|
||
The accepted answer on HackerRank is 1/3. This aligns with the fact that the problem wording more closely resembles Interpretation (1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Answer: 2/3 | ||
|
||
After drawing the first marble, we are left with 2 red marbles and 4 blue marbles. Now we calculate the probability of drawing a blue marble as : | ||
|
||
= (# of blue marbles) / (total # of marbles) | ||
= 4 / (2 + 4) | ||
= 2 / 3 |
41 changes: 41 additions & 0 deletions
41
10 Days of Statistics/Day 4 - Binomial Distribution I/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
double ratio = 1.09; | ||
|
||
double p = ratio / (1 + ratio); | ||
int n = 6; | ||
|
||
/* Calculate result */ | ||
double result = 0; | ||
for (int x = 3; x <= n; x++) { | ||
result += binomial(n, x, p); | ||
} | ||
System.out.format("%.3f", result); | ||
} | ||
|
||
private static Double binomial(int n, int x, double p) { | ||
if (p < 0 || p > 1 || n < 0 || x < 0 || x > n) { | ||
return null; | ||
} | ||
return combinations(n, x) * Math.pow(p, x) * Math.pow(1 - p, n - x); | ||
} | ||
|
||
private static Long combinations(int n, int x) { | ||
if (n < 0 || x < 0 || x > n) { | ||
return null; | ||
} | ||
return factorial(n) / (factorial(x) * factorial(n - x)); | ||
} | ||
|
||
private static Long factorial (int n) { | ||
if (n < 0) { | ||
return null; | ||
} | ||
long result = 1; | ||
while (n > 0) { | ||
result *= n--; | ||
} | ||
return result; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
10 Days of Statistics/Day 4 - Binomial Distribution II/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Values given in problem statement */ | ||
double p = 0.12; | ||
int n = 10; | ||
|
||
/* "No more than 2 rejects" */ | ||
double result = 0; | ||
for (int x = 0; x <= 2; x++) { | ||
result += binomial(n, x, p); | ||
} | ||
System.out.format("%.3f%n", result); | ||
|
||
/* "At least 2 rejects" */ | ||
result = 1 - binomial(n, 0, p) - binomial(n, 1, p); | ||
System.out.format("%.3f%n", result); | ||
} | ||
|
||
private static Double binomial(int n, int x, double p) { | ||
if (p < 0 || p > 1 || n < 0 || x < 0 || x > n) { | ||
return null; | ||
} | ||
return combinations(n, x) * Math.pow(p, x) * Math.pow(1 - p, n - x); | ||
} | ||
|
||
private static Long combinations(int n, int x) { | ||
if (n < 0 || x < 0 || x > n) { | ||
return null; | ||
} | ||
return factorial(n) / (factorial(x) * factorial(n - x)); | ||
} | ||
|
||
private static Long factorial (int n) { | ||
if (n < 0) { | ||
return null; | ||
} | ||
long result = 1; | ||
while (n > 0) { | ||
result *= n--; | ||
} | ||
return result; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
10 Days of Statistics/Day 4 - Geometric Distribution I/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.util.Scanner; | ||
|
||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Read and save input */ | ||
Scanner scan = new Scanner(System.in); | ||
int numerator = scan.nextInt(); | ||
int denominator = scan.nextInt(); | ||
int n = scan.nextInt(); | ||
scan.close(); | ||
|
||
/* Geometric Series */ | ||
double p = (double) numerator / denominator; | ||
System.out.format("%.3f", geometric(n, p)); | ||
} | ||
|
||
private static double geometric (int n, double p) { | ||
return Math.pow(1 - p, n - 1) * p; | ||
} | ||
|
||
private static Long combinations(int n, int x) { | ||
if (n < 0 || x < 0 || x > n) { | ||
return null; | ||
} | ||
return factorial(n) / (factorial(x) * factorial(n - x)); | ||
} | ||
|
||
private static Long factorial (int n) { | ||
if (n < 0) { | ||
return null; | ||
} | ||
long result = 1; | ||
while (n > 0) { | ||
result *= n--; | ||
} | ||
return result; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
10 Days of Statistics/Day 4 - Geometric Distribution II/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.Scanner; | ||
|
||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Read and save input */ | ||
Scanner scan = new Scanner(System.in); | ||
int numerator = scan.nextInt(); | ||
int denominator = scan.nextInt(); | ||
int n = scan.nextInt(); | ||
scan.close(); | ||
|
||
/* Geometric Series */ | ||
double p = (double) numerator / denominator; | ||
double result = 0; | ||
for (int i = 1; i <= 5; i++) { | ||
result += geometric(i, p); | ||
} | ||
System.out.format("%.3f", result); | ||
} | ||
|
||
private static double geometric (int n, double p) { | ||
return Math.pow(1 - p, n - 1) * p; | ||
} | ||
|
||
private static Long combinations(int n, int x) { | ||
if (n < 0 || x < 0 || x > n) { | ||
return null; | ||
} | ||
return factorial(n) / (factorial(x) * factorial(n - x)); | ||
} | ||
|
||
private static Long factorial (int n) { | ||
if (n < 0) { | ||
return null; | ||
} | ||
long result = 1; | ||
while (n > 0) { | ||
result *= n--; | ||
} | ||
return result; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
10 Days of Statistics/Day 5 - Normal Distribution I/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
double mean = 20; | ||
double std = 2; | ||
System.out.format("%.3f%n", cumulative(mean, std, 19.5)); | ||
System.out.format("%.3f%n", cumulative(mean, std, 22) - cumulative(mean, std, 20)); | ||
} | ||
|
||
/* Calculates cumulative probability */ | ||
public static double cumulative(double mean, double std, double x) { | ||
double parameter = (x - mean) / (std * Math.sqrt(2)); | ||
return (0.5) * (1 + erf(parameter)); | ||
} | ||
|
||
/* Source: http://introcs.cs.princeton.edu/java/21function/ErrorFunction.java.html */ | ||
// fractional error in math formula less than 1.2 * 10 ^ -7. | ||
// although subject to catastrophic cancellation when z in very close to 0 | ||
// from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2 | ||
public static double erf(double z) { | ||
double t = 1.0 / (1.0 + 0.5 * Math.abs(z)); | ||
|
||
// use Horner's method | ||
double ans = 1 - t * Math.exp( -z*z - 1.26551223 + | ||
t * ( 1.00002368 + | ||
t * ( 0.37409196 + | ||
t * ( 0.09678418 + | ||
t * (-0.18628806 + | ||
t * ( 0.27886807 + | ||
t * (-1.13520398 + | ||
t * ( 1.48851587 + | ||
t * (-0.82215223 + | ||
t * ( 0.17087277)))))))))); | ||
if (z >= 0) return ans; | ||
else return -ans; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
10 Days of Statistics/Day 5 - Normal Distribution II/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
double mean = 70; | ||
double std = 10; | ||
|
||
double result_1 = 100 * (1 - cumulative(mean, std, 80)); | ||
double result_2 = 100 * (1 - cumulative(mean, std, 60)); | ||
double result_3 = 100 * cumulative(mean, std, 60); | ||
|
||
System.out.format("%.2f%n", result_1); | ||
System.out.format("%.2f%n", result_2); | ||
System.out.format("%.2f%n", result_3); | ||
} | ||
|
||
/* Calculates cumulative probability */ | ||
public static double cumulative(double mean, double std, double x) { | ||
double parameter = (x - mean) / (std * Math.sqrt(2)); | ||
return (0.5) * (1 + erf(parameter)); | ||
} | ||
|
||
/* Source: http://introcs.cs.princeton.edu/java/21function/ErrorFunction.java.html */ | ||
// fractional error in math formula less than 1.2 * 10 ^ -7. | ||
// although subject to catastrophic cancellation when z in very close to 0 | ||
// from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2 | ||
public static double erf(double z) { | ||
double t = 1.0 / (1.0 + 0.5 * Math.abs(z)); | ||
|
||
// use Horner's method | ||
double ans = 1 - t * Math.exp( -z*z - 1.26551223 + | ||
t * ( 1.00002368 + | ||
t * ( 0.37409196 + | ||
t * ( 0.09678418 + | ||
t * (-0.18628806 + | ||
t * ( 0.27886807 + | ||
t * (-1.13520398 + | ||
t * ( 1.48851587 + | ||
t * (-0.82215223 + | ||
t * ( 0.17087277)))))))))); | ||
if (z >= 0) return ans; | ||
else return -ans; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
10 Days of Statistics/Day 5 - Poisson Distribution I/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import java.util.Scanner; | ||
|
||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Read and save input */ | ||
Scanner scan = new Scanner(System.in); | ||
double lambda = scan.nextDouble(); | ||
int k = scan.nextInt(); | ||
scan.close(); | ||
|
||
System.out.println(poisson(k, lambda)); | ||
} | ||
|
||
private static double poisson(int k, double lambda) { | ||
return (Math.pow(lambda, k) * Math.pow(Math.E, -1 * lambda)) / factorial(k); | ||
} | ||
|
||
private static Long factorial (int n) { | ||
if (n < 0) { | ||
return null; | ||
} | ||
long result = 1; | ||
while (n > 0) { | ||
result *= n--; | ||
} | ||
return result; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
10 Days of Statistics/Day 5 - Poisson Distribution II/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.Scanner; | ||
|
||
/* Useful Formulas: | ||
https://www.hackerrank.com/challenges/s10-poisson-distribution-2/forum/comments/175398 | ||
https://www.hackerrank.com/challenges/s10-poisson-distribution-2/forum/comments/176962 | ||
*/ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Read and save input */ | ||
Scanner scan = new Scanner(System.in); | ||
double A = scan.nextDouble(); | ||
double B = scan.nextDouble(); | ||
scan.close(); | ||
|
||
/* E[x^2] = lambda + lambda^2. Plug this into each formula */ | ||
double dailyCostA = 160 + 40 * (A + (A * A)); | ||
double dailyCostB = 128 + 40 * (B + (B * B)); | ||
|
||
/* Print output */ | ||
System.out.format("%.3f%n", dailyCostA); | ||
System.out.format("%.3f%n", dailyCostB); | ||
} | ||
} |
Oops, something went wrong.