Skip to content

Instantly share code, notes, and snippets.

View danielrobertson's full-sized avatar
☁️

Daniel danielrobertson

☁️
View GitHub Profile
@danielrobertson
danielrobertson / ReverseLinkedList.java
Created July 7, 2016 19:43
Reverse a singly linked list
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;
}
@danielrobertson
danielrobertson / IsBst.java
Created July 10, 2016 00:21
Check is BST
// 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))
@danielrobertson
danielrobertson / TreeTraversal.java
Created July 10, 2016 00:25
Tree searches - BST and DFS
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);
}
}
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;
}
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){
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
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);
}
}
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();
}
@danielrobertson
danielrobertson / firestoreUpload.js
Last active June 13, 2018 15:48
How to upload a list of JSON documents to Google Firestore. This script puts documents into a collection named "animals"
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
@danielrobertson
danielrobertson / conway.js
Created February 12, 2019 19:09
Conway's game of life
/*
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.)