Skip to content

Instantly share code, notes, and snippets.

View NicMcPhee's full-sized avatar

Nic McPhee NicMcPhee

View GitHub Profile
@NicMcPhee
NicMcPhee / CoinToss.ex
Last active November 3, 2015 23:19
Cryptographically fair coin toss in Elixir
# Person A flips and Person B chooses "heads" or "tails".
# Person B wins if their choice matches the flip, otherwise
# person A wins.
defmodule CoinToss do
def run_simulation(num_people) do
counterPid = spawn(__MODULE__, :counter_loop, [0, 0])
Process.register(counterPid, :counter)
people = create_people(num_people)
@NicMcPhee
NicMcPhee / ConvertingSignedBytes.java
Created March 31, 2015 23:53
A little example showing how to convert Java's signed bytes to the appropriate unsigned integer value.
public class ConvertingSignedBytes {
public static void main(String[] args) {
byte x = (byte) 37;
byte y = (byte) 150;
printByte(x);
printByte(y);
}
public static void printByte(byte b) {
int value;
@NicMcPhee
NicMcPhee / plotting_evolved_sine_functions_with_gorilla_repl.clj
Last active August 29, 2015 14:08
An example of using Gorilla Repl and Clojure to plot some evolved approximations to the sine function. The evolution was done with the ECJ system (http://cs.gmu.edu/~eclab/projects/ecj/). I'd never used Gorilla Repl, and I was really impressed with how easy it was to get it up and running and generate some basic sketches with. If you want to see…
;; gorilla-repl.fileformat = 1
;; **
;;; # Plotting some evolved sine results
;;;
;;; I decided to play with [Gorilla Repl](http://gorilla-repl.org) to try to plot some of the evolved Sine results just to make sure we were solving the problem we thought we were solving.
;;;
;;; The short version is that Gorilla Repl was quite excellent, and we are indeed solving the sine problem :-)
;;;
;;; One of the nifty things is that Clojure allowed me to define the symbol % to be protected division, so we can paste in functions straight from ECJ and run them without having to do any annoying search/replace action to get things in a format that our graphing tool can process.
@NicMcPhee
NicMcPhee / Counter.java
Last active July 14, 2019 14:53
A simple example of a successful use of Java synchronization to eliminate race conditions. The "synchronized" keyword on Counter.incrementCount() is crucial here; removing it will lead to serious race conditions on multiple-core computers. Having it ensures that two threads can't enter this method at the same time, making sure that only one thre…
public class Counter {
private int count = 0;
public int getCount() {
return count ;
}
/**
* The "synchronized" keyword is crucial here; removing it will lead to serious
@NicMcPhee
NicMcPhee / IncorrectSynchronizedIncrement.java
Created November 20, 2013 17:24
An simple example of an *incorrect* use of Java synchronization. Synchronizing the incrementCount() method doesn't actually fix the race condition because synchronization happens on a per *object* basis and we have multiple separate objects, each running in a separate thread, so the synchronization really accomplishes nothing here.
public class IncorrectSynchronizedIncrement implements Runnable {
private static final int NUM_THREADS = 4;
private static final int NUM_INCREMENTS = 10000;
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[NUM_THREADS];
for (int i=0; i<NUM_THREADS; ++i) {
@NicMcPhee
NicMcPhee / SimpleIncrement.java
Created November 20, 2013 16:42
A simple example that illustrates the existence of race conditions when using Java Threads. If you run this code on a multi-core box you're likely to get a lower count (potentially much lower) than you might naively expect.
public class SimpleIncrement implements Runnable {
private static final int NUM_THREADS = 4;
private static final int NUM_INCREMENTS = 10000;
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[NUM_THREADS];
for (int i=0; i<NUM_THREADS; ++i) {
@NicMcPhee
NicMcPhee / Employees.xml
Created October 24, 2013 04:40
A simple example of reading and parsing an XML document in Java. This is based substantially on http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html but cleaned up some so there's less duplicate logic. It assumes the existence of a file called "Employees.xml" which is also included here.
<?xml version="1.0"?>
<company>
<employee>
<firstname>Tom</firstname>
<lastname>Cruise</lastname>
</employee>
<employee>
<firstname>Paul</firstname>
<lastname>Enderson</lastname>
</employee>
@NicMcPhee
NicMcPhee / GzipReader_TarReader_example.rb
Last active December 23, 2015 12:59
This is intended as an example of how to use GzipReader and TarReader to process a collection of gzipped tar files (.tgz files or .tar.gz files). Searching (as of Sept 2013) for documentation of these libraries and examples of their use isn't terribly satisfactory, so I thought I'd write up an example in the hopes that people find it helpful.
require 'rubygems/package'
require 'zlib'
# This is intended as an example of how to use GzipReader and TarReader to process a
# collection of gzipped tar files (.tgz files or .tar.gz files). Searching (as of Sept 2013)
# for documentation of these libraries and examples of their use isn't terribly satisfactory,
# so I thought I'd write up an example in the hopes that people find it helpful.
# This assumes that a set of GZipped Tar files are provided as command line arguments,
# and that these zipped tar files contain log data that includes (among other things)
@NicMcPhee
NicMcPhee / RomanNumeralConverter.groovy
Last active December 16, 2015 21:20
A finished solution to the roman numeral kata (http://codingdojo.org/cgi-bin/wiki.pl?KataRomanNumerals) from the 24 April and 1 May 2013 coding dojos at the University of Minnesota, Morris. This works, but it really isn't terribly readable and we'd like to do something to improve that.
class RomanNumeralConverter {
def mappings = [[[1000 , "M"], [500 , "D"], [100 , "C"]],
[[100 , "C"], [50 , "L"], [10 , "X"]],
[[10 , "X"], [5 , "V"], [1 , "I"]]]
def convert(integer) {
String result = ""
def pair = handleDigit("M","D", "C", 100, result, integer)
@NicMcPhee
NicMcPhee / DictionaryReplacer.groovy
Last active December 16, 2015 01:59
Unfinished solution to the dictionary replacer kata (http://codingdojo.org/cgi-bin/wiki.pl?back=KataDictionaryReplacer) from the 10 April 2013 coding dojo at the University of Minnesota, Morris. We got a number of good tests written and can pass everything that just has a single tagged term, but time ran out before we got to dealing with the mor…
class DictionaryReplacer {
def replace(string, dictionary){
def returnValue
if (string.size() < 2){
returnValue = string
}
else if (string[0]!='$'){
def firstDollar = string.indexOf('$')
if (firstDollar == -1) {