Skip to content

Instantly share code, notes, and snippets.

@sudipto80
sudipto80 / gist:994bfb0495b3fcdac344
Created Oct 10, 2014
k Nearest Neighbour using LINQ
View gist:994bfb0495b3fcdac344
//Nearest Neighbour
var trainingSet = File.ReadAllText(@"C:\iris.csv")
.Split(new char[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries)
.Select ( f => f.Split(','))
.Skip(1)
.Select (f =>
new
{
SepalLength = Convert.ToDouble( f[0]),
SepalWidth = Convert.ToDouble(f[1]),
View gist:5c887a7df2971c84c196
string keyPad = @"2 = ABC2
3 = DEF3
4 = GHI4
5 = JKL5
6 = MNO6
7 = PQRS7
8 = TUV8
9 = WXYZ9";
Dictionary<char,char> keyMap = new Dictionary<char,char>();
//4663 can lead to "good","home" etc
@sudipto80
sudipto80 / gist:8b9819b37b8bd27448ff
Created Oct 10, 2014
Simulating Swype style keyboard
View gist:8b9819b37b8bd27448ff
void Main()
{
//Should show understand. See the bold characters
string path = "ujnbvcderesdftrewazxcvbnhgfds";
//Define other methods and classes here
StreamReader sr = new StreamReader ("C:\\T9.txt");
string total = sr.ReadToEnd(); sr.Close();
List<string> suggestions = new List<string>();
var query = total.Split(new char[]{'\r','\n',' '},StringSplitOptions.RemoveEmptyEntries)
.Select (t => t.Trim()) ;
@sudipto80
sudipto80 / gist:a008f7f992d7a8f01d71
Created Oct 10, 2014
Meta Programming using Roslyn (Finding all methods that don't use all the parameters)
View gist:a008f7f992d7a8f01d71
SyntaxTree tree = SyntaxTree.ParseText(@"int fun(int x,int z){ int y = 0; x++; return x+1;}
double funny(double x){ return x/2.13;}");
List<MethodDeclarationSyntax> methods = tree.GetRoot()
.DescendantNodes()
.Where(d => d.Kind == SyntaxKind.MethodDeclaration)
.Cast<MethodDeclarationSyntax>().ToList();
methods.Select(z =>
{
var parameters = z.ParameterList.Parameters.Select(p => p.Identifier.ValueText);
@sudipto80
sudipto80 / gist:824c84bac10a9e1e3780
Created Oct 10, 2014
Meta Programming using Roslyn (Finding LoC for all the methods)
View gist:824c84bac10a9e1e3780
SyntaxTree tree = SyntaxTree.ParseText(@"int fun(int x){ int y = 10; x++;
if(x>20) x -= y; return x+1;}
double funny(double x){ int s = 3; double t = 4;
return x + s * t/2.13;}");
List<MethodDeclarationSyntax> methods = tree.GetRoot()
.DescendantNodes()
.Where(d => d.Kind == SyntaxKind.MethodDeclaration)
.Cast<MethodDeclarationSyntax>().ToList();
@sudipto80
sudipto80 / gist:3c286e7167e99dc6a6bc
Created Oct 10, 2014
Meta Programming using Roslyn (Finding all Local Variables for all the methods)
View gist:3c286e7167e99dc6a6bc
SyntaxTree tree = SyntaxTree.ParseText(@"int fun(int x){ int y = 0; x++; return x+1;}
double funny(double x){ int s = 3; double t = 4; return x + s * t/2.13;}");
List<MethodDeclarationSyntax> methods = tree.GetRoot()
.DescendantNodes()
.Where(d => d.Kind == SyntaxKind.MethodDeclaration)
.Cast<MethodDeclarationSyntax>()
.ToList();
methods.Select(z => new { MethodName = z.Identifier.ValueText,
@sudipto80
sudipto80 / gist:ccb75622b3f83b33b3f1
Created Oct 10, 2014
Armstrong DSL ( You need Evaluant LINQ Compiler)
View gist:ccb75622b3f83b33b3f1
public static class IntEx
{
public static int Cube(this int number)
{
return number * number * number;
}
public static int Square(this int number)
{
return number * number;
}
@sudipto80
sudipto80 / gist:6194ecce24861d2b3474
Created Oct 10, 2014
Lazy Fibonacci and Friends
View gist:6194ecce24861d2b3474
public static class SequenceEx
{
public static IEnumerable<T> StartWith<T>(params T[] seeds)
{
return new List<T>(seeds).AsEnumerable();
}
public static IEnumerable<T> ThenFollow<T>(this IEnumerable<T> thisSequence,
Func<T,T,T> rule) where T:IEquatable<T>
{
@sudipto80
sudipto80 / gist:1d96feab131cc03c7cb4
Created Oct 10, 2014
Peter Norvig's Spell Checker (using List Comprehension in LINQ)
View gist:1d96feab131cc03c7cb4
//Peter Norvig's Spell Check
Dictionary<string,int> NWords = new Dictionary<string,int>();
public IEnumerable<string> Edits1(string word)
{
char[] alphabet = {'a','b','c','d','e','f','g','h','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z'};
var splits = Enumerable.Range(1,word.Length).Select(i => new {First = new string(word.ToCharArray().Take(i).ToArray()),
Second = new string(word.ToCharArray().Skip(i).ToArray())});
var deletes = splits.Where (split => !string.IsNullOrEmpty(split.Second)).Select (split => split.First + split.Second.Substring(1));
@sudipto80
sudipto80 / gist:5e40eca9efac900314c6
Created Oct 28, 2014
Squirrel Example : Finding whether women are more generaous than men while paying tip
View gist:5e40eca9efac900314c6
//Problem : Locate average percentage of Tip paid by men and women from tips.csv
//Done in 3 lines of C# code using Squirrel.NET
//Loading the data to Squirrel.NET Table is easy
Table tips = DataAcquisition.LoadCSV(@"..\..\tips.csv");
//Add a new column based on the formula
tips.AddColumn(columnName: "tip%", formula: "[tip]*100/[totbill]", decimalDigits: 3);