Skip to content

Instantly share code, notes, and snippets.

@zachelko
zachelko / bounded_remove_if.cpp
Last active August 29, 2015 14:00
Variation on std::remove_if which respects a bound for the number of values to remove
#include <cassert>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <vector>
template <typename T, typename BinaryPredicate>
void bounded_remove_if(T &values,
BinaryPredicate predicate,
@zachelko
zachelko / post-commit.py
Last active August 29, 2015 13:57
git post-commit hook to tweet your commit messages using twidge
#!/usr/bin/python3.3
import sys
import subprocess
# The git hook will invoke this script with some data in STDIN
# So, let's re-assign stdin to /dev/tty so we can grab user input
# and decide whether or not to tweet this commit
sys.stdin = open('/dev/tty')
shouldTweet = input('Tweet this commit? y/n: ')
if shouldTweet != "y" and shouldTweet != "Y":
@zachelko
zachelko / findOccurrencesOfMostCommonSubstring.cpp
Last active June 27, 2016 08:10
Find the number of times the most-common substring occurs in a given string
/* Given an input string of size n, return the number of occurrences
* of the most common substring between lengths k,l.
*/
int findOccurrencesOfMostCommonSubstring(int n, int k, int l, const string &input)
{
std::map<std::string, int> substringMap;
int maxOccurrences = 1;
// 1. Build every legal substring with lengths between k and l
// 2. Place into map
@zachelko
zachelko / dofind.sh
Created January 31, 2014 17:31
Recursively search for text in files based on file-extension from a given starting directory (defaults to cwd)
#!/bin/bash
#
# Sample usage:
# Find "QObject" in every .hpp / .cpp file located somewhere within /home/zach/coderoot
# dofind.sh "QObject" hs /home/zach/coderoot
#
# Same as above, but begin the search in the current directory, and only check .cpp files
# dofind.sh "QObject" s
#
# Same as above, but specify the file extension manually
@zachelko
zachelko / pidfromname.sh
Last active August 29, 2015 13:55
Obtain PID of a given process name. Can then pipe it into other commands as needed.
#!/bin/bash
ps aux | grep -m 1 $1 | awk '{print $2}'
public static String reverse(String str)
{
int startIndex = 0;
int stopIndex = str.length() - 1;
char[] chArray = str.toCharArray();
while (startIndex < stopIndex)
{
char temp = chArray[stopIndex];
chArray[stopIndex] = chArray[startIndex];
chArray[startIndex] = temp;
def fibonacci(nthTerm):
if nthTerm < 2:
return nthTerm
else:
return fibonacci(nthTerm - 1) + fibonacci(nthTerm - 2)
def findLargestIterative(theList):
if len(theList) > 0:
largest = theList[0]
for item in theList:
if item > largest:
largest = item
return largest
else:
return None
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
int main(const int argc, const char* argv[])
{
if (argc != 2)
{
def printOdds(startVal, endVal):
for odd in range(startVal, endVal + 1, 2):
print(str(odd))