Skip to content

Commit

Permalink
Added 20 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Feb 17, 2017
1 parent 5ac5cfb commit 34e6656
Show file tree
Hide file tree
Showing 26 changed files with 859 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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)
There are 6 cases where sum >= 10: (4,6), (5,5), (5,6), (6,4), (6,5), (6,6)

This gives us probability(sum >= 10) = 6/36 = 1/6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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 {

public static void main(String[] args) {
Expand All @@ -12,13 +10,9 @@ public static void main(String[] args) {
double maxWeight = 9800;
int n = 49;

double samplesMean = mean * n;

/* this INCORRECT formula (based off HackerRank tutorial) passes HackerRank tests */
double samplesSTD = std * Math.sqrt(n);

/* this is the correct formula (which currently wont pass HackerRank tests) */
// double samplesSTD = std / Math.sqrt(n);
/* Formulas are from problem's tutorial */
double samplesMean = n * mean;
double samplesSTD = Math.sqrt(n) * std;

System.out.format("%.4f", cumulative(samplesMean, samplesSTD, maxWeight));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ public static void main(String[] args) {
double mean = 2.4;
double std = 2;

double samplesMean = mean * n;
/* Formulas are from problem's tutorial */
double samplesMean = n * mean;
double samplesSTD = Math.sqrt(n) * std;

/* this INCORRECT formula (based off HackerRank tutorial) passes HackerRank tests */
double samplesSTD = std * Math.sqrt(n);

/* this is the correct formula (which currently wont pass HackerRank tests) */
// double samplesSTD = std / Math.sqrt(n);

System.out.format("%.4f", cumulative(samplesMean, samplesSTD, ticketsLeft));
}

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

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

public class Solution {
public static void main(String[] args) {
/* Read input: Create and fill X,Y arrays */
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
double [][] X = new double[n][m + 1];
double [][] Y = new double[n][1];
for (int row = 0; row < n; row++) {
X[row][0] = 1;
for (int col = 1; col <= m; col++) {
X[row][col] = scan.nextDouble();
}
Y[row][0] = scan.nextDouble();
}

/* Calculate B */
double [][] xtx = multiply(transpose(X),X);
double [][] xtxInv = invert(xtx);
double [][] xty = multiply(transpose(X), Y);
double [][] B = multiply(xtxInv, xty);

int sizeB = B.length;

/* Calculate and print values for the "q" feature sets */
int q = scan.nextInt();
for (int i = 0; i < q; i++) {
double result = B[0][0];
for (int row = 1; row < sizeB; row++) {
result += scan.nextDouble() * B[row][0];
}
System.out.println(result);
}
}

/* Multiplies 2 matrices in O(n^3) time */
public static double[][] multiply(double [][] A, double [][] B) {
int aRows = A.length;
int aCols = A[0].length;
int bRows = B.length;
int bCols = B[0].length;

double [][] C = new double[aRows][bCols];
int cRows = C.length;
int cCols = C[0].length;

for (int row = 0; row < cRows; row++) {
for (int col = 0; col < cCols; col++) {
for (int k = 0; k < aCols; k++) {
C[row][col] += A[row][k] * B[k][col];
}
}
}
return C;
}

public static double[][] transpose(double [][] matrix) {
/* Create new array with switched dimensions */
int originalRows = matrix.length;
int originalCols = matrix[0].length;
int rows = originalCols;
int cols = originalRows;
double [][] result = new double[rows][cols];

/* Fill our new 2D array */
for (int row = 0; row < originalRows; row++) {
for (int col = 0; col < originalCols; col++) {
result[col][row] = matrix[row][col];
}
}
return result;
}

/******************************************************************/
/* Matrix Inversion code (the 2 functions below) are from: */
/* http://www.sanfoundry.com/java-program-find-inverse-matrix/ */
/******************************************************************/

public static double[][] invert(double a[][])
{
int n = a.length;
double x[][] = new double[n][n];
double b[][] = new double[n][n];
int index[] = new int[n];
for (int i=0; i<n; ++i)
b[i][i] = 1;

// Transform the matrix into an upper triangle
gaussian(a, index);

// Update the matrix b[i][j] with the ratios stored
for (int i=0; i<n-1; ++i)
for (int j=i+1; j<n; ++j)
for (int k=0; k<n; ++k)
b[index[j]][k]
-= a[index[j]][i]*b[index[i]][k];

// Perform backward substitutions
for (int i=0; i<n; ++i)
{
x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];
for (int j=n-2; j>=0; --j)
{
x[j][i] = b[index[j]][i];
for (int k=j+1; k<n; ++k)
{
x[j][i] -= a[index[j]][k]*x[k][i];
}
x[j][i] /= a[index[j]][j];
}
}
return x;
}

// Method to carry out the partial-pivoting Gaussian
// elimination. Here index[] stores pivoting order.

public static void gaussian(double a[][], int index[])
{
int n = index.length;
double c[] = new double[n];

// Initialize the index
for (int i=0; i<n; ++i)
index[i] = i;

// Find the rescaling factors, one from each row
for (int i=0; i<n; ++i)
{
double c1 = 0;
for (int j=0; j<n; ++j)
{
double c0 = Math.abs(a[i][j]);
if (c0 > c1) c1 = c0;
}
c[i] = c1;
}

// Search the pivoting element from each column
int k = 0;
for (int j=0; j<n-1; ++j)
{
double pi1 = 0;
for (int i=j; i<n; ++i)
{
double pi0 = Math.abs(a[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1)
{
pi1 = pi0;
k = i;
}
}

// Interchange rows according to the pivoting order
int itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i=j+1; i<n; ++i)
{
double pj = a[index[i]][j]/a[index[j]][j];

// Record pivoting ratios below the diagonal
a[index[i]][j] = pj;

// Modify other elements accordingly
for (int l=j+1; l<n; ++l)
a[index[i]][l] -= pj*a[index[j]][l];
}
}
}
}
29 changes: 29 additions & 0 deletions 30 Days of Code/Day 10 - Binary Numbers/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
System.out.println(consecutiveOnes(n));
}

public static int consecutiveOnes(int n) {
int count = 0;
int max = 0;
while (n > 0) {
if (n % 2 == 1) {
count++;
max = Math.max(max, count);
} else {
count = 0;
}
n = n >> 1;
}
return max;
}
}
39 changes: 39 additions & 0 deletions 30 Days of Code/Day 11 - 2D Arrays/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {

public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int arr[][] = new int[6][6];
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 6; col++) {
arr[row][col] = scan.nextInt();
}
}
scan.close();

System.out.println(maxHourglass(arr));
}

public static int maxHourglass(int [][] arr) {
int max = Integer.MIN_VALUE;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
int sum = findSum(arr, row, col);
max = Math.max(max, sum);
}
}
return max;
}

private static int findSum(int [][] arr, int r, int c) {
int sum = arr[r+0][c+0] + arr[r+0][c+1] + arr[r+0][c+2]
+ arr[r+1][c+1] +
arr[r+2][c+0] + arr[r+2][c+1] + arr[r+2][c+2];
return sum;
}
}
51 changes: 51 additions & 0 deletions 30 Days of Code/Day 12 - Inheritance/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

class Student extends Person{
private int[] testScores;

/*
* Class Constructor
*
* @param firstName - A string denoting the Person's first name.
* @param lastName - A string denoting the Person's last name.
* @param id - An integer denoting the Person's ID number.
* @param scores - An array of integers denoting the Person's test scores.
*/
public Student(String firstName, String lastName, int id, int [] scores) {
super(firstName, lastName, id);
testScores = scores;
}

/*
* Method Name: calculate
* @return A character denoting the grade.
*/
public char calculate() {
double average = 0;
for (int score : testScores) {
average += score;
}
average /= testScores.length;

if (average >= 90) {
return 'O';
}
else if (average >= 80) {
return 'E';
}
else if (average >= 70) {
return 'A';
}
else if (average >= 55) {
return 'P';
}
else if (average >= 40) {
return 'D';
}
else {
return 'T';
}
}
}
18 changes: 18 additions & 0 deletions 30 Days of Code/Day 13 - Abstract Classes/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

class MyBook extends Book {
int price;

MyBook(String title, String author, int price) {
super(title, author);
this.price = price;
}

void display() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: " + price);
}
}
24 changes: 24 additions & 0 deletions 30 Days of Code/Day 14 - Scope/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

class Difference {
private int[] elements;
public int maximumDifference;

Difference (int [] elements) {
this.elements = elements;
}

// O(n) runtime. O(1) space
void computeDifference() {
maximumDifference = 0;
int min = elements[0];
int max = elements[0];
for (int num : elements) {
min = Math.min(min, num);
max = Math.max(max, num);
}
maximumDifference = max - min;
}
} // End of Difference class
Loading

0 comments on commit 34e6656

Please sign in to comment.