Skip to content

Commit

Permalink
Refactored all solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Apr 5, 2017
1 parent 5f4c2f4 commit decc8e7
Show file tree
Hide file tree
Showing 135 changed files with 847 additions and 848 deletions.
27 changes: 6 additions & 21 deletions 10 Days of Statistics/Day 0 - Mean, Median, and Mode/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import java.util.Arrays;
import java.util.HashMap;

/* Runtime: O(n log n) due to sorting */
// Time Complexity: O(n log n) due to sorting
public class Solution {

public static void main(String[] args) {

/* Read input: Create and fill array */
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();

int [] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scan.nextInt();
Expand All @@ -39,26 +36,14 @@ public static void main(String[] args) {
median = array[size / 2];
}

/* Calculate Mode */
/* Calculate Mode - if there's a tie, choose the smaller number */
HashMap<Integer, Integer> map = new HashMap<>();
int maxOccurrences = 0;
int mode = Integer.MAX_VALUE;

for (int num : array) {
/* Update occurrences for this number */
int occurrences;
if (map.containsKey(num)) {
occurrences = map.get(num) + 1;
} else {
occurrences = 1;
}
map.put(num, occurrences);

/* Update our mode. If there's a tie, choose the smaller number */
if (occurrences > maxOccurrences) {
maxOccurrences = occurrences;
mode = num;
} else if (occurrences == maxOccurrences && num < mode) { // this does the tie-breaking
map.merge(num, 1, Integer::sum);
int occurrences = map.get(num);
if (occurrences > maxOccurrences || (occurrences == maxOccurrences && num < mode)) {
maxOccurrences = occurrences;
mode = num;
}
Expand Down
5 changes: 1 addition & 4 deletions 10 Days of Statistics/Day 0 - Weighted Mean/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {

/* Read and save data */
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] elements = new int[size];
Expand All @@ -28,7 +26,6 @@ public static void main(String[] args) {
total += elements[i] * weights[i];
totalWeights += weights[i];
}

double weightedMean = (double) total / totalWeights;
System.out.format("%.1f", weightedMean);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import java.util.Arrays;

public class Solution {

public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] element = new int[size];
int [] frequency = new int[size];
for (int i = 0; i < size; i++) {
element[i] = scan.nextInt();
}

int numElements = 0;
for (int i = 0; i < size; i++) {
frequency[i] = scan.nextInt();
Expand All @@ -35,14 +33,13 @@ public static void main(String[] args) {
}
Arrays.sort(data);

/* Works with both even and odd length arrays */
/* Calculate interquartile range */
double q1 = findMedian(data, 0, data.length / 2 - 1);
double q3 = findMedian(data, (data.length + 1) / 2, data.length - 1);

System.out.println(q3 - q1);
}

/* Treats elements from "start" to "end" as an array and calculates its median */
/* Treats elements from "start" to "end" (inclusive) as an array and calculates its median */
private static double findMedian(int [] array, int start, int end) {
if ((end - start) % 2 == 0) { // odd number of elements
return (array[(end + start) / 2]);
Expand Down
25 changes: 11 additions & 14 deletions 10 Days of Statistics/Day 1 - Quartiles/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,19 @@
import java.util.Arrays;

public class Solution {

public static void main(String[] args) {
int [] array = getValues();
Arrays.sort(array);

/* Works with both even and odd length arrays */
int q1 = findMedian(array, 0, array.length / 2 - 1);
int q2 = findMedian(array, 0, array.length - 1);
int q3 = findMedian(array, (array.length + 1) / 2, array.length - 1);

/* Print output */
System.out.println(q1);
System.out.println(q2);
System.out.println(q3);
}

/* Treats elements from "start" to "end" as an array and calculates its median */
private static int findMedian(int [] array, int start, int end) {
if ((end - start) % 2 == 0) { // odd number of elements
return (array[(end + start) / 2]);
} else { // even number of elements
int value1 = array[(end + start) / 2];
int value2 = array[(end + start) / 2 + 1];
return Math.round((value1 + value2) / 2);
}
}

/* Creates array from input */
private static int [] getValues() {
Scanner scan = new Scanner(System.in);
Expand All @@ -44,4 +30,15 @@ private static int findMedian(int [] array, int start, int end) {
scan.close();
return array;
}

/* Treats elements from "start" to "end" (inclusive) as an array and calculates its median */
private static int findMedian(int [] array, int start, int end) {
if ((end - start) % 2 == 0) { // odd number of elements
return (array[(end + start) / 2]);
} else { // even number of elements
int value1 = array[(end + start) / 2];
int value2 = array[(end + start) / 2 + 1];
return Math.round((value1 + value2) / 2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] array = new int[size];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
double ratio = 1.09;

double p = ratio / (1 + ratio);
int n = 6;
double ratio = 1.09; // hardcoded value
double p = ratio / (1 + ratio);
int n = 6;

/* Calculate result */
double result = 0;
Expand All @@ -32,7 +30,7 @@ private static Long combinations(int n, int x) {
return factorial(n) / (factorial(x) * factorial(n - x));
}

private static Long factorial (int n) {
private static Long factorial(int n) {
if (n < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
/* Values given in problem statement */
double p = 0.12;
Expand Down Expand Up @@ -35,7 +34,7 @@ private static Long combinations(int n, int x) {
return factorial(n) / (factorial(x) * factorial(n - x));
}

private static Long factorial (int n) {
private static Long factorial(int n) {
if (n < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
int numerator = scan.nextInt();
int denominator = scan.nextInt();
Expand All @@ -30,7 +29,7 @@ private static Long combinations(int n, int x) {
return factorial(n) / (factorial(x) * factorial(n - x));
}

private static Long factorial (int n) {
private static Long factorial (int n){
if (n < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
int numerator = scan.nextInt();
int denominator = scan.nextInt();
Expand All @@ -34,7 +33,7 @@ private static Long combinations(int n, int x) {
return factorial(n) / (factorial(x) * factorial(n - x));
}

private static Long factorial (int n) {
private static Long factorial(int n) {
if (n < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
double mean = 20;
double std = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
double mean = 70;
double std = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
double lambda = scan.nextDouble();
int k = scan.nextInt();
Expand All @@ -20,7 +19,7 @@ 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) {
private static Long factorial(int n) {
if (n < 0) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
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 */
/* Save input */
Scanner scan = new Scanner(System.in);
double A = scan.nextDouble();
double B = scan.nextDouble();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
double mean = 205;
double std = 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {

public static void main(String[] args) {
double ticketsLeft = 250;
int n = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
double [] xs = new double[size];
Expand All @@ -18,10 +17,11 @@ public static void main(String[] args) {
for (int i = 0; i < size; i++) {
ys[i] = scan.nextDouble();
}
scan.close();

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) {
Expand Down Expand Up @@ -62,3 +62,4 @@ private static Double standardDeviation(double [] array) {
return Math.sqrt(variance);
}
}

Loading

0 comments on commit decc8e7

Please sign in to comment.