Skip to content

Instantly share code, notes, and snippets.

View mikedugan's full-sized avatar

Mike Dugan mikedugan

View GitHub Profile
@mikedugan
mikedugan / caesar.cs
Created January 4, 2014 00:42
the caesar cipher transforms the string by replacing it with the value at offset n from the char in the string array. nifty.
static string Caesar(string value, int shift)
{
char[] buffer = value.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
// Letter.
char letter = buffer[i];
// Add shift to all.
letter = (char)(letter + shift);
// Subtract 26 on overflow.
@mikedugan
mikedugan / atbash.cs
Created January 4, 2014 00:41
the atbash cipher replaces each character in a string with its inverse - a becomes z, b becomes y, etc
class AtbashTable
{
/// <summary>
/// Lookup table to shift characters.
/// </summary>
char[] _shift = new char[char.MaxValue];
/// <summary>
/// Generates the lookup table.
/// </summary>
@mikedugan
mikedugan / fibonacci.cs
Created January 4, 2014 00:37
iterative C# implementation of a Fibonacci sequence
public static int Fibonacci(int n)
{
int a = 0;
int b = 1;
// In N steps compute Fibonacci sequence iteratively.
for (int i = 0; i < n; i++)
{
int temp = a;
a = b;
b = temp + b;
@mikedugan
mikedugan / palindrome.cs
Created January 4, 2014 00:34
not incredibly useful, but a basic method to determine if a string is a palindrome
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
@mikedugan
mikedugan / fisher-yates.cs
Created January 4, 2014 00:32
a simple C# implementation of the fisher-yates shuffle used to randomize array elements without bias
public static void Shuffle<T>(T[] array)
{
var random = _random;
for (int i = array.Length; i > 1; i--)
{
// Pick random element to swap.
int j = random.Next(i); // 0 <= j <= i-1
// Swap.
T tmp = array[j];
array[j] = array[i - 1];
@mikedugan
mikedugan / dijkstra.cs
Created January 3, 2014 05:29
Dijkstra's algorithm
class Dijkstra
{
private int rank = 0;
private int[,] L;
private int[] C;
public int[] D;
private int trank = 0;
public Dijkstra(int paramRank,int [,]paramArray)
{
L = new int[paramRank, paramRank];
@mikedugan
mikedugan / box-mueller.cs
Created January 3, 2014 05:15
Box-Mueller method
public static double NextGaussianDouble(this Random r)
{
double U, u, v, S;
do
{
u = 2.0 * r.NextDouble() - 1.0;
v = 2.0 * r.NextDouble() - 1.0;
S = u * u + v * v;
}
@mikedugan
mikedugan / stdev.cs
Created January 3, 2014 05:12
StDev implementations in C#
using System;
using System.Collections.Generic;
namespace SampleApp
{
internal class Program
{
private static void Main()
{
List<double> data = new List<double> {1, 2, 3, 4, 5, 6};
@mikedugan
mikedugan / Levenshtein.cs
Created January 3, 2014 05:07 — forked from wickedshimmy/Levenshtein.cs
the Levenshtein algorithm provides a simple edit-distance calculation.
using System;
using System.Linq;
using NUnit.Framework;
[TestFixture]
public class Levenshtein {
public static int EditDistance (string original, string modified)
{
if (original == modified)
@mikedugan
mikedugan / geocode.jquery.js
Last active December 31, 2015 21:19
Google GeoCode request builder
function buildApiRequest() {
var address = $('#street1').value.replace(/ /gi, '+') + ',+';
address += $('#city').value.replace(/ /gi, '+') + ',+';
return 'http://maps.googleapis.com/maps/api/geocode/json?address=' + address + $('#state').value + '&sensor=false';
}