This file contains hidden or 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
| Node reverse(Node n) { | |
| // maintain previous node, rewriting all pointers to previous | |
| Node prev = null; | |
| while(n != null) { | |
| Node next = n.next; | |
| n.next = prev; | |
| prev = n; | |
| n = next; | |
| } |
This file contains hidden or 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
| // Determine if a binary tree is a binary search tree | |
| boolean isBst(Node r) { | |
| return isBst(r, null, null); | |
| } | |
| boolean isBst(Node r, Integer min, Integer max) { | |
| if(r == null) | |
| return true; | |
| if((min != null && r.data < min) || (max != null && r.data > max)) |
This file contains hidden or 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
| void dfs(Node r) { | |
| if(r == null) | |
| return; | |
| visit(r); | |
| r.visited = true; // avoid cycles | |
| for(Node n : r.adjacent) { | |
| if(!n.visited) | |
| dfs(n); | |
| } | |
| } |
This file contains hidden or 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
| boolean isPrime(int n) { | |
| if(n < 2) return false; | |
| for(int i = 2; i <= Math.sqrt(n); ++i) { | |
| if(n % i == 0) return false; | |
| } | |
| return true; | |
| } |
This file contains hidden or 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
| int binarySearch(int[] a, int x) { | |
| int low = 0; | |
| int high = a.length - 1; | |
| int mid; | |
| while (low <= high) { | |
| mid = (low + high) | |
| if (a[mid] < x) { | |
| low = mid + 1; | |
| } else if(a[mid]>x){ |
This file contains hidden or 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
| void mergesort(int[] array) { | |
| int[] helper = new int[array.length]; | |
| mergesort(array, helper, 0, array.length - 1); | |
| } | |
| void mergesort(int[] array, int[] helper, int low, int high) { | |
| if (low < high) { | |
| int middle = (low + high) / 2; | |
| mergesort(array, helper, low, middle); // left | |
| mergesort(array, helper, middle + 1, high); // right |
This file contains hidden or 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
| void quickSort(int arr[], int left, int right) { //Confirmed | |
| int index = partition(arr, left, right); | |
| if (left < index - 1) { //when sorting 2 3 1, will return 2 1 3, left point at 3 and 2 1 unsorted | |
| quickSort(arr, left, index - 1); | |
| } | |
| if (index < right) { //when sorting 3 1 2, will return 1 3 2, index at 3 and right is 2. | |
| quickSort(arr, index, right); | |
| } | |
| } | |
This file contains hidden or 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
| void radixSort(int arr[], int maxDigits) { | |
| int exp = 1; | |
| for (int i = 0; i < maxDigits; i++) { | |
| ArrayList bucketList[] = new ArrayList[10]; | |
| for (int k = 0; k < 10; k++) { | |
| bucketList[k] = new ArrayList(); | |
| } | |
This file contains hidden or 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
| const firebase = require("firebase-admin"); | |
| const serviceAccount = require("./firebase-config.json"); // from the Firebase project overview page | |
| firebase.initializeApp({ | |
| credential: firebase.credential.cert(serviceAccount), | |
| databaseURL: "https://<PROJECT_NAME>.firebaseio.com" | |
| }); | |
| // this code could theoretically run in a Cloud Function as the back-end to a volunteer filling out a form, | |
| // or scraping a 3rd party animal database, which will spin through the records and create entries in Firestore |
This file contains hidden or 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
| /* | |
| Input: | |
| an n by m grid of "alive" or "dead" cells | |
| Output: | |
| a transformation on the input grid using the following rules: | |
| - An "alive" cell remains alive if 2 or 3 neighbors are "alive." Otherwise, it becomes "dead." | |
| - A "dead" cell becomes alive if exactly 3 neighbors are "alive." Otherwise, it remains "dead." | |
| (The term "neighbor" refers to the at-most-8 adjacent cells horizontally, vertically, and diagonally.) |