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 4 solutions, READMEs, MIT LICENSE
- Loading branch information
1 parent
bcf11f2
commit 5ac5cfb
Showing
92 changed files
with
744 additions
and
26 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
10 Days of Statistics/Day 0 - Mean, Median, and Mode/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
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
4 changes: 4 additions & 0 deletions
4
10 Days of Statistics/Day 1 - Interquartile Range/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
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
4 changes: 4 additions & 0 deletions
4
10 Days of Statistics/Day 1 - Standard Deviation/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
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
5 changes: 5 additions & 0 deletions
5
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
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
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
5 changes: 5 additions & 0 deletions
5
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
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
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
4 changes: 4 additions & 0 deletions
4
10 Days of Statistics/Day 6 - The Central Limit Theorem 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
4 changes: 4 additions & 0 deletions
4
10 Days of Statistics/Day 6 - The Central Limit Theorem 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
4 changes: 4 additions & 0 deletions
4
10 Days of Statistics/Day 6 - The Central Limit Theorem III/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
64 changes: 64 additions & 0 deletions
64
10 Days of Statistics/Day 7 - Pearson Correlation Coefficient 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,64 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
import java.util.Scanner; | ||
|
||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Read and save input */ | ||
Scanner scan = new Scanner(System.in); | ||
int size = scan.nextInt(); | ||
double [] xs = new double[size]; | ||
double [] ys = new double[size]; | ||
for (int i = 0; i < size; i++) { | ||
xs[i] = scan.nextDouble(); | ||
} | ||
for (int i = 0; i < size; i++) { | ||
ys[i] = scan.nextDouble(); | ||
} | ||
|
||
System.out.println(pearson(xs, ys)); | ||
} | ||
|
||
/* Calculates Pearson coefficient */ | ||
private static Double pearson(double [] xs, double [] ys) { | ||
if (xs == null || ys == null || xs.length != ys.length) { | ||
return null; | ||
} | ||
double xMean = getMean(xs); | ||
double yMean = getMean(xs); | ||
int n = xs.length; | ||
|
||
double numerator = 0; | ||
for (int i = 0; i < n; i++) { | ||
numerator += (xs[i] - xMean) * (ys[i] - yMean); | ||
} | ||
return numerator / (n * standardDeviation(xs) * standardDeviation(ys)); | ||
} | ||
|
||
private static Double getMean(double [] array) { | ||
if (array == null) { | ||
return null; | ||
} | ||
double total = 0; | ||
for (double num : array) { | ||
total += num; | ||
} | ||
return total / array.length; | ||
} | ||
|
||
private static Double standardDeviation(double [] array) { | ||
if (array == null) { | ||
return null; | ||
} | ||
double mean = getMean(array); | ||
double sum = 0; | ||
for (double x : array) { | ||
sum += Math.pow(x - mean, 2); | ||
} | ||
double variance = sum / array.length; | ||
return Math.sqrt(variance); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
10 Days of Statistics/Day 7 - Spearman's Rank Correlation Coefficient/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,96 @@ | ||
// Author: Rodney Shaghoulian | ||
// Github: github.com/rshaghoulian | ||
// HackerRank: hackerrank.com/rshaghoulian | ||
|
||
import java.util.Scanner; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
/* O(n log n) runtime. O(n) space | ||
* | ||
* The challenging part is creating the rank arrays. | ||
*/ | ||
public class Solution { | ||
|
||
public static void main(String[] args) { | ||
/* Read and save input */ | ||
Scanner scan = new Scanner(System.in); | ||
int size = scan.nextInt(); | ||
double [] X = new double[size]; | ||
double [] Y = new double[size]; | ||
for (int i = 0; i < size; i++) { | ||
X[i] = scan.nextDouble(); | ||
} | ||
for (int i = 0; i < size; i++) { | ||
Y[i] = scan.nextDouble(); | ||
} | ||
|
||
System.out.format("%.3f", spearman(X, Y)); | ||
} | ||
|
||
/* Calculates Spearman's rank correlation coefficient, */ | ||
private static Double spearman(double [] X, double [] Y) { | ||
/* Error check */ | ||
if (X == null || Y == null || X.length != Y.length) { | ||
return null; | ||
} | ||
|
||
/* Create Rank arrays */ | ||
int [] rankX = getRanks(X); | ||
int [] rankY = getRanks(Y); | ||
|
||
/* Apply Spearman's formula */ | ||
int n = X.length; | ||
double numerator = 0; | ||
for (int i = 0; i < n; i++) { | ||
numerator += Math.pow((rankX[i] - rankY[i]), 2); | ||
} | ||
numerator *= 6; | ||
return 1 - numerator / (n * ((n * n) - 1)); | ||
} | ||
|
||
/* Returns a new array with ranks. Assumes unique array values. */ | ||
public static int[] getRanks(double [] array) { | ||
int n = array.length; | ||
|
||
/* Create Pair[] and sort by values */ | ||
Pair [] pair = new Pair[n]; | ||
for (int i = 0; i < n; i++) { | ||
pair[i] = new Pair(i, array[i]); | ||
} | ||
Arrays.sort(pair, new PairValueComparator()); | ||
|
||
/* Create and return ranks[] */ | ||
int [] ranks = new int[n]; | ||
int rank = 1; | ||
for (Pair p : pair) { | ||
ranks[p.index] = rank++; | ||
} | ||
return ranks; | ||
} | ||
|
||
/* A class to store 2 variables */ | ||
public static class Pair { | ||
public final int index; | ||
public final double value; | ||
|
||
public Pair(int i, double v) { | ||
index = i; | ||
value = v; | ||
} | ||
} | ||
|
||
/* This lets us sort Pairs based on their value field */ | ||
public static class PairValueComparator implements Comparator<Pair> { | ||
@Override | ||
public int compare(Pair p1, Pair p2) { | ||
if (p1.value < p2.value) { | ||
return -1; | ||
} else if (p1.value > p2.value) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.