Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 67 additions & 7 deletions src/main/java/ca/ciccc/Assignment2.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,22 @@ public class Assignment2 {
*/
public String numberLoops(int n) {
// Todo: your code goes here
String lines = "";

return null;
for(int i = 1; i <= n; ++i){
String line = "";
for (int j = 0; j < n - i; ++j){
line += ".";
}
for (int j = 0; j < i; ++j) {
line += String.format("%d", i);
}
lines += line;
if (i < n) {
lines += "\n";
}
}
return lines;
}

/**
Expand All @@ -34,8 +48,17 @@ public String numberLoops(int n) {
*/
public boolean countNumbers(int[] nums) {
// Todo: your code goes here

return false;
int numberOfOnes = 0;
int numberOfSevens = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 1) {
numberOfOnes += 1;
}
if (nums[i] == 7) {
numberOfSevens += 1;
}
}
return numberOfOnes > numberOfSevens;
}

/**
Expand All @@ -52,8 +75,17 @@ public boolean countNumbers(int[] nums) {
*/
public int sumExcept13(int[] nums) {
// Todo: your code goes here
int total = 0;

for(int i = 0; i < nums.length; ++i) {
if (nums[i] == 13) {
++i;
continue;
}
total += nums[i];
}

return 0;
return total;
}

/**
Expand All @@ -69,8 +101,16 @@ public int sumExcept13(int[] nums) {
*/
public int[] shiftArray(int[] nums) {
// Todo: your code goes here
int[] shifted = new int[nums.length];

return null;
for (int i = 0; i < nums.length; ++i) {
if (i == 0) {
shifted[shifted.length -1] = nums[i];
} else {
shifted[i - 1] = nums[i];
}
}
return shifted;
}

/**
Expand All @@ -80,7 +120,18 @@ public int[] shiftArray(int[] nums) {
*/
public boolean tripleIncreasingOrder(int[] nums) {
// Todo: your code goes here

int increasingStreak = 1;
int lastNumber = nums[0];
for (int i = 1; i < nums.length; ++i){
if (nums[i] == lastNumber + 1 ) {
increasingStreak += 1;
} else { increasingStreak = 1;
}
lastNumber = nums[i];
if (increasingStreak == 3) {
return true;
}
}
return false;
}

Expand All @@ -91,8 +142,17 @@ public boolean tripleIncreasingOrder(int[] nums) {
*/
public boolean evenOrOdd(int[] nums){
// Todo: your code goes here
int evens = 0;
int odds = 0;

return false;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] % 2 == 0) {
evens += 1;
} else {
odds += 1;
}
}
return evens == odds;
}
}

59 changes: 47 additions & 12 deletions src/main/java/ca/ciccc/MagicSquare.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package ca.ciccc;

/**
*
* Magic Squares are square arrays of numbers that have the interesting property that
* the numbers in each column, and in each row, all add up to the same total.
*
* <p>
* Given an n x n square of numbers, determine if it is magic square.
*
* <p>
* Reference: {@link java.lang.Integer}
* Integer is a wrapper class for {@code int} primitive type.
*/
Expand All @@ -18,6 +17,7 @@ public class MagicSquare {

/**
* Constructor that takes n as the number of rows and cols.
*
* @param n number of rows and cols.
*/
public MagicSquare(int n) {
Expand All @@ -28,7 +28,8 @@ public MagicSquare(int n) {
/**
* Constructor that takes n as the number of rows and cols.
* Initializes square 2-Dimensional array.
* @param n number of rows and cols.
*
* @param n number of rows and cols.
* @param square
*/
public MagicSquare(int n, Integer[][] square) {
Expand All @@ -38,6 +39,7 @@ public MagicSquare(int n, Integer[][] square) {

/**
* Getter for square
*
* @return {@code this.square}
*/
public Integer[][] getSquare() {
Expand All @@ -46,6 +48,7 @@ public Integer[][] getSquare() {

/**
* Setter for square.
*
* @param square
*/
public void setSquare(Integer[][] square) {
Expand All @@ -54,29 +57,61 @@ public void setSquare(Integer[][] square) {

/**
* Check if {@code square} is magic square or not. (static helper method)
*
* @param square 2-Dimensional array
* @return {@code true} if {@code square} is magic, otherwise {@code false}
*
* <p>
* Sample Input
* {
* {16, 3, 2, 13},
* {5, 10, 11, 8},
* {9, 6, 7, 12},
* {4, 15, 14, 1}
* {16, 3, 2, 13},
* {5, 10, 11, 8},
* {9, 6, 7, 12},
* {4, 15, 14, 1}
* }
*
* <p>
* Sample Output
* true
*
* <p>
* Explanation: The sums of each row and col are equal.
*/
public static boolean isMagicSquare(Integer[][] square) {
// TODO: You need to implement this method.
return false;
int totalSupposedToBe = 0;
int squareHeight = square.length;
for (int c = 0; c < squareHeight; ++c) {
int firstRowTotal = 0;

for (int r = 0; r < squareHeight; ++r) {

firstRowTotal += square[r][c];
}
totalSupposedToBe = firstRowTotal;
}
for (int r = 1; r < squareHeight; ++r) {
int rowTotal = 0;
for (int c = 0; c < squareHeight; ++c) {
rowTotal += square[r][c];
}

if (rowTotal != totalSupposedToBe) {
return false;
}
}
for (int c = 0; c < squareHeight; ++c) {
int columnTotal = 0;
for (int r = 0; r < squareHeight; ++r) {
columnTotal += square[r][c];
}
if (columnTotal != totalSupposedToBe) {
return false;
}
}
return true;
}

/**
* Check if {@code this.square} is magic square or not.
*
* @return {@code true} if {@code square} is magic, otherwise {@code false}
*/
public boolean isMagicSquare() {
Expand Down