Skip to content

Instantly share code, notes, and snippets.

View danielrobertson's full-sized avatar
☁️

Daniel danielrobertson

☁️
View GitHub Profile
@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 / 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 / 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 / ScanPorts
Created May 3, 2016 15:37
Find open ports
nmap -PN localhost
-----------------------
PORT STATE SERVICE
631/tcp open ipp
1023/tcp open netvenuechat
@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 / 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 / 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 / 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 / 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));
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;