FRQs

  • See FRQ #4 bellow for problem description from CollegeBoard

Problem Summary

  • Data class with the following functions repopulate, and countIncreasingCols with there being no return value on the repopulate function and the countIncreasingCols function returning an integer value.

Repopulate

  • The value generated is between 1 & MAX inclusive with MAX assumed to be greater than or equal to 10.
  • The value generated must be be divisible by 10 but not 100.

CountIncreasingCols

  • Assumes that grid cannot be null
  • Assumes that grid is a rectangular 2D array with at least one row and one column
  • If there is only one column, then the method returns 1 increasing column

Solutions

  • Following solutions both in Java and Python are provided bellow
  • Some changes had to have been made to solution template to make the program run such as the addition of the constructor for Data class and other various additions to the program beyond just the function definitions.
  • Notice: In an actual exam the FRQ should only be answered in Java, Python is only provided for the sake of convenience and to help with understanding the problem.

Java Solution

class Solution {
    public boolean isPalindrome(int x) {
        String number = ""  +  x;
        String reversedNumber = ""; 
        
        if (x < 0){
            return false;
        }

        for (int i = number.length()-1; i >= 0 ; i--){
            reversedNumber += number.substring(i,i+1);
        }

        if (!number.equals(reversedNumber)){
            System.out.println("Number: " + number + " Reversed: " + reversedNumber);
            return false;
        }

        return true;
                
    }

    public static void main(String args[]){
        Solution s = new Solution();
        System.out.println(s.isPalindrome(121));
        System.out.println(s.isPalindrome(-121));
        System.out.println(s.isPalindrome(10));
        System.out.println(s.isPalindrome(-101));
    }
}

Solution.main(null);



true
false
Number: 10 Reversed: 01
false
false
import java.util.Random;

public class Data {
    // Declaration of given variables grid and MAX
    private int[][] grid;
    public static final int MAX = 110;
    
    // Creation of Random object for use in repopulate method
    private Random random ;

    // Constructor for Data class
    public Data(int[][] grid ) {
        random = new Random();
        this.grid = grid;

    }

    // Just for printing can be ignored not part of solution
    public void print_grid() {
        for(int i = 0; i < grid.length; i++) {
          for(int j=0; j < grid[0].length; j++) {
              System.out.print(grid[i][j] + " ");
              }
              System.out.println();
            }
    }

    public void repopulate() {
        int random_element; // Variable to store random element
        for(int i = 0; i < grid.length; i++) { // Loop through rows
          for(int j=0; j < grid[0].length; j++) { // Loop through columns
              while(true) {
                random_element = random.nextInt(MAX); // Generate random element
                if (random_element % 10 == 0 && random_element % 100 != 0) { // Checks criteria of divisibility by 10 and not 100 
                  grid[i][j] = random_element; // If met set element to random element and break out of loop 
                  break;
                } else {
                  grid[i][j] = random.nextInt(MAX); // If not met a new element is generated
                }
              }
            }
        }
    }

    public int countIncreaseCols() {
        int count = 0;
        for (int j = 0; j < grid[0].length; j++) { // Iterates through columns
            boolean isIncreasing = true; 
            if (grid[0].length > 1) { // Checks if there is more than one column to prevent out of bounds error
                for (int i = 1; i < grid.length; i++) {  // Iterates through rows
                    if (grid[i][j] <= grid[i - 1][j]) { // Checks if the current element is less than or equal to the previous element
                        isIncreasing = false; // If so set isIncreasing to false and break out of loop
                        break; 
                    }
                }
        
                if (isIncreasing) { // If the column is increasing increment count as if 
                    count++;        // the value is not less than or equal to the previous 
                                    // element then it must be increasing 
                }
            }
            
            else if (grid[0].length == 1) { // To match the criteria of a single column being increasing
                count++;
            }

            else { // If there are no columns then break out of loop
                break;
            }
        }
        return count; 
    }
    
}
    int[][] grid = new int[3][3];

    Data data = new Data(grid);
    System.out.println("Value set as Max: " + data.MAX);
        
    System.out.println("Initial grid:");
    data.print_grid();
    System.out.println("The increasing columns within the data: " + data.countIncreaseCols());
        
    data.repopulate();

    System.out.println("\nRepopulated grid:");
    data.print_grid();
    System.out.println("The increasing columns within the data: " + data.countIncreaseCols());

    System.out.println("\n\n2nd Test Case: \n\n");
    int[][] grid2 = new int[1][1];

    Data data2 = new Data(grid2);
    System.out.println("Value set as Max: " + data2.MAX);
        
    System.out.println("Initial grid:");
    data2.print_grid();
    System.out.println("The increasing columns within the data: " + data2.countIncreaseCols());
        
    data2.repopulate();

    System.out.println("\nRepopulated grid:");
    data2.print_grid();
    System.out.println("The increasing columns within the data: " + data2.countIncreaseCols());
    


Value set as Max: 110
Initial grid:
0 0 0 
0 0 0 
0 0 0 
The increasing columns within the data: 0



Repopulated grid:
30 40 90 
40 70 80 
50 60 50 
The increasing columns within the data: 1


2nd Test Case: 


Value set as Max: 110
Initial grid:
0 
The increasing columns within the data: 1

Repopulated grid:
80 
The increasing columns within the data: 1

Python Solution

  • As Java as a language may be unfamiliar to some of you, I have provided a Python solution to the problem. Allowing for greater clarity and understanding of the problem. All methods have the same function as the java code with type hinting to better facilitate comparisons.
import random
from typing import List # Type hinting to better highlight data types like the Java code

class Data:
    def __init__(self, grid: List[List[int]]):
        self.grid: List[List[int]] = grid
        self.MAX: int = 110

    def _repopulate(self) -> None:
        row: int
        cID: int
        for row in range(len(self.grid)):
            for cID in range(len(self.grid[row])):
                # Generate a random number between 1 and self.MAX that is divisible by 10 but not by 100
                value: int = random.randint(1, self.MAX)
                while value % 10 != 0 or value % 100 == 0:
                    value = random.randint(1, self.MAX)
                self.grid[row][cID] = value

    def _countIncreaseCols(self) -> int:
        count: int = 0
        row: int
        cID: int
        for cID in range(len(self.grid[0])):
            is_increasing: bool = True  # Flag to track if the column is increasing
            if len(self.grid[0]) > 1:  # If the column has more than one element
                for row in range(1, len(self.grid)):
                    if self.grid[row][cID] <= self.grid[row - 1][cID]:
                        is_increasing = False
                        break
                if is_increasing:
                    count += 1
            
            elif len(self.grid[0]) == 1:  # If the column has only one element
                count += 1

            else:
                break
        return count

    def _printgrid(self) -> None:
        row: int
        cID: int
        for row in range(len(self.grid)):
            for cID in range(len(self.grid[row])):
                print(self.grid[row][cID], end=" ")
            print()

if __name__ == "__main__":
    grid: List[List[int]] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    data = Data(grid)

    print(f"Value set as Max: {data.MAX}")
    print("Initial grid:")
    data._printgrid()
    print(f"The increasing columns within the data {data._countIncreaseCols()}")
    
    print(f"\nValue set as Max: {data.MAX}")
    data._repopulate()
    print("Repopulated grid")
    data._printgrid()
    print(f"The increasing columns within the data {data._countIncreaseCols()}")

    print("\n\n2nd Test Case: \n\n")
    grid2: List[List[int]] = [[0]]
    data2 = Data(grid2)

    print(f"Value set as Max: {data2.MAX}")
    print("Initial grid:")
    data2._printgrid()
    print(f"The increasing columns within the data {data._countIncreaseCols()}")
    
    print(f"Value set as Max: {data2.MAX}")
    data2._repopulate()
    print("\nRepopulated grid")
    data2._printgrid()
    print(f"The increasing columns within the data {data2._countIncreaseCols()}")

Value set as Max: 110
Initial grid:
0 0 0 
0 0 0 
0 0 0 
The increasing columns within the data 0

Value set as Max: 110
Repopulated grid
40 20 90 
30 30 40 
110 70 50 
The increasing columns within the data 1


2nd Test Case: 


Value set as Max: 110
Initial grid:
0 
The increasing columns within the data 1
Value set as Max: 110

Repopulated grid
10 
The increasing columns within the data 1