Skip to content

Instantly share code, notes, and snippets.

@sinairv
sinairv / JoinAndSeparate.cs
Created December 17, 2011 15:51
Utilities for Joining and Separating a Set of Strings with a Specified Separator Character
/// <summary>
/// Separates the given compound string assuming that string components are separated
/// with the given separator character.
/// It is assumed that any usage of the separator character inside the compound string
/// is escaped with a backslash character, and any usage of the backslash character
/// is escaped with double backslash characters.
/// </summary>
/// <param name="compound">The compound string</param>
/// <param name="separator">The separator character</param>
/// <returns><c>List</c> of strings that built the original compound string</returns>
@sinairv
sinairv / TestHelper.cs
Created January 15, 2012 10:21 — forked from haacked/TestHelper.cs
String Comparison Unit Test Helper
public static class TestHelpers
{
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
{
ShouldEqualWithDiff(actualValue, expectedValue, DiffStyle.Full, Console.Out);
}
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle)
{
ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);
@sinairv
sinairv / MeanVarStDev.cs
Created May 14, 2012 11:44
Mean, Variance, and Standard Deviation Methods
/// <summary>
/// Calculates the mean of an array of values
/// </summary>
/// <param name="v">the array of values to calculate their mean</param>
/// <returns>The mean of the array of values</returns>
public static double Mean(double[] v)
{
double sum = 0.0;
for (int i = 0; i < v.Length; i++)
@sinairv
sinairv / KFoldCrossValidation.cs
Created May 14, 2012 12:14
A Helper Method for Performing K-Fold Cross Validation
private static readonly Random s_rnd = new Random();
/// <summary>
/// Divides numbers from 0 to length - 1 into k random folds.
/// This method is a helper method for K-fold cross validaiton.
/// </summary>
/// <param name="length">The length of the data to fold into k divisions. </param>
/// <param name="k">number of divisions</param>
/// <returns>K arrays of indices. Each of the arrays contain 0-based indices of the
/// data to be put in each division.</returns>
@sinairv
sinairv / ListViewColumnSorter.cs
Created May 14, 2012 12:41
Dealing with ListView control, in the details mode
// taken from: http://msdn.microsoft.com/en-us/library/ms996467.aspx
using System.Collections;
using System.Windows.Forms;
namespace ListViewControlling
{
/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
@sinairv
sinairv / TrimFullString.cs
Last active October 4, 2015 20:27
A method that trims both white-space and control characters from a string, and its rather fast!
public static string TrimFullString(string str)
{
int start = 0;
int end = str.Length - 1;
for (; start <= end; start++)
{
if (!Char.IsWhiteSpace(str[start]) &&
!Char.IsControl(str[start]))
break;
@sinairv
sinairv / IntervalOverlap.cs
Created May 14, 2012 13:27
Determining how two intervals overlap
/// <summary>
/// Provides tools to detect the kind of the overlap between two intervals
/// </summary>
public static class IntervalOverlap
{
/// <summary>
/// Detects kind of the overlap that the specified two ranges have.
/// </summary>
/// <param name="start1">The inclusive start of the first interval.</param>
/// <param name="end1">The exclusive end of the first interval.</param>
@sinairv
sinairv / MakeStringVisible.cs
Created May 14, 2012 13:38
Representing strings with all their characters visible
/// <summary>
/// Returns a visible version of the string, by making
/// its whitespace and control characters visible
/// using well known escape sequences, or the equivalant
/// hexa decimal value.
/// </summary>
/// <param name="str">The string to be made visible.</param>
/// <returns></returns>
public static string MakeStringVisible(string str)
{
@sinairv
sinairv / FindAndSetColor.cs
Created May 14, 2012 13:45
Find string and set its color in a RichTextBox
// searches an arbitrary string
public static void FindStringAndSetColor(RichTextBox rtb, string key, Color color)
{
int lastIndex = -1;
do
{
int found = rtb.Find(key, lastIndex + 1, RichTextBoxFinds.None);
if (found >= 0)
{
rtb.Select(found, key.Length);
@sinairv
sinairv / rec-cte.sql
Created July 25, 2012 02:20
Understanding recursive CTE
-- taken from: http://www.databasejournal.com/features/mssql/article.php/10894_3502676_2/Common-Table-Expressions-CTE-on-SQL-2005.htm
-- recursive CTEs are divided in two parts 1. above "UNION ALL", and 2. below that
-- the statements on part 1 should be run without recursion to the CTE itself, this is our so-called recursion exit condition (anchor member)
-- the statements on part 2 use the CTE name in their select clause, this is the main recursion (recursive member)
-- the script below lists employees as well as their direct managers.
USE AdventureWorks ;
GO
WITH DirectReports(LoginID, ManagerID, EmployeeID) AS