2022

public int getScore() {
	private int points = 0;
	if (levelOne.goalReached()) {
		points += levelOne.getPoints();
		if (levelTwo.goalReached()) {
            points += levelTwo.getPoints();
            if (levelThree.goalReached()) {
                points += levelThree.getPoints();
            }
        }
    }
    if (isBonus()) {
        points *= 3; 
    }
    return points; 
}
public int playManyTimes(int num) {
    int max = 0; 
    for (int i = 0; i < num; i++) {
        play();
        if (max < getScore()) {
            max = getScore(); 
        }

    }

    return max; 
}
public class Textbook extends Book {
    private int edition;
    public Textbook(String title, double price, int edition) {
        super(title, price); 
        this.edition = edition; 
    }

    public int getEdition() {
        return edition;
    }

    public String getBookInfo() {
        return super.getBookInfo() + "-" + edition;
    }

    public boolean canSubstituteFor(Textbook textbook) {
        if (this.getTitle().equals(textbook.getTitle()) && this.getEdition >= textbook.getEdition) {
            return true;
        }
        return false; 
    }
}
public double getAverageRating() {
    double sum = 0;
    for (Review review : allReviews) {
        sum += review.getRating();
    }
    return sum/allReviews.length;
}
public ArrayList<String> collectComments() {
    ArrayList<String> formatComments = new ArrayList<String>();
    for (int i = 0; i < allReviews.length; i++) {
        String comment = allReviews[i].getComment();
        if (comment.indexOf("!") >= 0) {
            String rating = i + "-" + comment;
            if !((comment.substring(comment.length() - 1).equals("!") || comment.substring(comment.length() - 1).equals("."))) {
                rating += ".";
            }
        }
        formatComments.add(rating);
    }
    return formatComments; 
}
public void repopulate() {
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[i].length; j++) {
            int element = (int)(Math.random() * MAX + 1);
            while ((element % 10 != 0) || (element % 100 == 0)) {
                element = (int)(Math.random() * MAX + 1);
            }
            grid[i][j] = element; 
        }
    }
}
public int countIncreasingCols() {
    int count = 0; 
    for (int col = 0; col < grid[0].length; col++) {
        int num = 0;
        boolean ascend = true;
        for (int row = 0; row < grid.length; row++) {
            if (num > grid[row][col]) {
                ascend = false;
            }
        }
        if (ascend) {
            count++;
        }
    }

    return count; 

}

2021

public int scoreGuess(String guess) {
    int length = guess.length();
    int count = 0;
    for (int i = 0; i <= secret.length() - length; i++) {
        if (secret.substring(i, i + length).equals(guess)) {
            count++; 
        }
    }
    return count * length * length; 
}
public String findBetterGuess(String guess1, String guess2) {
    int score1 = scoreGuess(guess1);
    int score2 = scoreGuess(guess2);

    if (score1 < score2) {
        return guess2;
    } 
    if (score1 > score2) {
        return guess1;
    }
    if (guess1.compareTo(guess2) < 0) {
        return guess2;
    } else {
        return guess1; 
    }
}
public class CombinedTable {
    private SingleTable t1;
    private SingleTable t2;

    public CombinedTable(SingleTable t1, SingleTable t2) {
        this.t1 = t1;
        this.t2 = t2; 
    }

    public boolean canSeat(int num) {
        if (num <= (t1.getNumSeats() + t2.getNumSeats() - 2)) {
            return true; 
        }
        return false; 
    }

    public double getDesirability() {
        if (t1.getHeight() == t2.getHeight()) {
            return (t1.getViewQuality() + t2.getViewQuality()) / 2; 
        }
        return (t1.getViewQuality() + t2.getViewQuality()) / 2 - 10;
    }
}
public void addMembers(String[] names, int gradYear) {
    for (String name : names) {
        MemberInfo member = new MemberInfo(name, gradYear, true);
        memberList.add(member);
    }
}
public ArrayList<MemberInfo> removeMembers(int year) {
    ArrayList<MemberInfo> goodMember = new ArrayList<MemberInfo>();
    for (int i = 0; i < memberList.size(); i++) {
        if (memberList.get(i).getGradYear() <= year) {
            memberList.remove(i);
        }
    }

    for (int i = 0; i < memberList.size(); i++) {
        if (memberList.get(i).inGoodStanding()) {
            goodMember.add(memberList.get(i));
        }
    }

    return goodMember;
}
public static boolean isNonZeroRow(int[][] array2D, int r) {
    boolean noZero = true;
    for (int i = 0; i < array2D[0].length; i++) {
        if (array2D[r][i] == 0) {
            noZero = false;
        }
    }

    return noZero;
}
public static int[][] resize(int[][] array2D) {
    int row = numNonZeroRows(array2D);
    int[][] resize = new int[row][array2D[0].length];

    int resizeRow = 0;

    for (int i = 0; i < array2D.length; i++) {
        if (isNonZeroRow(array2D, i)) {
            for (int j = 0; j < array2D[0].length; j++){
                resize[resizeRow][j] = array2D[i][j];
            }
            resizeRow++;
        }
    }

    return resize; 
}

2019

public static int numberOfLeapYears(int year1, int year2) {
    int num = 0; 

    for (int i = year1; i <= year2; i++) {
        if (isLeapYear(i)) {
            num++;
        }
    }
    return num;
}
public static int dayOfWeek(int month, int day, int year) {
    int numDays = dayOfYear(month, day, year);

    return (numDays - 1 + firstDayOfYear(year)) % 7 
}
public class StepTracker {
    private int steps;
    private int activeDay;
    private int days;
    private int stepGoal;

    public StepTracker(int stepGoal) {
        this.stepGoal = stepGoal; 
    }

    public void addDailySteps(int steps) {
        this.steps += steps; 
        if (steps >= stepGoal) {
            activeDay++; 
        }
        days++;
    }

    public double averageSteps() {
        return (double)(steps/days);
    }

    public int activeDays() {
        return activeDay; 
    }
}
public ArrayList<String> getDelimitersList(String[] tokens) {
    ArrayList<String> delimiters = new ArrayList<String>();
    for (String ch : tokens) {
        if (ch.equals(openDel) || ch.equals(closeDel)) {
            delimiters.add(ch);
        }
    }
    return delimiters;
}
public boolean isBalanced(ArrayList<String> delimiters) {
    int open = 0; 
    int close = 0; 

    for (String ch : delimiters) {
        if (ch.equals(openDel)) {
            open++;
        } else {
            close++;
        }

        if (close > open) {
            return false; 
        }
    }

    if (open != close) {
        return false;
    }
    return true;
}
public LightBoard(int numRows, int numCols) {
    lights = new boolean[numRows][numCols];
    int[] random = {0, 0, 1, 1, 1};
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            int num = (int)(Math.random() * 5)
            if (random[num] == 0) {
                lights[i][j] = true;
            } else {
                lights[i][j] = false;
            }
        }
    }
}
public boolean evaluateLight(int row, int col) {
    int on = 0;
    for (int i = 0; i < lights.length; i++) {
        if (lights[i][col] == true) {
            on++;
        }
    }

    if (lights[row][col] == true) {
        if (on % 2 == 0) {
            return false;
        }
    }


    if (lights[row][col] == false) {
        if (on % 3 == 0) {
            return true;
        }
    }

    return lights[row][col];
}

2018

public boolean simulate() {
    int distance = 0; 
    int hops = 0;
    while (distance < goalDistance && hops < maxHops) {
        distance += hopDistance();
        hops++;
        if (distance < 0) {
            return false; 
        }
    }
    if (distance >= goalDistance && hops <= maxHops) {
        return true;
    }
    return false; 
}
public double runSimulations(int num) {
    int success = 0;
    for (int i = 0; i < num; i++) {
        if (simulate()) {
            success++;
        }
    }
    return (double)success/num;
}
public WordPairList(String[] words) {
    allPairs = new ArrayList<WordPair>();
    for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) {
            WordPair word = new WordPair(words[i], words[j]);
            allPairs.add(word);
        }
    }
}
public int numMatches() {
    int count = 0;
    for (WordPair word : allPairs) {
        if (word.getFirst().equals(word.getSecond())) {
            count++;
        }
    }
    return count;
}
public static int[] getColumn(int[][] arr2D, int c) {
    int[] column = new int[arr2D.length];
    for (int row = 0; row < arr2D.length; row++) {
        column[row] = arr2D[row][c];
    }
    return column; 
}
public static boolean isLatin(int[][] square) {
    if (containsDuplicates(square[0])) {
        return false;
    }

    for (int i = 1; i < square.length; i++) {
        if (!hasAllValues(square[0], square[i])) {
            return false;
        }
    }

    for (int i = 0; i < square.length; i++) {
        if (!hasAllValues(square[0], getColumn(square, i))) {
            return false;
        }
    }
    return true;
}

2017

public Digits(int num) {
    digitList = new ArrayList<Integer>();
    while (num > 0) {
        digitList.set(0, num%10);
        num /= 10;
    }
    if (num == 0) {
        digitList.set(0, 0);
    }
}
public boolean isStrictlyIncreasing() {
    int max = 0; 
    if (digitList.size() == 0) {
        return true;
    }
    for (Integer num : digitList) {
        if (num > max) {
            max = num; 
        } else {
            return false;
        }
    }
    return true;
}
public void replaceNthOccurrence(String str, int n, String repl) {
    String returnStr = "";
    if (findNthOccurrence(str, n) == -1) {
        return currentPhrase; 
    } else {
        for (int i = 0; i < findNthOccurrence(str, n); i++) {
            returnStr += currentPhrase.substring(i, i+1);
        }
        returnStr += repl;
        for (int i = findNthOccurrence(str, n) + str.length(); i < currentPhrase.length(); i++) {
            returnStr += currentPhrase.substring(i, i+1); 
        }
        return returnStr;
    }
}

redo #3a

public void replaceNthOccurrence(String str, int n, String repl) {
    int loc = findNthOccurence(str, n);
    if (loc > -1) {
        currentPhrase = currentPhrase.substring(0, loc - 1) + repl + currentPhrase.substring(loc + str.length(), currentPhrase.length());
    }
}
public int findLastOccurrence(String str) {
    int index = findNthOccurence(str, 1);
    int i = 1; 
    while (index != -1) {
        i++;
        index = findNthOccurence(str, i);
    }
    return index; 
}
public static Position findPosition(int num, int[][] intArr) {
    for (int i = 0; i < intArr.length; i++) {
        for (int j = 0; j < intArr[0].length; j++) {
            if (num == intArr[i][j]) {
                return new Position(i, j);
            }
        }
    }
    return null;
}
public static Position[][] getSuccessorArray(int[][] intArr) {
    Position[][] positionArray = new Position[intArr.length][intArr[0].length];
    for (int i = 0; i < intArr.length; i++) {
        for (int j = 0; j < intArr[0].length; j++) {
            positionArray[i][j] = findPosition(intArr[i][j] + 1, intArr);
        }
    }
    return positionArray;
}

2016

public class RandomStringChooser {
    private ArrayList<String> array;

    public RandomStringChooser(String[] array) {
        array = new ArrayList<String>();
        for (String words : array) {
            array.add(words);
        }
    }

    public String getNext() {
        if (array.size() > 0) {
            int index = (int)(Math.random() * array.size())
            String returnWord = array.get(index);
            array.remove(index);
            return returnWord;
        } else {
            return "NONE";
        }
    }
}
public RandomLetterChooser(String str) {
    super(getSingleLetters(str));
}
public LogMessage(String message) {
    machineId = message.substring(0, message.indexOf(":"));
    description = message.substring(message.indexOf(":") + 1);
}
public boolean containsWord(String keyword) {
    int startIndex = description.indexOf(keyword);
    int endIndex = startIndex + 3;
    if ((startIndex != -1) && (startIndex == 0 || description.substring(startIndex - 1, startIndex).equals(" ")) && (endIndex == description.length() - 1 || description.substring(startIndex + 4, startIndex + 5).equals(" "))) {
        return true;
    }
    return false; 
}
public List<LogMessage> removeMessages(String keyword) {
    List<LogMessage> wordList = new List<LogMessage>();
    for (int i = 0; i < messageList.size(); i++) {
        String description = messageList.get(i).getDescription();
        if (messageList.get(i).containsWord(description)) {
            wordList.add(messageList.remove(i));
        }
    }
    return wordList;
}
private boolean toBeLabeled(int r, int c, boolean[][] blackSquares) {
    if (blackSquares[r][c]) {
        return false;
    } 
    
    if (r == 0 || c == 0) {
        return true;
    }

    if (blackSquares[r - 1][c] || blackSquares[r][c-1]) {
        return true;
    }
}
public Crossword(boolean[][] blackSquares) {
    puzzle = new Square[blackSquares.length][blackSquares[0].length];
    int num = 1; 
    for (int i = 0; i < blackSquares.length; i++) {
        for (int j = 0; j < blackSquares[0].length; j++) {
            if (blackSquares[i][j]) {
                puzzle[i][j] = new Square(true, 0);
            } else {
                if (toBeLabeled(i, j, blackSquares)) {
                    puzzle[i][j] = new Square(false, num);
                    num++;
                } else {
                    puzzle[i][j] = new Square(false, 0);
                }
            }
        }
    }
}
public static int totalLetters(List<String> wordList) {
    int length = 0; 
    for (String word : wordList) {
        length += word.length();
    }
    return length; 
}
public static int basicGapWidth(List<String> wordList, int formattedLen) {
    return (formattedLen - totalLetters(wordList)) / (wordList.length - 1);
}
public static String format(List<String> wordList, int formattedLen) {
    int leftover = leftoverSpaces(wordList, formattedLen);

    String text = "";
    for (int i = 0; i < wordList.length; i++) {
        text += wordList.get(i);
        for (int j = 0; j < basicGapWidth(wordList, formattedLen); j++) {
            text += " "; 
        }
        if (leftover > 0) {
            text += " ";
            leftover--;
        }
    }

    return text; 
}

2015

public static int arraySum(int[] arr) {
    int sum = 0;
    for (int num : arr) {
        sum += num;
    }
    return sum; 
}
public static int[] rowSums(int[][] arr2D) {
    int[] sumArray = new int[arr2D.length];
    for (int i = 0; i < arr2D.length; i++) {
        sumArray[i] = arraySum(arr2D[i]);
    }
    return sumArray;
}
public static boolean isDiverse(int[][] arr2D) {
    int[] sumArray = rowSums(arr2D);

    for (int i = 0; i < sumArray.length - 1; i++) {
        for (int j = i + 1; j < sumArray.length; j++) {
            if (sumArray[i] == sumArray[j]) {
                return false; 
            }
        }
    }
    return true;

}
public class HiddenWord {
    private String word;

    public HiddenWord(String word) {
        this.word = word; 
    }

    public String getHint(String guess) {
        String result = "";
        for (int i = 0; i < guess.length(); i++) {
            String letter = guess.substring(i, i+1);
            if (letter.equals(word.substring(i, i+1))) {
                result += letter;
            } else if (word.indexOf(letter) != -1) {
                result += "+";
            } else {
                result += "*";
            }
        }
        return result; 
    }

}
public int getValueAt(int row, int col) {
    for (SparseArrayEntry entry : entries) {
        if (entry.getRow() == row && entry.getCol() == col) {
            return entry.getValue();
        }
    }
    return 0;
}
public void removeColumn(int col) {
    numCols--;
    for (int i = entries.size() - 1; i >=0; i--) {
        if (entries.get(i).getCol() == col) {
            entries.remove(i);
        }
    }
    for (int i = 0; i < entries.size(); i++) {
        if (entries.get(i).getCol() > col) {
            entries.get(i).col = entries.get(i).getCol() - 1;
        }
    }
}

2014

public static String scrambleWord(String word) {
    String wordReturn = "";
    int i = 0; 
    while (i < word.length()){
        if ((word.substring(i, i + 1).equals("A") && !word.substring(i + 1, i + 2).equals("A"))) {
            wordReturn += word.substring(i + 1, i + 2);
            wordReturn += "A";
            i += 2;
        } else {
            wordReturn += word.substring(i, i + 1);
            i++;
        }
    }
    return wordReturn;
}
public static void scrambleOrRemove(List<String> wordList) {
    for (int i = wordList.size() - 1; i >= 0; i--) {
        String scramble = scrambleWord(wordList.get(i));
        if (wordList.get(i).equals(scramble)) {
            wordList.remove(i);
        } else {
            wordList.set(i, scramble);
        }
    }
}
public SeatingChart(List<Student> studentList, int rows, int cols) {
    seats = new Student[rows][cols];
    int count = 0;

    for (int col = 0; col < seats[rows].length; col++) {
        for (int row = 0; row < seats.length; row++) {
            if (count < studentList.size() - 1) {
                seats[col][row] = studentList.get(count);
                count++;
            } else {
                seats[col][row] = null; 
            }

        }
    }
}
public int removeAbsentStudents(int allowedAbsences) {
    int count = 0;
    for (int row = 0; row < seats.length; row++) {
        for (int col = 0; col < seats[row].length; col++) {
            if (seats[row][col] != null) {

                if (seats[row][col].getAbsenceCount() > allowedAbsences) {
                    seats[row][col] = null;
                    count++;
                }
            }
        }
    }
    return count; 
}
public class A {
   

    public static void main(String[] args) {
        ArrayList<Integer> test = new ArrayList<Integer>();
        test.add(5);
        test.add(3);
        System.out.println(test);

        for (int i = test.size() - 1; i >= 0; i--) {
            if (test.get(i) == 5) {

                test.remove(i);
            } else {
                test.set(i, 1);
            }
        }
    
        System.out.println(test);
    }
}
A.main(null)
[5, 3]
[1]