Last active
May 12, 2018 23:54
-
-
Save TigerHix/aaea70a0f1aec5574a412e1da182db41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Question 1 | |
public class Digits { | |
private ArrayList<Integer> digitList; | |
// Alternative method: parse num as String, use charAt and Integer.parseInt to extract the digits | |
public Digits(int num) { | |
digitList = new ArrayList<Integer>(); | |
if (num == 0) digitList.add(0); | |
while (num > 0) { | |
digitList.add(0, num % 10); | |
num /= 10; | |
} | |
} | |
// Alternative method: use for loop indices for comparison | |
public boolean isStrictlyIncreasing() { | |
int lastDigit = -1; | |
for (int digit : digitList) { | |
if (digit <= lastDigit) return false; | |
lastDigit = digit; | |
} | |
return true; | |
} | |
} | |
// Question 2 | |
public class MultPractice implements StudyPractice { | |
private int c; | |
private int x; | |
public MultPractice(int c, int x) { | |
this.c = c; | |
this.x = x; | |
} | |
public String getProblem() { | |
return c + " TIMES " + x; | |
} | |
public void nextProblem() { | |
x++; | |
} | |
} | |
// Question 3 | |
public class Phrase { | |
private String currentPhrase; | |
public Phrase(String p) { | |
currentPhrase = p; | |
} | |
public int findNthOccurrence(String str, int n) { | |
/* Implementation not shown */ | |
} | |
public void replaceNthOccurrence(String str, int n, String repl) { | |
int index = findNthOccurrence(str, n); | |
if (index == -1) return; | |
currentPhrase = currentPhrase.substring(0, index) + repl + currentPhrase.substring(index + str.length()); | |
} | |
// Alternative method (-1 mark penalty due to findNthOccurrence not being utilized): | |
// start from the end of string, loop backwards, substring in each loop | |
public int findLastOccurrence(String str) { | |
int n = 0, lastIndex = -1; | |
while (true) { | |
n++; | |
int index = findNthOccurrence(str, n); | |
if (index == -1) break; | |
lastIndex = index; | |
} | |
return lastIndex; | |
} | |
public String toString() { | |
return currentPhrase; | |
} | |
} | |
// Question 4 | |
public class Successors { | |
public static Position findPosition(int num, int[][] intArr) { | |
for (int r = 0; r < intArr.length; r++) { | |
for (int c = 0; c < intArr[0].length; c++) { | |
if (intArr[r][c] == num) return new Position(r, c); | |
} | |
} | |
return null; | |
} | |
public static Position[][] getSuccessorArray(int[][] intArr) { | |
Position[][] successorArr = new Position[intArr.length][intArr[0].length]; | |
for (int r = 0; r < intArr.length; r++) { | |
for (int c = 0; c < intArr[0].length; c++) { | |
successorArr[r][c] = findPosition(intArr[r][c] + 1, intArr); | |
} | |
} | |
return successorArr; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!