Skip to content

Instantly share code, notes, and snippets.

View jbranchaud's full-sized avatar

Josh Branchaud jbranchaud

View GitHub Profile
@jbranchaud
jbranchaud / GetSortedOccurrenceList.bash
Created December 14, 2011 17:19
Get a sorted list of unique items in a file with their number of occurrences.
#!/bin/bash
# Given a file, output a sorted list of unique items with the number of occurrences for each item next to it
fname=$1
cat $fname | sort | uniq -c
exit 0
@jbranchaud
jbranchaud / getFileContents
Created February 7, 2012 20:30
Read file contents into a char[]
public char[] getFileContents(File file) {
// char array to store the file contents in
char[] contents = null;
try {
// Read in the contents line by line storing them in a StringBuffer
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = "";
@jbranchaud
jbranchaud / createParser
Created February 7, 2012 21:09
Create an ASTParser (CompilationUnit) for the JDT using a char[]
public CompilationUnit createParser(char[] contents) {
// Create the ASTParser which will be a CompilationUnit
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(contents);
parser.setResolveBindings(true);
CompilationUnit parse = (CompilationUnit) parser.createAST(null);
return parse;
@jbranchaud
jbranchaud / RSVPCounter
Created August 24, 2012 19:17
Count people that have RSVP'd
cat rsvpresults.yaml | grep 'totalPeople' | sed 's/totalPeople: //' | awk '{s+=$1} END {print s}'
@jbranchaud
jbranchaud / wpcodesplitter
Created October 19, 2012 16:15
A quick function to split up/remove the <br /> tags when a WP sourcecode example gets auto-formatted.
import os
import sys
# add the offending text to file1.txt
f = open("file1.txt", 'r')
content = f.read()
contentArray = content.split('<br />')
for item in contentArray:
print item
# then copy/paste the console output
@jbranchaud
jbranchaud / validate_directory.py
Created November 5, 2012 04:37
A quick and tolerant directory validator for unix systems in Python
import os
"""
validate_directory: string -> string
given a string that represents a directory, this function will go through and
do some basic validation on it. If there is a problem with the directory as
given, it will be converted to the home directory and returned as is.
"""
def validate_directory(directory=None):
if directory == None or directory == '~' or not os.path.isdir(directory):
@jbranchaud
jbranchaud / PAGisEVIL
Created January 31, 2013 19:59
Solution for Parity Analysis
TYPE
StrSet = set(str)
StrLifted = lift(StrSet)
State = Var -> StrLifted
PROBLEM Constant_Propagation
direction : forward
carrier : State
@jbranchaud
jbranchaud / dafny-exercise0
Created March 11, 2013 01:09
Exercise 0 from Microsoft's Dafny tutorial on Rise4Fun.com
/*
* Write a method Max that takes two integer parameters and returns
* their maximum. Add appropriate annotations and make sure your code
* verifies.
*/
method Max(a: int, b:int) returns (c: int)
ensures c >= a && c >= b;
{
if (a > b) {
return a;
@jbranchaud
jbranchaud / dafny-exercise2
Created March 11, 2013 01:55
Exercise 2 from Microsoft's Dafny on Rise4Fun.com
/*
* Using a precondition, change Abs to say it can only be
* called on negative values. Simplify the body of Abs into
* just one return statement and make sure the method still
* verifies.
*/
method Abs(x: int) returns (y: int)
requires 0 > x;
ensures 0 <= y;
ensures 0 <= x ==> x == y;