diff --git a/_notebooks/2023-04-27-FRQs.ipynb b/_notebooks/2023-04-27-FRQs.ipynb index 2ffc2988..f78a4dd5 100644 --- a/_notebooks/2023-04-27-FRQs.ipynb +++ b/_notebooks/2023-04-27-FRQs.ipynb @@ -24,36 +24,97 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 42, "metadata": { "vscode": { "languageId": "java" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Check digit for 123456 is 7\n", + "Check digit for 9876543 is 8\n", + "1234562 is invalid\n", + "98765434 is invalid\n" + ] + } + ], "source": [ - "//////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", - "public static int getCheck(int num) {\n", - " int sum = 0;\n", - " int numDigits = getNumberOfDigits(num);\n", + "public class CheckDigit {\n", + " /** Returns the number of digits in num. */\n", + " public static int getNumberOfDigits(int num) {\n", + " if (num == 0) {\n", + " return 1;\n", + " }\n", + " int count = 0;\n", + " while (num != 0) {\n", + " count++;\n", + " num /= 10;\n", + " }\n", + " return count;\n", + " }\n", + "\n", + " /** Returns the nth digit of num.\n", + " * Precondition: n >= 1 and n <= the number of digits in num\n", + " */\n", + " public static int getDigit(int num, int n) {\n", + " int numDigits = getNumberOfDigits(num);\n", + " int index = numDigits - n;\n", + " for (int i = 0; i < index; i++) {\n", + " num /= 10;\n", + " }\n", + " return num % 10;\n", + " }\n", + "\n", + " //////////////////// Part A \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + " public static int getCheck(int num) {\n", + " int sum = 0;\n", + " int numDigits = getNumberOfDigits(num);\n", + "\n", + " for (int i = 1; i <= numDigits; i++) {\n", + " int factor = numDigits - i + 2;\n", + " sum += factor * getDigit(num, i);\n", + " }\n", "\n", - " for (int i = 1; i <= numDigits; i++) {\n", - " int factor = numDigits - i + 2;\n", - " sum += factor * getDigit(num, i);\n", + " int checkDigit = sum % 10;\n", + " return checkDigit;\n", " }\n", "\n", - " int checkDigit = sum % 10;\n", - " return checkDigit;\n", + " //////////////////// Part B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n", + " public static boolean isValid(int numWithCheckDigit) {\n", + " int num = numWithCheckDigit / 10; // Get rid of the check digit\n", + " int checkDigit = numWithCheckDigit % 10; // Get the check digit\n", + " \n", + " return getCheck(num) == checkDigit; // Check if the computed check digit matches the given check digit\n", + " } \n", + " \n", + " public static void main(String[] args) {\n", + " int num1 = 123456;\n", + " int check1 = getCheck(num1);\n", + " System.out.println(\"Check digit for \" + num1 + \" is \" + check1);\n", + " // expected output: \"Check digit for 123456 is 2\"\n", + " \n", + " int num2 = 9876543;\n", + " int check2 = getCheck(num2);\n", + " System.out.println(\"Check digit for \" + num2 + \" is \" + check2);\n", + " // expected output: \"Check digit for 9876543 is 4\"\n", + " \n", + " int numWithCheck1 = 1234562;\n", + " boolean isValid1 = isValid(numWithCheck1);\n", + " System.out.println(numWithCheck1 + \" is \" + (isValid1 ? \"valid\" : \"invalid\"));\n", + " // expected output: \"1234562 is invalid\"\n", + " \n", + " int numWithCheck2 = 98765434;\n", + " boolean isValid2 = isValid(numWithCheck2);\n", + " System.out.println(numWithCheck2 + \" is \" + (isValid2 ? \"valid\" : \"invalid\"));\n", + " // expected output: \"98765434 is invalid\"\n", + " }\n", "}\n", "\n", - "//////////////////// 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", - "}" + "CheckDigit.main(null);" ] }, { @@ -66,13 +127,25 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 43, "metadata": { "vscode": { "languageId": "java" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current number: 0\n", + "Current number: 2\n", + "Current number: 4\n", + "Current number: 6\n", + "Current number: 4\n" + ] + } + ], "source": [ "public class AdditionPattern {\n", " private int current;\n", @@ -98,7 +171,28 @@ " current -= increment;\n", " }\n", " }\n", - "}" + "}\n", + "\n", + "public class AdditionPatternTester {\n", + " public static void main(String[] args) {\n", + " AdditionPattern pattern = new AdditionPattern(0, 2);\n", + " System.out.println(\"Current number: \" + pattern.currentNumber()); // Expected output: 0\n", + " \n", + " pattern.next();\n", + " System.out.println(\"Current number: \" + pattern.currentNumber()); // Expected output: 2\n", + " \n", + " pattern.next();\n", + " System.out.println(\"Current number: \" + pattern.currentNumber()); // Expected output: 4\n", + " \n", + " pattern.next();\n", + " System.out.println(\"Current number: \" + pattern.currentNumber()); // Expected output: 6\n", + " \n", + " pattern.prev();\n", + " System.out.println(\"Current number: \" + pattern.currentNumber()); // Expected output: 4\n", + " }\n", + "}\n", + "\n", + "AdditionPatternTester.main(null);" ] }, { @@ -111,42 +205,140 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 44, "metadata": { "vscode": { "languageId": "java" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current schedule:\n", + "0 0\n", + "1 1\n", + "2 2\n", + "Available mechanics:\n", + "3 4 5 \n", + "Updated schedule:\n", + "0 0\n", + "2 2\n" + ] + } + ], "source": [ - "//////////////////// 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", + "import java.util.ArrayList;\n", + "\n", + "public class RepairSchedule {\n", + " /** Each element represents a repair by an individual mechanic in a bay. */\n", + " private ArrayList schedule;\n", + "\n", + " /** Number of mechanics available in this schedule. */\n", + " private int numberOfMechanics;\n", + " private int mechanicNum;\n", + " private int bayNum;\n", + "\n", + " /** Constructs a RepairSchedule object.\n", + " * Precondition: n >= 0\n", + " */\n", + " public RepairSchedule(int n, int m, int b) {\n", + " schedule = new ArrayList();\n", + " this.numberOfMechanics = n;\n", + " this.mechanicNum = m;\n", + " this.bayNum = b;\n", + " }\n", + " \n", + " public int getMechanicNum() {\n", + " return mechanicNum;\n", + " }\n", + " \n", + " public int getBayNum() {\n", + " return bayNum;\n", + " }\n", + "\n", + " /** Attempts to schedule a repair by a given mechanic in a given bay as described in part (a).\n", + " * Precondition: 0 <= m < numberOfMechanics and b >= 0\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", " }\n", + " schedule.add(new CarRepair(m, b));\n", + " return true;\n", " }\n", - " schedule.add(new CarRepair(m, b));\n", - " return true;\n", - "}\n", "\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", + " /** Returns an ArrayList containing the mechanic identifiers of all available mechanics,\n", + " * as described in part (b).\n", + " */\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", + " if (isAvailable) {\n", + " availableMechanics.add(i); // add the available mechanic to the list\n", " }\n", " }\n", - " if (isAvailable) {\n", - " availableMechanics.add(i); // add the available mechanic to the list\n", + " return availableMechanics; // return the list of available mechanics\n", + " }\n", + "\n", + " /** Removes an element from schedule when a repair is complete. */\n", + " public void carOut(int b) {\n", + " for (int i = 0; i < schedule.size(); i++) {\n", + " if (schedule.get(i).getBayNum() == b) {\n", + " schedule.remove(i);\n", + " break;\n", + " }\n", " }\n", " }\n", - " return availableMechanics; // return the list of available mechanics\n", - "}" + "\n", + " public static void main(String[] args) {\n", + " // Create a new repair schedule with 3 mechanics\n", + " RepairSchedule schedule = new RepairSchedule(3, 4, 4);\n", + " \n", + " // Add some repairs to the schedule\n", + " schedule.addRepair(0, 0);\n", + " schedule.addRepair(1, 1);\n", + " schedule.addRepair(2, 2);\n", + " schedule.addRepair(0, 3);\n", + " schedule.addRepair(1, 4);\n", + " \n", + " // Print out the schedule\n", + " System.out.println(\"Current schedule:\");\n", + " for (CarRepair repair : schedule.schedule) {\n", + " System.out.println(repair.getMechanicNum() + \" \" + repair.getBayNum());\n", + " }\n", + " \n", + " // Print out the available mechanics\n", + " ArrayList availableMechanics = schedule.availableMechanics();\n", + " System.out.println(\"Available mechanics:\");\n", + " for (int i : availableMechanics) {\n", + " System.out.print(i + \" \");\n", + " }\n", + " \n", + " // Mark a repair as complete\n", + " schedule.carOut(1);\n", + " \n", + " // Print out the updated schedule\n", + " System.out.println(\"\\nUpdated schedule:\");\n", + " for (CarRepair repair : schedule.schedule) {\n", + " System.out.println(repair.getMechanicNum() + \" \" + repair.getBayNum());\n", + " }\n", + " } \n", + "}\n", + "\n", + "RepairSchedule.main(null);" ] }, { @@ -159,48 +351,115 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 45, "metadata": { "vscode": { "languageId": "java" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "false\n", + "true\n", + "Original array:\n", + "0 0 0 \n", + "1 2 3 \n", + "0 0 0 \n", + "4 5 6 \n", + "0 0 0 \n", + "Resized array:\n", + "1 2 3 \n", + "4 5 6 \n" + ] + } + ], "source": [ - "//////////////////// 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", + "public class ArrayResizer {\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", + " return true;\n", " }\n", - " return true;\n", - "}\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", + " public static int numNonZeroRows(int[][] array2D) {\n", + " int count = 0;\n", + " for (int i = 0; i < array2D.length; i++) {\n", + " if (isNonZeroRow(array2D, i)) {\n", + " count++;\n", " }\n", - " row++;\n", " }\n", + " return count;\n", + " } \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", + " row++;\n", + " }\n", + " }\n", + " \n", + " // return the new array\n", + " return resizedArray;\n", " }\n", - " \n", - " // return the new array\n", - " return resizedArray;\n", - "}" + "\n", + " public static void main(String[] args) {\n", + " int[][] array = {\n", + " {0, 0, 0},\n", + " {1, 2, 3},\n", + " {0, 0, 0},\n", + " {4, 5, 6},\n", + " {0, 0, 0}\n", + " };\n", + "\n", + " // test isNonZeroRow\n", + " System.out.println(isNonZeroRow(array, 0)); // false\n", + " System.out.println(isNonZeroRow(array, 1)); // true\n", + "\n", + " // test resize\n", + " int[][] resizedArray = resize(array);\n", + "\n", + " // print original array\n", + " System.out.println(\"Original array:\");\n", + " for (int[] row : array) {\n", + " for (int val : row) {\n", + " System.out.print(val + \" \");\n", + " }\n", + " System.out.println();\n", + " }\n", + "\n", + " // print resized array\n", + " System.out.println(\"Resized array:\");\n", + " for (int[] row : resizedArray) {\n", + " for (int val : row) {\n", + " System.out.print(val + \" \");\n", + " }\n", + " System.out.println();\n", + " }\n", + " }\n", + "}\n", + "\n", + "ArrayResizer.main(null);" ] } ],