CSA Blog
Ian Wu
09 Jan 2025
public int countWays(int n) {
if (n == 0) return 1;
if (n < 0) return 0;
return countWays(n - 1) + countWays(n - 2);
}
countWays(3) –> countWays(2) + countWays(1)
countWays(3) –> (countWays(1) + countWays(0)) + (countWays(0) + countWays(-1))
countWays(3) –> (1 + 1) + (1 + 0) = 3
public int countWaysVariableSteps(int n, int[] steps) {
if (n == 0) return 1;
if (n < 0) return 0;
int totalWays = 0;
for (int step : steps) {
totalWays += countWaysVariableSteps(n - step, steps);
}
return totalWays;
}
int[] steps = {1, 2};
System.out.println(countWaysVariableSteps(4, steps));
This code has a time complexity of O(2^n) was two calls are made for each increment of n. This could be remedied by storing already calculated values and not calculating for values already calculated
-
return equation(6) * equation(7)
return (equation(5) * equation(4)) * (equation(5) * equation(4))
return (equation(5) * equation(4)) * ((equation(5) * equation(4)) * equation(5))
return 12 ^ 5
D. 248832
-
The program recursively iterates through every character and turns uppercase letters lowercase and lowercase letters uppercase
Therefore, answer is D. tHIS IS MY FAVORITE: yAY FOR PROGRAMMING!!!
-
A. The first function call in a recursive chain, since this call will initiate the other recursive calls
Sprint 1 - 3 Review Ticket
Team Issues / Plan
Issues Page
Planning, Assigned Role, etc. can all be found above
N@tM Presentation
Programming
Key Highlights
- Storing custom objects inside database, different ways.
- Could be boring, make three columns, but that can’t happen everywhere, so intentionally stored custom object
Method 1:
- Annotations, create variable not stored in database, and every time database is updated run function to convert unstored variable to string
Method 2 (code below)
Queue Commit
Image: 
@Converter
public class QueueConverter implements AttributeConverter<Queue, String> {
private static final ObjectMapper objectMapper = new ObjectMapper();
@Override
public String convertToDatabaseColumn(Queue queue) {
try {
return objectMapper.writeValueAsString(queue);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Error converting Queue to JSON", e);
}
}
@Override
public Queue convertToEntityAttribute(String dbData) {
try {
return objectMapper.readValue(dbData, Queue.class);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Error converting JSON to Queue", e);
}
}
}
Had to think about existing code…
Current user database is situated somewhere else, not on our backend. Therefore, I had to create the function separately with an async function so that this can be substituted later for a fetch from the existing user database. Everything else works independently of this.
async function fetchPeople() {
const response = await fetch('http://localhost:8085/api/people');
if (response.ok) {
const people = await response.json();
return people.map(person => person.name);
}
return [];
}
int x = 7;
int y = 3;
if ((x < 10) && (y < 0)) {
System.out.println("Value is: " + x * y);
} else {
System.out.println("Value is: " + x / y);
}
y is > 0, so the conditional fails and the code in the else block is ran. Therefore, it will print out the value 7 / 3. However, this is integer division, and therefore the resulting output is 2.
Q12: What is returned as a result of the call mystery(“computer”)
public String mystery(String input) {
String output = "";
for (int k = 1; k < input.length(); k = k + 2) {
output += input.substring(k, k + 1);
}
return output;
}
mystery("computer");
The for loop increments by two, iterating over every other element in the list and appending it to the output string.
Q19: The expression !(!(a != b) && (b > 7)) is equal to which of the following?
Simplification:
!(!(a != b) && (b > 7))
!((a == b) && (b > 7))
!(a == b) || !(b > 7)
(a != b) || (b <= 7)
Notes
Starting in the second half, I began using java, and it helped a lot more.
Example: Q23, best visualised with an extra print statement:
Q23: What will be the contents of animals be as a result of calling manipulate?
private List<String> animals = new ArrayList<>(Arrays.asList("bear", "zebra", "bass", "cat", "koala", "baboon"));
public void manipulate(List<String> animals) {
for (int k = animals.size() - 1; k > 0; k--) {
if (animals.get(k).substring(0, 1).equals("b")) {
animals.add(animals.size() - k, animals.remove(k));
}
System.out.println(animals);
}
}
manipulate(animals)
[bear, baboon, zebra, bass, cat, koala]
[bear, baboon, zebra, bass, cat, koala]
[bear, baboon, zebra, bass, cat, koala]
[bear, baboon, zebra, bass, cat, koala]
[bear, zebra, bass, cat, koala, baboon]
This is much easier to visualize than thinking through individually, although that is what will have to happen on the AP test.
Memories
6.4 CollegeBoard hack –> ChatGPT fail
Challenge Hack:
Create a function that iterates through every X items and shifts only those elements Y shift left.
Example: ({1, 2, 3, 4, 5, 6}, Y = 1, X = 2)
Output: {5, 2, 1, 4, 3, 6}
Example: ({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, Y = 2, X = 3)
Output: {10, 2, 3, 1, 5, 6, 4, 8, 9, 7}
int [] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int shift = 2;
int space = 4;
/* function */
private int [] shiftRight(int [] values, int shift, int space) {
/* code here */
}
for ( int value : shiftRight(numbers, shift, space)) {
System.out.println(value);
}
Beyond Perfunctory
Remake of python game on old blog. Jupyter notebook has no image display and therefore image file name is displayed instead. Image functionality is done on local file though. Practices transition from python to java, combines concepts from rest of units to demonstrate understanding and practice java.
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import java.awt.GridLayout;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.util.*;
class City {
char firstLetter;
String state;
String county;
String name;
int population;
String filename;
public City (String filename){
String[] information = filename.split("_");
this.firstLetter = information[0].charAt(0);
this.state = information[2];
this.county = information[1];
this.name = information[0];
String pop = information[3];
this.population = Integer.parseInt(pop.substring(0, pop.length() - 4));
this.filename = filename;
}
public char getFirstLetter() {
return firstLetter;
}
public String getState() {
return state;
}
public String getCounty() {
return county;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
public String getFilename() {
return filename;
}
public void displayImage(String filename) {
Main.showImage("./Cities/" + filename);
}
}
public class Main {
public static void main(String[] args) {
int end = 0;
int finaled = 0;
float score = 0;
float score_to_add;
Scanner scanner = new Scanner(System.in);
Random rand = new Random();
File folder = new File("./Cities");
File[] listOfFiles = folder.listFiles();
City city;
City[] cities = new City[listOfFiles.length];
for(int i = 0;i<listOfFiles.length;i++) {
city = new City(listOfFiles[i].getName());
cities[i] = city;
}
List<Integer> done_cities = new ArrayList<Integer>();
boolean done_yet;
boolean is_same;
int cities_done = 0;
while (end == 0) {
score_to_add = 1;
finaled = 0;
System.out.println("----------------------------------------");
System.out.println("Here is your city!");
done_yet = true;
int city_to_add = 0;
int randInt;
while (done_yet == true) {
is_same = false;
randInt = rand.nextInt(cities.length);
for(int i=0;i<done_cities.size();i++) {
if (randInt == done_cities.get(i)) {
is_same = true;
}
}
if (is_same == false) {
done_yet = false;
}
if (done_yet == false) {
city_to_add = randInt;
}
}
City current_city = cities[city_to_add];
cities_done += 1;
done_cities.add(city_to_add);
current_city.displayImage(current_city.getFilename());
System.out.println("Cities done: " + cities_done);
System.out.print("Enter Answer: ");
String response = scanner.nextLine();
while (finaled == 0) {
if (response.equals("s")) {
if (score_to_add > 0) {
System.out.println("State: " + current_city.getState());
score_to_add -= 0.5;
System.out.print("Enter Answer: ");
response = scanner.nextLine();
} else {
System.out.println("Two hints have already been used, sorry!");
System.out.print("Enter Answer: ");
response = scanner.nextLine();
}
} else if (response.equals("c")) {
if (score_to_add > 0) {
System.out.println("County: " + current_city.getCounty());
score_to_add -= 0.5;
System.out.print("Enter Answer: ");
response = scanner.nextLine();
} else {
System.out.println("Two hints have already been used, sorry!");
}
} else if (response.equals("f")) {
if (score_to_add > 0) {
System.out.println("First Letter: " + current_city.getFirstLetter());
score_to_add -= 0.5;
System.out.print("Enter Answer: ");
response = scanner.nextLine();
} else {
System.out.println("Two hints have already been used, sorry!");
}
} else if (response.equals("e")) {
System.out.println("Your final score was: " + score);
finaled = 1;
end = 1;
} else {
finaled = 1;
if (response.equals(current_city.getName())) {
score += score_to_add;
System.out.println("Good job! The city was " + current_city.getName() + ", " + current_city.getCounty() + ", " + current_city.getState() + " (" + current_city.getPopulation() + ")");
System.out.println("Score: " + score);
} else {
score = 0;
System.out.println("I'm sorry, The city was " + current_city.getName() + ", " + current_city.getCounty() + ", " + current_city.getState() + " (" + current_city.getPopulation() + ")");
System.out.println("Score: " + score);
}
}
}
}
}
static void showImage(final String fileName)
{
System.out.println(fileName);
}
}
Main.main(null)
----------------------------------------
Here is your city!
./Cities/Temple City_Los Angeles County_CA_36494.jpg
Cities done: 1
Enter Answer: Good job! The city was Temple City, Los Angeles County, CA (36494)
Score: 1.0
----------------------------------------
Here is your city!
./Cities/Anaheim_Orange County_CA_346824.jpg
Cities done: 2
Enter Answer: First Letter: A
Enter Answer: Good job! The city was Anaheim, Orange County, CA (346824)
Score: 1.5
----------------------------------------
Here is your city!
./Cities/Santee_San Diego County_CA_60037.jpg
Cities done: 3
Enter Answer: I'm sorry, The city was Santee, San Diego County, CA (60037)
Score: 0.0
----------------------------------------
Here is your city!
./Cities/Orange_Orange County_CA_139911.jpg
Cities done: 4
Enter Answer: I'm sorry, The city was Orange, Orange County, CA (139911)
Score: 0.0
----------------------------------------
Here is your city!
./Cities/North Tustin_Orange County_CA_25718.jpg
Cities done: 5
Enter Answer: I'm sorry, The city was North Tustin, Orange County, CA (25718)
Score: 0.0
----------------------------------------
Here is your city!
./Cities/Hesperia_San Bernardino County_CA_99818.jpg
Cities done: 6
Enter Answer: Your final score was: 0.0
23 Sep 2024