Logging Experiences

Code Snippet: Mean, Variance, and Standard Deviation Methods

Posted in C#, Programming, Snippets by Sina Iravanian on May 15, 2011

The methods to calculate mean, variance, and standard deviation of a vector of values. These are put here for easy reference, so that I do not need to rewrite them again.


A Helper Method for Performing K-Fold Cross Validation

Posted in C#, Programming, Snippets by Sina Iravanian on May 15, 2011

The following method is a utility method for creating the K divisions upon which one is going to perform the K-fold cross validation operation. The input of the method is the length of the training data, and the number K. The output says which indices of the training-data is to be put in each division.

Tagged with: ,

Dealing with ListView control, in the details mode

Posted in C#, Programming, Snippets, Tips and Tricks by Sina Iravanian on May 9, 2011

I needed a piece of code to manage list-view controllers. I needed to use ListView controls in the details mode, which automatically sort its contents when I click on the header, and also pastes the content in a tabular format when I press Ctrl+C or Ctrl+Insert on it. So I created the following classes. It is necessary to note that the code that carries out the sorting stuff when the header is clicked is taken from the following MSDN article:

So these are the contributions of these pieces of code: 1) Sorting ListView items automatically when the header is clicked, 2) Pasting the contents of the ListView control to the clipboard in a tabular format, and 3) Providing an easy to use API to append data to a ListView control.

The following gist contains the code to accomplish this. It consists of 3 files. The ListViewColumnSorter.cs file is responsible for the sorting stuff. The ListViewUtils.cs file provides the utility methods for a ListView control. The main methods that perform the job are located in the ListViewUtils class. The two needed methods are InitListView and AppendToListView. And finally the Usage.cs file provides a small sample demo of the utility methods.

Code Snippet: Trimming Strings of White-space and control characters

Posted in C#, Programming, Snippets by Sina Iravanian on March 2, 2011

The following method trims the given string of both white-space characters and control characters, and its rather fast!

Tagged with: , ,

Code Snippet: Determining how two intervals overlap

Posted in C#, Programming, Snippets by Sina Iravanian on August 9, 2010

The following pieces of code help you figure out how two intervals overlap, and provides tools to represent them in a way that is useful for debugging purposes. This job is quite easy to accomplish, but since it is written too frequently each time from scratch, I have put them in this gist.

The IntervalOverlapKinds.cs file defines the IntervalOverlapKinds enumeration. The interval detection is carried out in the static methods of the IntervalOverlap class.

Code Snippet: Representing strings with all their characters visible

Posted in C#, Programming, Snippets by Sina Iravanian on August 8, 2010

This piece of code is useful if you intend to work on a string processing algorithm. There are times, that you need to see the real content of a string while showing them in a windows form, console, and so on. This little method may help:

Code Snippet: Searching and Highlighting a token or string in a RichTextBox

Posted in C#, Programming, Snippets by Sina Iravanian on August 8, 2010

Suppose that you intend to find a string or a token (i.e., whole word, instead of a substring) and change their color in some RichTextBox. The following code snippets will help. The FindStringAndSetColor method looks for instances of an arbitrary string in the contents of the RichTextBox, while the FindTokenAndSetColor method looks for whole words (tokens).

Tagged with: , ,