From d5ff836049e004f52b628236e507d6334adfad52 Mon Sep 17 00:00:00 2001 From: YashShah138 <89223735+YashShah138@users.noreply.github.com> Date: Thu, 27 Apr 2023 09:53:18 -0700 Subject: [PATCH] frqs stuff --- .vscode/targets.log | 12 +- _notebooks/2023-04-27-CarLot.ipynb | 20 +- _notebooks/2023-04-27-FRQs.ipynb | 339 ++++++++++------------------- 3 files changed, 121 insertions(+), 250 deletions(-) diff --git a/.vscode/targets.log b/.vscode/targets.log index 797d33e0..112c0ca3 100644 --- a/.vscode/targets.log +++ b/.vscode/targets.log @@ -7,7 +7,7 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question # This program built for i386-apple-darwin11.3.0 -# Make data base, printed on Wed Apr 26 15:17:54 2023 +# Make data base, printed on Thu Apr 27 08:35:46 2023 # Variables @@ -140,8 +140,6 @@ XPC_SERVICE_NAME = application.com.microsoft.VSCode.57404499.57404505 # automatic +D = $(patsubst %/,%,$(dir $+)) # environment -VSCODE_L10N_BUNDLE_LOCATION = -# environment RECOMMENDER_API_URL = https://gw.api.openshift.io//api/v2 # automatic +F = $(notdir $+) @@ -170,11 +168,11 @@ MAKELEVEL := 0 # environment CONDA_PREFIX = /Users/yash/anaconda3 # environment -VSCODE_PID = 46681 +VSCODE_PID = 87621 # environment LANG = C # variable set hash-table stats: -# Load=81/1024=8%, Rehash=0, Collisions=4/105=4% +# Load=80/1024=8%, Rehash=0, Collisions=4/104=4% # Pattern-specific Variable Values @@ -290,10 +288,10 @@ restart-jekyll: .FORCE help: # Implicit rule search has not been done. +# Modification time never checked. make: *** No rule to make target `all'. Stop. -# Modification time never checked. # File has not been updated. # commands to execute (from `Makefile', line 2): cat Makefile @@ -342,6 +340,6 @@ convert: .FORCE # strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096 # strcache free: total = 4087 / max = 4087 / min = 4087 / avg = 4087 -# Finished Make data base on Wed Apr 26 15:17:54 2023 +# Finished Make data base on Thu Apr 27 08:35:47 2023 diff --git a/_notebooks/2023-04-27-CarLot.ipynb b/_notebooks/2023-04-27-CarLot.ipynb index 222d21a7..3f595dd9 100644 --- a/_notebooks/2023-04-27-CarLot.ipynb +++ b/_notebooks/2023-04-27-CarLot.ipynb @@ -31,11 +31,7 @@ { "cell_type": "code", "execution_count": 12, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -138,11 +134,7 @@ { "cell_type": "code", "execution_count": 13, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -308,11 +300,7 @@ { "cell_type": "code", "execution_count": 19, - "metadata": { - "vscode": { - "languageId": "java" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -545,7 +533,7 @@ "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", - "name": "Java", + "name": "java", "pygments_lexer": "java", "version": "11.0.12+8-LTS-237" }, diff --git a/_notebooks/2023-04-27-FRQs.ipynb b/_notebooks/2023-04-27-FRQs.ipynb index 191f4b65..c514fee6 100644 --- a/_notebooks/2023-04-27-FRQs.ipynb +++ b/_notebooks/2023-04-27-FRQs.ipynb @@ -19,99 +19,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## FRQs - Methods and Control Structures\n", - "### 2019 FRQ Question 1" + "## FRQs - Methods and Control Structures" ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, + "execution_count": 7, + "metadata": { + "vscode": { + "languageId": "java" + } + }, "outputs": [], "source": [ - "//////////////////// GIVEN CODE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "//////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "public static int getCheck(int num) {\n", + " int sum = 0;\n", + " int numDigits = getNumberOfDigits(num);\n", "\n", - "public class APCalendar {\n", - " /** Returns true if year is a leap year and false otherwise. */\n", - " private static boolean isLeapYear(int year) {\n", - " if (year % 4 == 0) {\n", - " if (year % 100 == 0) {\n", - " if (year % 400 == 0) {\n", - " return true;\n", - " } else {\n", - " return false;\n", - " }\n", - " } else {\n", - " return true;\n", - " }\n", - " } else {\n", - " return false;\n", - " }\n", - " } \n", - " \n", - " \n", - " //////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - " public static int numberOfLeapYears(int year1, int year2) {\n", - " int count = 0;\n", - " for (int y = year1; y <= year2; y++) {\n", - " if (isLeapYear(y)) {\n", - " count++;\n", - " }\n", - " }\n", - " return count;\n", + " for (int i = 1; i <= numDigits; i++) {\n", + " int factor = numDigits - i + 2;\n", + " sum += factor * getDigit(num, i);\n", " }\n", "\n", - " /** Returns the value representing the day of the week for the first day of year, * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. */\n", - " private static int firstDayOfYear(int year) {\n", - " int y = year - 1;\n", - " int d = (y + (y / 4) - (y / 100) + (y / 400) + 1) % 7;\n", - " return d;\n", - " }\n", - " \n", - " /** Returns n, where month, day, and year specify the nth day of the year.\n", - " * Returns 1 for January 1 (month = 1, day = 1) of any year.\n", - " * Precondition: The date represented by month, day, year is a valid date.\n", - " */\n", - " private static int dayOfYear(int month, int day, int year) {\n", - " int[] daysInMonth = {31, 28 + (isLeapYear(year) ? 1 : 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n", - " int numDays = 0;\n", - " for (int i = 1; i < month; i++) {\n", - " numDays += daysInMonth[i - 1];\n", - " }\n", - " numDays += day;\n", - " if (isLeapYear(year) && month > 2) {\n", - " numDays++;\n", - " }\n", - " return numDays;\n", - " }\n", - " \n", - "\n", - " /** Returns the value representing the day of the week for the given date\n", - " * (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,\n", - " * and 6 denotes Saturday.\n", - " * Precondition: The date represented by month, day, year is a valid date.\n", - " */\n", - " //////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - " public static int dayOfWeek(int month, int day, int year) {\n", - " int startDay = firstDayOfYear(year);\n", - " int nthDay = dayOfYear(month, day, year);\n", - " int returnDay = (startDay + nthDay - 1) % 7;\n", - " return returnDay;\n", - " }\n", - "\n", - " // public static void main1(String[] args) {\n", - " // int year1 = 2005;\n", - " // int year2 = 2023;\n", - " // int month = 4;\n", - " // int day = 26;\n", - " // int year = 2023;\n", - " \n", - " // System.out.println(\"Number of leap years from \" + year1 + \" to \" + year2 + \": \" + numberOfLeapYears(year1, year2));\n", - " // System.out.println(\"Day of the week for \" + month + \"-\" + day + \"-\" + year + \": \" + dayOfWeek(month, day, year));\n", - " // }\n", + " int checkDigit = sum % 10;\n", + " return checkDigit;\n", "}\n", "\n", - "// APCalendar.main1(null);" + "//////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "public static boolean isValid(int numWithCheckDigit) {\n", + " int numDigits = getNumberOfDigits(numWithCheckDigit);\n", + " int checkDigit = getDigit(numWithCheckDigit, numDigits);\n", + " int number = numWithCheckDigit / 10; // remove check digit\n", + " int computedCheckDigit = getCheck(number);\n", + " return checkDigit == computedCheckDigit;\n", + "}" ] }, { @@ -119,69 +61,44 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## FRQs - Classes\n", - "### 2019 FRQ Question 2" + "## FRQs - Classes" ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, + "execution_count": 6, + "metadata": { + "vscode": { + "languageId": "java" + } + }, "outputs": [], "source": [ - "public class StepTracker {\n", - " private int minSteps;\n", - " private int totalSteps;\n", - " private int numActiveDays;\n", - " private int numDays;\n", - " \n", - " public StepTracker(int minSteps) {\n", - " this.minSteps = minSteps;\n", - " this.totalSteps = 0;\n", - " this.numActiveDays = 0;\n", - " this.numDays = 0;\n", + "public class AdditionPattern {\n", + " private int current;\n", + " private final int start;\n", + " private final int increment;\n", + "\n", + " public AdditionPattern(int start, int increment) {\n", + " this.start = start;\n", + " this.increment = increment;\n", + " this.current = start;\n", " }\n", " \n", - " public void addDailySteps(int steps) {\n", - " totalSteps += steps;\n", - " numDays++;\n", - " if (steps >= minSteps) {\n", - " numActiveDays++;\n", - " }\n", + " public int currentNumber() {\n", + " return current;\n", " }\n", " \n", - " public int activeDays() {\n", - " return numActiveDays;\n", + " public void next() {\n", + " current += increment;\n", " }\n", " \n", - " public double averageSteps() {\n", - " if (numDays == 0) {\n", - " return 0.0;\n", - " } else {\n", - " return (double) totalSteps / numDays;\n", + " public void prev() {\n", + " if (current - increment >= start) {\n", + " current -= increment;\n", " }\n", " }\n", - "\n", - " // public static void main2(String[] args) {\n", - " // // create a StepTracker object with a minimum of 5000 steps per day\n", - " // StepTracker st = new StepTracker(5000);\n", - " \n", - " // // add daily steps for the past week\n", - " // st.addDailySteps(6000);\n", - " // st.addDailySteps(4500);\n", - " // st.addDailySteps(7000);\n", - " // st.addDailySteps(3000);\n", - " // st.addDailySteps(5500);\n", - " // st.addDailySteps(8000);\n", - " // st.addDailySteps(4000);\n", - " \n", - " // // print the number of active days and the average number of steps per day\n", - " // System.out.println(\"Active days: \" + st.activeDays());\n", - " // System.out.println(\"Average steps per day: \" + st.averageSteps());\n", - " // }\n", - "}\n", - "\n", - "// StepTracker.main2(null);" + "}" ] }, { @@ -189,75 +106,47 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## FRQs - Arrays and ArrayLists\n", - "### 2019 FRQ Question 3" + "## FRQs - Arrays and ArrayLists" ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, + "execution_count": 10, + "metadata": { + "vscode": { + "languageId": "java" + } + }, "outputs": [], "source": [ - "import java.util.ArrayList;\n", - "\n", - "public class Delimiters {\n", - " /** The open and close delimiters. */\n", - " private String openDel;\n", - " private String closeDel;\n", - " \n", - " /** Constructs a Delimiters object where open is the open delimiter and close is the\n", - " * close delimiter.\n", - " * Precondition: open and close are non-empty strings. */\n", - " public Delimiters(String open, String close) {\n", - " openDel = open;\n", - " closeDel = close;\n", - " }\n", - " \n", - " /** Returns an ArrayList of delimiters from the array tokens, as described in part (a). */\n", - " //////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - " public ArrayList getDelimitersList(String[] tokens) {\n", - " ArrayList delimitersList = new ArrayList<>();\n", - " for (String token : tokens) {\n", - " if (isDelimiter(token)) {\n", - " delimitersList.add(token);\n", - " }\n", + "//////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "public boolean addRepair(int m, int b) {\n", + " for (CarRepair repair : schedule) {\n", + " if (repair.getMechanicNum() == m || repair.getBayNum() == b) {\n", + " return false;\n", " }\n", - " return delimitersList;\n", - " }\n", - "\n", - " /** Returns true if the delimiters are balanced and false otherwise, as described in part (b). * Precondition: delimiters contains only valid open and close delimiters. */\n", - " //////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - " public boolean isDelimiter(String token) {\n", - " return token.equals(\"(\") || token.equals(\")\") || token.equals(\"[\") || token.equals(\"]\") || token.equals(\"{\") || token.equals(\"}\");\n", " }\n", + " schedule.add(new CarRepair(m, b));\n", + " return true;\n", + "}\n", "\n", - " public boolean isBalanced(ArrayList delimiters) {\n", - " int count = 0;\n", - " for (String delimiter : delimiters) {\n", - " if (delimiter.equals(openDel)) {\n", - " count++;\n", - " } else if (delimiter.equals(closeDel)) {\n", - " count--;\n", - " }\n", - " if (count < 0) {\n", - " return false;\n", + "//////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "public ArrayList availableMechanics() {\n", + " ArrayList availableMechanics = new ArrayList();\n", + " for (int i = 2; i <= 5; i++) { // iterate over all mechanic numbers\n", + " boolean isAvailable = true;\n", + " for (CarRepair repair : schedule) { // iterate over all repairs in schedule\n", + " if (repair.getMechanicNum() == i) { // if the mechanic is already assigned to a repair\n", + " isAvailable = false;\n", + " break; // exit inner loop as soon as a mechanic is found to be busy\n", " }\n", " }\n", - " return count == 0;\n", + " if (isAvailable) {\n", + " availableMechanics.add(i); // add the available mechanic to the list\n", + " }\n", " }\n", - "\n", - " // public static void main3(String[] args) {\n", - " // String[] tokens = {\"{\", \"(\", \"a\", \")\", \"}\", \"[\", \"b\", \"]\", \"(\", \"c\", \")\"};\n", - " // Delimiters delimiters = new Delimiters(\"(\", \")\");\n", - " // ArrayList delimitersList = delimiters.getDelimitersList(tokens);\n", - " // System.out.println(\"Delimiters list: \" + delimitersList);\n", - " // boolean isBalanced = delimiters.isBalanced(delimitersList);\n", - " // System.out.println(\"Is balanced: \" + isBalanced);\n", - " // }\n", - "}\n", - "\n", - "// Delimiters.main3(null);" + " return availableMechanics; // return the list of available mechanics\n", + "}" ] }, { @@ -271,51 +160,47 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, + "execution_count": 8, + "metadata": { + "vscode": { + "languageId": "java" + } + }, "outputs": [], "source": [ - "public class LightBoard {\n", - " /** The lights on the board, where true represents on and false represents off. */\n", - " private boolean[][] lights;\n", - " \n", - " /** Constructs a LightBoard object having numRows rows and numCols columns.\n", - " * Precondition: numRows > 0, numCols > 0\n", - " * Postcondition: each light has a 40% probability of being set to on. */\n", - " //////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - " public LightBoard(int numRows, int numCols) {\n", - " lights = new boolean[numRows][numCols];\n", - " Random random = new Random();\n", - " for (int r = 0; r < numRows; r++) {\n", - " for (int c = 0; c < numCols; c++) {\n", - " if (random.nextDouble() < 0.4) {\n", - " lights[r][c] = true;\n", - " }\n", - " }\n", + "//////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "public static boolean isNonZeroRow(int[][] array2D, int r) {\n", + " for (int col = 0; col < array2D[r].length; col++) {\n", + " if (array2D[r][col] == 0) {\n", + " return false;\n", " }\n", - " } \n", + " }\n", + " return true;\n", + "}\n", "\n", - " /** Evaluates a light in row index row and column index col and returns a status\n", - " * as described in part (b).\n", - " * Precondition: row and col are valid indexes in lights. */\n", - " //////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - " public boolean evaluateLight(int row, int col) {\n", - " int numOn = 0;\n", - " for (int r = 0; r < lights.length; r++) {\n", - " for (int c = 0; c < lights[0].length; c++) {\n", - " if (lights[r][c] && (r == row || c == col)) {\n", - " numOn++;\n", - " }\n", + "//////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + "public static int[][] resize(int[][] array2D) {\n", + " // count the number of non-zero rows in the original array\n", + " int numNonZeroRows = numNonZeroRows(array2D);\n", + " \n", + " // create a new 2D array with the same number of columns as the original array\n", + " // and the number of rows equal to the count of non-zero rows\n", + " int[][] resizedArray = new int[numNonZeroRows][array2D[0].length];\n", + " \n", + " // iterate over each row in the original array\n", + " // if it is a non-zero row, copy it to the new array\n", + " int row = 0;\n", + " for (int i = 0; i < array2D.length; i++) {\n", + " if (isNonZeroRow(array2D, i)) {\n", + " for (int j = 0; j < array2D[0].length; j++) {\n", + " resizedArray[row][j] = array2D[i][j];\n", " }\n", - " }\n", - " if (lights[row][col] && (numOn % 2 == 0)) {\n", - " return false;\n", - " } else if (!lights[row][col] && (numOn % 3 == 0)) {\n", - " return true;\n", - " } else {\n", - " return lights[row][col];\n", + " row++;\n", " }\n", " }\n", + " \n", + " // return the new array\n", + " return resizedArray;\n", "}" ] } @@ -330,7 +215,7 @@ "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", - "name": "java", + "name": "Java", "pygments_lexer": "java", "version": "11.0.12+8-LTS-237" },