forked from nighthawkcoders/student
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
011ee6a
commit ba7955a
Showing
1 changed file
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"title: FRQ 3\n", | ||
"description: Third FRQ for collegeboard FRQ final\n", | ||
"toc: true\n", | ||
"layout: post\n", | ||
"courses: { ToC: {week: 23} }\n", | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 43, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "java" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"1 4 4\n", | ||
"2 0 1\n", | ||
"3 1 -9\n", | ||
"1 1 5\n", | ||
"new col3\n", | ||
"2 0 1\n", | ||
"1 3 4\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"public class Main{\n", | ||
" public static void main (String args[]){\n", | ||
" SparseArray arr = new SparseArray();\n", | ||
" // System.out.println(arr.getValueAt(3, 1));\n", | ||
" arr.printEntries();\n", | ||
" arr.removeColumn(1);\n", | ||
" arr.printEntries();\n", | ||
" // System.out.println(arr.getValueAt(3, 1));\n", | ||
" // System.out.println(arr.getValueAt(1, 3));\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"public class SparseArrayEntry{\n", | ||
" private int row;\n", | ||
" private int col;\n", | ||
" private int value;\n", | ||
"\n", | ||
" public SparseArrayEntry(int r, int c, int v){\n", | ||
" row = r;\n", | ||
" col = c;\n", | ||
" value = v;\n", | ||
" }\n", | ||
" public int getRow(){\n", | ||
" return row;\n", | ||
" }\n", | ||
"\n", | ||
" public int getCol(){\n", | ||
" return col;\n", | ||
" }\n", | ||
"\n", | ||
" public int getValue(){\n", | ||
" return value;\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"public class SparseArray{\n", | ||
" private int numRows;\n", | ||
" private int numCols;\n", | ||
" private List<SparseArrayEntry> entries;\n", | ||
"\n", | ||
" public SparseArray(){\n", | ||
" entries = new ArrayList<SparseArrayEntry>();\n", | ||
" entries.add(new SparseArrayEntry(1, 4, 4));\n", | ||
" entries.add(new SparseArrayEntry(2, 0, 1));\n", | ||
" entries.add(new SparseArrayEntry(3, 1, -9));\n", | ||
" entries.add(new SparseArrayEntry(1, 1, 5));\n", | ||
" }\n", | ||
"\n", | ||
" public int getNumRows(){\n", | ||
" return numRows;\n", | ||
" }\n", | ||
"\n", | ||
" public int getNumCols(){\n", | ||
" return numCols;\n", | ||
" }\n", | ||
"\n", | ||
" public int getValueAt(int row, int col){\n", | ||
" for (SparseArrayEntry e : entries){\n", | ||
" if (e.getRow() == row && e.getCol() == col){\n", | ||
" return e.getValue();\n", | ||
" }\n", | ||
" }\n", | ||
" return 0;\n", | ||
" }\n", | ||
"\n", | ||
" public void printEntries(){\n", | ||
" for (SparseArrayEntry e : entries){\n", | ||
" System.out.println(String.valueOf(e.getRow()) + \" \" + String.valueOf(e.getCol()) + \" \" + String.valueOf(e.getValue()));\n", | ||
" }\n", | ||
" }\n", | ||
"\n", | ||
" public void removeColumn(int col){\n", | ||
" numCols --;\n", | ||
" ArrayList<SparseArrayEntry> removeList = new ArrayList<SparseArrayEntry>();\n", | ||
" int iterator = entries.size();\n", | ||
" for (int i = 0; i < iterator; i ++){\n", | ||
" if (entries.get(i).getCol() == col){\n", | ||
" removeList.add(entries.get(i));\n", | ||
" }\n", | ||
" else if (entries.get(i).getCol() > col){\n", | ||
" System.out.println(\"new col\" + String.valueOf(entries.get(i).getCol() - 1));\n", | ||
" entries.add(new SparseArrayEntry(entries.get(i).getRow(), entries.get(i).getCol() - 1, entries.get(i).getValue()));\n", | ||
" removeList.add(entries.get(i));\n", | ||
" }\n", | ||
" }\n", | ||
" for (SparseArrayEntry e : removeList){\n", | ||
" entries.remove(e);\n", | ||
" }\n", | ||
" }\n", | ||
"}\n", | ||
"Main.main(null);" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Learnings:\n", | ||
"\n", | ||
"1. You can't remove an entry in a list while iterating over it like using an enhanced for loop. Instead you can use an iterator.\n", | ||
"2. to get the length of an arraylist is use .size()\n", | ||
"3. using the size of a list to iterate until is dynamic, so it changes as the list gets bigger. Makes sense because the check is rerun every time." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Java", | ||
"language": "java", | ||
"name": "java" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "java", | ||
"file_extension": ".jshell", | ||
"mimetype": "text/x-java-source", | ||
"name": "Java", | ||
"pygments_lexer": "java", | ||
"version": "20.0.2+9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |