Skip to content

Instantly share code, notes, and snippets.

@mattcunningham
Last active January 12, 2017 18:03
Show Gist options
  • Save mattcunningham/c1c97afcf9d03ff0d705a4ee83bfb60f to your computer and use it in GitHub Desktop.
Save mattcunningham/c1c97afcf9d03ff0d705a4ee83bfb60f to your computer and use it in GitHub Desktop.
public class ArrayChecker {
private int[][] testNums;
public ArrayChecker(int[][] testNums) {
this.testNums = testNums;
}
public int arrayInstance(int instance) {
int instances = 0;
for (int[] testNum : testNums) {
for (int num : testNum) {
if (num == instance) {
instances++;
}
}
}
return instances;
}
public int arrayDivisor(int divisor) {
int instances = 0;
for (int[] testNum : testNums) {
for (int num : testNum) {
if ((num % divisor) == 0) {
instances++;
}
}
}
return instances;
}
}
public class ArrayWords {
public String getUpDown(String[][] lettersArray, int rowStart, int rowEnd, int colNumber) {
String ret = "";
for (int i = rowStart; i <= rowEnd; i++) {
ret += lettersArray[i][colNumber];
}
return ret;
}
public String getLeftRight(String[][] lettersArray, int colStart, int colEnd, int rowNumber) {
String ret = "";
for (int i = colStart; i <= colEnd; i++) {
ret += lettersArray[rowNumber][i];
}
return ret;
}
public static void main(String args[]) {
ArrayWords wordsTest = new ArrayWords();
String[][] testLetters = {{"z", "w", "r"},
{"a", "a", "h"},
{"f", "t", "e"}};
String selectedWord = "";
selectedWord = wordsTest.getUpDown(testLetters, 0, 2, 1);
if (selectedWord.equals("wat")) {
System.out.println("Part A correct.");
}
else {
System.out.println("Part A incorrect.");
System.out.println("Expected: \"wat\". Your output was \"" +
selectedWord + "\"");
}
selectedWord = wordsTest.getLeftRight(testLetters, 1, 2, 1);
if (selectedWord.equals("ah")) {
System.out.println("Part B correct.");
}
else {
System.out.println("Part B incorrect.");
System.out.println("Expected: \"ah\". Your output was \"" +
selectedWord + "\"");
}
}
}
public class DiceGame {
private int[][] playerRolls;
public DiceGame(int[][] initRolls) {
playerRolls = initRolls;
}
public int getPlayerTotal(int playerNum) {
int total = 0;
for (int score : this.playerRolls[playerNum]) {
total += score;
}
return total;
}
public int getMaxPlayer() {
int max = 0;
int maxPlayer = 0;
for (int player = 0; player < playerRolls.length; player++) {
int playerTotal = getPlayerTotal(player);
if (playerTotal > max) {
max = playerTotal;
maxPlayer = player;
}
}
return maxPlayer;
}
}
public class FillerUp {
double[][] numberArray;
public FillerUp(double[][] numberArray2D) {
this.numberArray = numberArray2D;
}
public void printArray() {
for (double[] numberArray1D : numberArray) {
for (double number : numberArray1D) {
System.out.print(number + " ");
}
}
}
public void printArrayBackward() {
for (int i = numberArray.length - 1; i > -1; i--) {
for (int j = numberArray[i].length - 1; j > -1; j--) {
System.out.print(numberArray[i][j] + " ");
}
}
}
}
public class MapReader {
private int[][] map;
public MapReader(int[][] heightMap ) {
this.map = heightMap;
}
public int[] findPeak() {
int[] ret = new int[2];
int max = 0;
for (int x = 0; x < this.map.length; x++) {
for (int y = 0; y < this.map[x].length; y++) {
if (this.map[x][y] > max) {
ret = new int[]{x, y};
max = this.map[x][y];
}
}
}
return ret;
}
public double distanceAB(int aX, int aY, int bX, int bY) {
return Math.sqrt(Math.pow(bX-aX, 2) + Math.pow(bY-aY, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment