Skip to content

Commit

Permalink
RandomProblem002.java matrix multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhargulati committed Feb 9, 2016
1 parent 6dc4bc7 commit aa721a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
Expand Up @@ -3,7 +3,7 @@
/**
* Given a text and a string pattern check if pattern exist in the text.
*/
public class RandomProblem02 {
public class RandomProblem002 {

public static boolean patternExistInText(String text, String pattern) {
/*
Expand Down
@@ -0,0 +1,37 @@
package com.shekhargulati.ninetynine_problems.java8._00_random;

/**
* Perform matrix multiplication of two arrays.
* Matrix multiplication of array A of size nxm and array B of size mxp is an array of size nxp
*/
public class RandomProblem003 {

public static int[][] matrixMultiplication(int[][] a, int[][] b) {
/*
1. Iterate over all the rows of A
2. For each row of A
2.1 iterate over all the columns of A and multiply them with each column of B
*/
int rowsA = a.length;
int colsA = a[0].length;
int rowsB = b.length;
int colsB = b[0].length;


if (colsA != rowsB) {
throw new IllegalArgumentException("Can't perform matrix calculation on matrix with given sizes");
}
int[][] result = new int[rowsA][colsB];

for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
int fValue = 0;
for (int k = 0; k < colsA; k++) {
fValue += a[i][k] * b[k][j];
}
result[i][j] = fValue;
}
}
return result;
}
}
Expand Up @@ -9,19 +9,19 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class RandomProblem02Test {
public class RandomProblem002Test {

@Test
public void shouldGiveTrueWhenPatternExistInText() throws Exception {
String text = Files.lines(Paths.get("src", "test", "resources", "book.txt")).collect(joining());
boolean patternExists = RandomProblem02.patternExistInText(text, "prudent");
boolean patternExists = RandomProblem002.patternExistInText(text, "prudent");
assertTrue(patternExists);
}

@Test
public void shouldGiveFalseWhenPatternDoesNotExistInText() throws Exception {
String text = Files.lines(Paths.get("src", "test", "resources", "book.txt")).collect(joining());
boolean patternExists = RandomProblem02.patternExistInText(text, "awesome");
boolean patternExists = RandomProblem002.patternExistInText(text, "awesome");
assertFalse(patternExists);
}
}
@@ -0,0 +1,31 @@
package com.shekhargulati.ninetynine_problems.java8._00_random;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class RandomProblem003Test {

@Test
public void performMatrixMultiplicationOfTwoArrays() throws Exception {
int[][] a = {
{1, 2, 3},
{4, 5, 6}
};

int[][] b = {
{7, 8},
{9, 10},
{11, 12}
};

int[][] result = {
{58, 64},
{139, 154}
};

int[][] c = RandomProblem003.matrixMultiplication(a, b);
assertThat(c, equalTo(result));
}
}

0 comments on commit aa721a7

Please sign in to comment.