Skip to content

Instantly share code, notes, and snippets.

View danielrobertson's full-sized avatar
☁️

Daniel danielrobertson

☁️
View GitHub Profile
@danielrobertson
danielrobertson / NumberOfPalindromicSubstrings.java
Last active September 22, 2015 01:42
Given a string, print the number of palindromic substrings
/**
* Created by daniel robertson on 9/6/15.
*/
import java.io.*;
import java.util.*;
import java.lang.StringBuffer;
public class NumberOfPalindromicSubstrings {
static int palindrome(String str) {
int result = 0;
@danielrobertson
danielrobertson / isPalindrome.java
Created September 22, 2015 01:44
Return true if the string is palindrome, false otherwise
boolean isPalindrome(String s) {
for(int front = 0, end = s.length() - 1; front <= end; front++, end--) {
if(s.charAt(front) != s.charAt(end)) {
return false;
}
}
return true;
}
@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) {
@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"
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 / 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;
@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 / 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 / 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') {