Skip to content

Commit

Permalink
added 4 solutions, READMEs, MIT LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Feb 12, 2017
1 parent bcf11f2 commit 5ac5cfb
Show file tree
Hide file tree
Showing 92 changed files with 744 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;
import java.util.Arrays;
import java.util.HashMap;
Expand Down
4 changes: 4 additions & 0 deletions 10 Days of Statistics/Day 0 - Weighted Mean/Solution.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;
import java.util.Arrays;

Expand Down
4 changes: 4 additions & 0 deletions 10 Days of Statistics/Day 1 - Quartiles/Solution.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;
import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {
Expand Down
5 changes: 5 additions & 0 deletions 10 Days of Statistics/Day 2 - Basic Probability/Solution.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Author: Rodney Shaghoulian
Github: github.com/rshaghoulian
HackerRank: hackerrank.com/rshaghoulian


Answer: 5/6

There are 6 possibilities on each die. On 2 dice, there are 6 * 6 = 36 possibilities
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Author: Rodney Shaghoulian
Github: github.com/rshaghoulian
HackerRank: hackerrank.com/rshaghoulian


Answer: 17/42

Urn X has a 4/7 probability of giving a red ball
Expand Down
5 changes: 5 additions & 0 deletions 10 Days of Statistics/Day 2 - More Dice/Solution.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Author: Rodney Shaghoulian
Github: github.com/rshaghoulian
HackerRank: hackerrank.com/rshaghoulian


Answer: 1/9

There are 6 possibilities on each die. On 2 dice, there are 6 * 6 = 36 possibilities
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Author: Rodney Shaghoulian
Github: github.com/rshaghoulian
HackerRank: hackerrank.com/rshaghoulian


Answer: 12/51

There are 13 of each suit in a deck.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Author: Rodney Shaghoulian
Github: github.com/rshaghoulian
HackerRank: hackerrank.com/rshaghoulian


Answer: 1/3

****** 2 valid answers: 1/3 or 1/2
Expand Down
5 changes: 5 additions & 0 deletions 10 Days of Statistics/Day 3 - Drawing Marbles/Solution.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Author: Rodney Shaghoulian
Github: github.com/rshaghoulian
HackerRank: hackerrank.com/rshaghoulian


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 :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

/* Useful Formulas:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

/* This HackerRank problem has a buggy tutorial (formula is incorrect) and a buggy
solution (uses the incorrect formula) Hopefully they fix this issue soon. */
public class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {
public static void main(String[] args) {
/* Provided variables */
Expand Down
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);
}
}
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;
}
}
}
}
Loading

0 comments on commit 5ac5cfb

Please sign in to comment.