Skip to content

Instantly share code, notes, and snippets.

View danielrobertson's full-sized avatar
☁️

Daniel danielrobertson

☁️
View GitHub Profile
@danielrobertson
danielrobertson / MaxSubarray.py
Created March 9, 2016 16:28
Maximum Contiguous Subarray
def maxSubarray(numbers):
currentMax = 0
totalMax = 0
for n in numbers:
currentMax = max(0, currentMax + n)
totalMax = max(totalMax, currentMax)
return totalMax
numbers = [-10, 2,2,-4,2,3,4,-5]
print(str(maxSubarray(numbers))) # 9
@danielrobertson
danielrobertson / bash_profile
Last active February 7, 2019 21:03
Drop this in your ~/.bash_profile
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# PS1 prompt
export PS1="\w\[\033[32m\]\$(parse_git_branch)\[\033[00m\]$ "
@danielrobertson
danielrobertson / GetRelevantTweets.py
Created November 17, 2015 16:57
Given a search term or hashtag, return relevant and recent Tweets using the Twitter Search API
# prereqs:
# Create a Twitter app and generate oauth credentials at https://apps.twitter.com
# python 2.7
# pip install oauth2
import oauth2 as oauth
import json
import urllib
CONSUMER_KEY = 'your consumer key'
@danielrobertson
danielrobertson / HttpJsonApiServer.js
Created November 16, 2015 02:26
An HTTP server with two resources that accept date in ISO format and return HHMMSS or milliseconds in JSON format
var http = require('http');
var url = require('url');
var port = process.argv[2];
http.createServer(function (request, response) {
urlInfo = url.parse(request.url, true);
iso = urlInfo.query['iso'];
response.writeHead(200, {'Content-Type': 'application/json'});
if (urlInfo.pathname === '/api/parsetime') {
@danielrobertson
danielrobertson / HttpUppercaser.js
Created November 15, 2015 22:51
An HTTP server that accepts a POST request, then returns the contents of the POST request uppercased
var http = require('http');
var map = require('through2-map');
var port = process.argv[2];
var server = http.createServer(function (request, response) {
if (request.method = 'POST') {
request.pipe(map(function (chunk) {
return chunk.toString().toUpperCase();
})).pipe(response);
}
@danielrobertson
danielrobertson / TimeServer.js
Created November 15, 2015 20:01
A TCP server that listens on the port given in command line arguments and writes the timestamp of connection to the connection
var net = require('net');
var port = process.argv[2];
var strftime = require('strftime');
var server = net.createServer(function (socket) {
var timestamp = strftime('%F %H:%M', new Date());
console.log(timestamp);
socket.write(timestamp);
socket.end();
});
@danielrobertson
danielrobertson / HttpCollect.js
Created November 15, 2015 18:25
Nodeschool.io's learnyounode HTTP Collect
var http = require('http');
var url = process.argv[2];
http.get(url, function (response) {
response.setEncoding('utf8');
response.on('error', console.error);
var allCharacters = '';
response.on('data', function (data) {
allCharacters += data;
int getEditDistance(String a, String b) {
int[][] distances = new int[a.length()][b.length()];
for(int i = 0; i < distances.length; i++) {
distances[i][0] = i;
}
for(int j = 0; j < distances[0].length; j++) {
distances[0][j] = j;
}
@danielrobertson
danielrobertson / Rotation.js
Created October 7, 2015 15:17
Are two strings a rotation of one another
var isRotation = function(s1, s2) {
return (s1.length === s2.length) && ((s1 + s1).indexOf(s2) !== -1);
};
s1 = "stackoverflow"
s2 = "tackoverflows"
s3 = "ackoverflowst"
s4 = "overflowstack"
s5 = "ovegflgwsgacg"
@danielrobertson
danielrobertson / maxDepthOfBTree.java
Created September 27, 2015 19:50
Find the maximum depth of a binary tree using depth-first search
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public int maxDepth(TreeNode root) {