[jddaniels commit] r171 - in trunk/Facebook_puzzles: src/net/ddaniels/facebook/puzzles test/net/ddaniels/facebook/pu...

0 views
Skip to first unread message

codesite...@google.com

unread,
Feb 27, 2008, 2:47:51 AM2/27/08
to jddaniels-commit...@googlegroups.com
Author: daniels.douglas
Date: Tue Feb 26 23:47:35 2008
New Revision: 171

Modified:
trunk/Facebook_puzzles/src/net/ddaniels/facebook/puzzles/Matrix.java
trunk/Facebook_puzzles/test/net/ddaniels/facebook/puzzles/TestGridFlip.java

Log:
adding further tests and flip checks

Modified: trunk/Facebook_puzzles/src/net/ddaniels/facebook/puzzles/Matrix.java
==============================================================================
---
trunk/Facebook_puzzles/src/net/ddaniels/facebook/puzzles/Matrix.java (original)
+++
trunk/Facebook_puzzles/src/net/ddaniels/facebook/puzzles/Matrix.java
Tue Feb 26 23:47:35 2008
@@ -9,11 +9,42 @@
public class Matrix {

private int[][] matrix;
+
+ /**
+ * Constructs a 1xm matrix vector based on the int[] passed in.
+ * @param vector
+ */
+ public Matrix(int[] vector) {
+ //Temporarily construct a 1xm int[][] to hold the implicit vector
+ int[][] tmpData = new int[1][];
+ tmpData[0] = vector;
+ setMatrixData(tmpData);
+ }
+ public Matrix(int[][] matrixData){
+ setMatrixData(matrixData);
+ }
+
+ private void setMatrixData(int[][] matrixData) {
+ matrix = new int[matrixData.length][matrixData[0].length];
+ for(int i=0;i<this.getRowCount(); i++ ) {
+ for(int j=0; j<this.getColCount(); j++) {
+ this.set(i, j, matrixData[i][j]);
+ }
+ }
+ }

public Matrix(int n, int m) {
//Construct a blank n x m matrix with blank entries
matrix = new int[n][m];
}
+ public Matrix(Matrix matrix2) {
+ this(matrix2.getColCount(), matrix2.getRowCount());
+ for(int i=0;i<this.getRowCount(); i++ ) {
+ for(int j=0; j<this.getColCount(); j++) {
+ this.set(i, j, matrix2.get(i, j));
+ }
+ }
+ }
public int getColCount() {
//Number of column elements in the first row
return matrix[0].length;
@@ -34,11 +65,107 @@
public void set(int row, int col, int val) {
matrix[row][col] = val;
}
+ /**
+ * Flips the sign of each integer element in the column.
+ * @param col - an integer in (1..n) and changes the sign of all the
numbers in that column.
+ */
+ public void flip_col(int col) {
+ //Convert to 0 base array indexing
+ col -= 1;
+ for(int i=0;i<this.getRowCount(); i++ ) {
+ int flippedVal = -this.get(i,col);
+ this.set(i, col, flippedVal);
+ }
+ }
+ /**
+ * Flips the sign of each integer element in the row.
+ * @param row
+ */
+ public void flip_row(int row) {
+ for(int i=0;i<this.getColCount(); i++ ) {
+ int flippedVal = -this.get(row,i);
+ this.set(row, i, flippedVal);
+ }
+ }
+ /**
+ * Adds the elements of one matrix to another.
+ * @param matrix
+ * @return
+ */
+ public Matrix add(Matrix matrix) {
+ Matrix m = new Matrix(this);
+ for(int i=0; i<m.getRowCount(); i++) {
+ for(int j=0; j<m.getColCount(); j++) {
+ m.set(i, j, m.get(i, j) + matrix.get(i, j));
+ }
+ }
+
+ return m;
+ }
+ /**
+ * Sum of columns
+ * @return n element Vector representing the sum of the n columns
+ */
+ public Matrix SC() {
+ //Sum of each column 1xm vector matrix
+ Matrix sc = new Matrix(1, matrix[0].length);
+ for(int i=0; i<matrix[0].length; i++) {
+ for(int j=0; j<matrix.length; j++) {
+ //Sum of column i for each row
+ int colSum = sc.get(0,i) + this.get(j,i);
+ sc.set(0,i,colSum);
+ }
+ }
+ return sc;
+ }
+
+ /**
+ * Sum of rows
+ * @return m element Vector representing the sum of the m rows
+ */
+ public Matrix SR() {
+ //Sum of each column 1xm vector matrix
+ Matrix sr = new Matrix(1, matrix.length);
+ for(int i=0; i<matrix.length; i++) {
+ for(int j=0; j<matrix[0].length; j++) {
+ int elem = this.get(i, j);
+ //Sum of column i for each row
+ int rowSum = sr.get(0,i) + elem;
+ sr.set(0,i,rowSum);
+ }
+ }
+ return sr;
+ }

+ /**
+ * Sum of all the elements in the matrix.
+ * @return
+ */
+ public int sumOfElements() {
+ int sum = 0;
+ for(int i=0; i<this.getRowCount(); i++) {
+ for(int j=0; j<this.getColCount(); j++) {
+ sum += this.get(i, j);
+ }
+ }
+ return sum;
+ }
+ /**
+ * Sum of SC(sum of each column) + SR(sum of each row)
+ * @return
+ */
+ public int S() {
+ Matrix sr = SR();
+ Matrix sc = SC();
+ int srSum = sr.sumOfElements();
+ int scSum = sc.sumOfElements();
+
+ return scSum + srSum;
+ }
public String toString() {
StringBuffer buff = new StringBuffer();
for(int i=0; i<matrix.length; i++) {
- for(int j=0; j<matrix[i].length; j++) {
+ for(int j=0; j<matrix[0].length; j++) {
buff.append(matrix[i][j] + " ");
}
buff.append("\n");
@@ -86,5 +213,48 @@
}

return matrix;
+ }
+ /**
+ * Checks if this Matrix contains any negative elements.
+ * @return
+ */
+ public boolean containsNegative() {
+ for(int i=0; i<this.getRowCount(); i++) {
+ for(int j=0; j<this.getColCount(); j++) {
+ if(this.get(i,j)<0) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+ @Override
+ public boolean equals(Object o) {
+ boolean isEqual = false;
+ if(o == this) {
+ isEqual = true;
+ }
+ else if(o instanceof Matrix) {
+ Matrix m = (Matrix)o;
+
+ if(m.getRowCount()==this.getRowCount() &&
m.getColCount()==this.getColCount()) {
+ //assume we're equal and check every element
+ isEqual = true;
+ for(int i=0; i<this.getRowCount(); i++) {
+ for(int j=0; j<this.getColCount(); j++) {
+ int thisVal = this.get(i,j);
+ int mVal = m.get(i,j);
+ if(thisVal!=mVal) {
+ isEqual = false;
+ //Exit the loop we've already found an unequal value
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ return isEqual;
+
}
}

Modified: trunk/Facebook_puzzles/test/net/ddaniels/facebook/puzzles/TestGridFlip.java
==============================================================================
---
trunk/Facebook_puzzles/test/net/ddaniels/facebook/puzzles/TestGridFlip.java (original)
+++
trunk/Facebook_puzzles/test/net/ddaniels/facebook/puzzles/TestGridFlip.java
Tue Feb 26 23:47:35 2008
@@ -1,7 +1,7 @@
package net.ddaniels.facebook.puzzles;

+import static org.junit.Assert.*;
import java.util.logging.Logger;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -27,5 +27,75 @@
public void test5x5MatrixFromFile() {
Matrix m = Matrix.fromFile("data/5x5.txt");
logger.info("5x5 file output: (n x m)==("+m.getRowCount()+"
x "+m.getColCount()+")\n" + m.toString());
+ }
+
+ @Test
+ public void test5x5SumOfRows() {
+ Matrix m = Matrix.fromFile("data/5x5.txt");
+ int[] expectedSrData = {0, -9, -9, -4, 9};
+ Matrix expectedSr = new Matrix(expectedSrData);
+ Matrix sr = m.SR();
+ assertEquals(expectedSr, sr);
+ }
+
+ @Test
+ public void test5x5SumOfCols() {
+ Matrix m = Matrix.fromFile("data/5x5.txt");
+ int[] expectedScData = {17, -4, -10, -9, -7};
+ Matrix expectedSc = new Matrix(expectedScData);
+ Matrix sc = m.SC();
+ assertEquals(expectedSc, sc);
+ }
+
+ @Test
+ public void test5x5Sum() {
+ Matrix m = Matrix.fromFile("data/5x5.txt");
+ int expectedSum = -26;
+ int sum = m.S();
+ assertEquals(expectedSum, sum);
+ }
+
+ @Test
+ public void test5x5FlipCol2SumOfCols() {
+ Matrix m = Matrix.fromFile("data/5x5.txt");
+ m.flip_col(2);
+
+ int[] expectedScData = {17, 4, -10, -9, -7};
+ Matrix expectedSc = new Matrix(expectedScData);
+ Matrix sc = m.SC();
+
+ int[] expectedSrData = {-2, 1, 7, -12, 1};
+ Matrix expectedSr = new Matrix(expectedSrData);
+ Matrix sr = m.SR();
+
+ int expectedSum = -10;
+ int sum = m.S();
+
+ assertEquals(expectedSc, sc);
+ assertEquals(expectedSr, sr);
+ assertEquals(expectedSum, sum);
+ }
+
+ @Test
+ public void test5x5Negative() {
+ Matrix m = Matrix.fromFile("data/5x5.txt");
+ m.flip_col(2);
+ m.flip_col(3);
+ m.flip_col(4);
+ m.flip_col(5);
+
+ int[] expectedScData = {17, 4, 10, 9, 7};
+ Matrix expectedSc = new Matrix(expectedScData);
+ Matrix sc = m.SC();
+
+
+ assertEquals(expectedSc, sc);
+
+ }
+
+ @Test
+ public void testMinPositiveSum() {
+ int[] elements = {1, 3, 3, 4, 6};
+
}
}

Reply all
Reply to author
Forward
0 new messages