Skip to content

Instantly share code, notes, and snippets.

View danielrobertson's full-sized avatar
☁️
Working on Cloudflare dashboard experiences

Daniel danielrobertson

☁️
Working on Cloudflare dashboard experiences
View GitHub Profile
@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);
}
}
@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 / 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;
}
boolean isPalindrome(Node n){
Node fast = n;
Node slow = n;
// push first half onto stack, then compare with second half
Stack<Integer> s = new Stack<Integer>();
while(fast != null && fast.next != null) {
s.push(slow.data);
slow = slow.next;
fast = fast.next.next;
@danielrobertson
danielrobertson / StringCompression.java
Created July 1, 2016 21:55
Cracking the Coding Chpt 1.6 String Compression
// aabccccaaa -> aabccccaaa
String compressString(String input) {
StringBuilder compressed = new StringBuilder();
int count = 1;
String[] arr = input.split("");
for(int i = 0; i < arr.length; i++) {
++count;
if(i + 1 >= arr.length || !arr[i + 1].equals(arr[i])) {
compressed.append(arr[i]);
compressed.append(Integer.toString(count));
@danielrobertson
danielrobertson / IsOneAway.java
Created July 1, 2016 20:26
Cracking the Coding Chpt 1.5 Is One Edit Away
// pale, ple -> true
// pales, pale -> true
// pale, bale -> true
// pale, bake -> false
boolean isOneAway(String a, String b) {
int aLength = a.length();
int bLength = b.length();
int shortIndex = 0;
int longIndex = 0;
@danielrobertson
danielrobertson / ISsPalindromePermutation.java
Created July 1, 2016 19:20
Cracking the Coding Chpt 1.4 PalindromePermutation
// palindrome will have even letter counts and at most one odd letter count
boolean isPalindromPermutation(String input) {
Map<String, Integer> frequency = new HashMap<String, Integer>();
for(String s : input.split("")) {
if(!frequency.containsKey(s)) {
frequency.put(s, 1);
} else {
frequency.put(s, 1 + frequency.get(s));
}
}
@danielrobertson
danielrobertson / IsPalindrome.java
Created July 1, 2016 18:54
is palindrome implementations
// using StringBuffer
boolean isPalindrome(String s) {
StringBuffer stringBuffer = new StringBuffer(s);
StringBuffer reverse = new StringBuffer(s);
reverse.reverse();
return stringBuffer.toString().equals(reverse.toString());
}
// in place
boolean isPalindrome(String input) {
@danielrobertson
danielrobertson / CaloricDeficitCalculator.js
Created May 5, 2016 04:07
Parses a CSV with daily calories in/out and calculates the average daily caloric deific
var fs = require('fs');
var parse = require('csv-parse');
var consumed = 0;
var burned = 0;
var numDays = 0;
var parser = parse({delimiter: ';'}, function(err, data){
data.forEach(function(data){
++numDays;
@danielrobertson
danielrobertson / ScanPorts
Created May 3, 2016 15:37
Find open ports
nmap -PN localhost
-----------------------
PORT STATE SERVICE
631/tcp open ipp
1023/tcp open netvenuechat