Skip to content

Instantly share code, notes, and snippets.

@dgroft
dgroft / Math.cs
Created November 6, 2012 14:11
Determine if two floats are "equal"
using System.Linq;
public static class Math
{
/// <summary>
/// Floating-point epsilon value.
/// </summary>
public const float FLT_EPSILON = 1.192092896e-07f;
/// <summary>
@dgroft
dgroft / MyImmutableObject.cs
Created November 8, 2012 20:59
A constructor for immutable types that takes a dynamic list of parameters
protected MyImmutableObject(IDictionary<string, object> parameters)
{
foreach (var field in GetType().GetFields().Where(x => parameters.Keys.Contains(x.Name)))
{
if (parameters[field.Name] is Dictionary<string, object>)
{
var ctor = field.FieldType.GetConstructor(new[] { typeof(Dictionary<string, object>) });
var instance = ctor.Invoke(new object[] { parameters[field.Name] });
field.SetValue(this, instance);
}
@dgroft
dgroft / ImmutableDataObject.cs
Created November 14, 2012 18:53
Immutable object base class
using System.Linq;
public abstract class DataObject
{
public override bool Equals(object obj)
{
var dataObj = obj as DataObject;
if (dataObj == null) return false;
@dgroft
dgroft / CSVtoXML.cs
Last active December 3, 2020 14:46
Convert CSV to XML in C#
string csvPath = "...";
string xmlPath = "...";
using (StreamReader streamReader = new StreamReader(new FileStream(csvPath, FileMode.Open)))
{
// snag headers
string[] headers = streamReader.ReadLine().Split(',');
// col0="{0}" col1="{1}" coln="{n}"
string attributesFormat = string.Join(" ", headers.Select((colStr, colIdx) => string.Format("{0}=\"{{{1}}}\"", colStr, colIdx)));
@dgroft
dgroft / csvtoxml.py
Last active December 18, 2015 05:38
Converts CSV to XML in Python.
import argparse
import csv
parser = argparse.ArgumentParser(description="Converts a CSV file to an XML file")
parser.add_argument("csv", help="the path to the .CSV file")
parser.add_argument("xml", help="the path to the .XML file")
parser.add_argument("--root", help="root tag name", default="root")
parser.add_argument("--row", help="row tag name", default="row")
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
args = parser.parse_args()
@dgroft
dgroft / GetPermutations.cs
Created June 25, 2013 16:50
Finds all permutations of a supplied list of generic values.
private static List<List<T>> GetPermutations<T>(List<T> values)
{
if (values.Count <= 1) return new List<List<T>>() { values };
var results = new List<List<T>>();
var perms = GetPermutations(values.Skip(1).ToList());
foreach (var perm in perms)
{
@dgroft
dgroft / maxsumnonadjacent.py
Last active December 19, 2015 08:19
Maximize the sum of a given collection such that if you include the number in sum, you may not use adjacent numbers toward the sum. For the collection [1, 5, 3, 9, 4], the max sum is 5 + 9 = 14. For the collection [1, 2, 3, 4, 5], the max sum is 1 + 3 + 5 = 9. For the collection [1, 3, 5, -1, 12, 6, 7, 11], the max sum is 1 + 5 + 12 + 11 = 29.
from __future__ import print_function
import heapq
# construct initial collection
initial_col = [1, 5, 3, 9, 4]
# construct heap
heap = []
# assemble the heap, store tuples as such: (original_value, original_index)
@dgroft
dgroft / ParseEnum.cs
Created July 8, 2013 15:08
Parsing an enum.
enum DaysOfTheWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
var tues = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), "Tuesday");
@dgroft
dgroft / redis-centos.sh
Created July 30, 2013 20:31
Installs Redis on CentOS.
#!/bin/bash
################################################################################
# Installing Redis on CentOS
# with lots of help from:
# * https://gist.github.com/coder4web/5762999
# * http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
# * https://coderwall.com/p/ypo94q
# * https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
################################################################################
@dgroft
dgroft / launch_sln.bat
Last active December 21, 2015 18:19
Launching Visual Studio 2008 with temporary environment variables and temporarily appending to PATH.
' invoke vsvars32.bat. just do it.
call "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
' temporary environment variables assignment
' set ENV_VAR = value
' temporary path modification
set path=%path%;C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE
' launch visual studio