Skip to content

Instantly share code, notes, and snippets.

View KvanTTT's full-sized avatar

Ivan Kochurkin KvanTTT

View GitHub Profile
@KvanTTT
KvanTTT / RationalNumberCounting.cs
Created September 22, 2012 09:04
Rational numbers counting (with inverse)
public static long RationalNumber(long i, long j)
{
if (j == 1)
{
if (i == 0)
return 1;
else if (i == 1)
return 2;
}
@KvanTTT
KvanTTT / PolynomialRegression.cs
Last active July 28, 2020 07:48
Implementation of Polynomial regression (http://en.wikipedia.org/wiki/Polynomial_regression) with Math.NET library (http://www.mathdotnet.com/)
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Generic;
public class PolynomialRegression
{
private int _order;
private Vector<double> _coefs;
/// <summary>
/// Calculates polynom regression for xData = [x1, x2, ... , xn] and yData = [y1, y2, ... , yn].
@KvanTTT
KvanTTT / PolygonTriangulator.cs
Created October 8, 2012 21:31
Short solution for splitting concave polygon on convex polygons or triangles with other utils (SelfIntersection checking).
using System.Collections.Generic;
using System.Drawing;
public class PolygonTriangulator
{
/// <summary>
/// Calculate list of convex polygons or triangles.
/// </summary>
/// <param name="Polygon">Input polygon without self-intersections (it can be checked with SelfIntersection().</param>
/// <param name="triangulate">true: splitting on triangles; false: splitting on convex polygons.</param>
echo off
:LOOP
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" "Asciimation_1_3.cs"
"Asciimation_1_3.exe" > "Asciimation_1_3.cs"
type "Asciimation_1_3.cs"
goto LOOP
:END
@KvanTTT
KvanTTT / QuineSnake.bat
Last active September 16, 2019 13:08
Game Snake in source code (Quine). One can play this with batch or power scripts. The game can be finished =)
echo off
:LOOP
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" "QuineSnake.cs"
"QuineSnake.exe" > "QuineSnake.cs"
type "QuineSnake.cs"
goto LOOP
:END
@KvanTTT
KvanTTT / QuineClock.bat
Created July 12, 2015 16:34
QuineClock
echo off
:LOOP
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" "QuineClock.cs"
"QuineClock.exe" > "QuineClock.cs"
type "QuineClock.cs"
goto LOOP
:END
@KvanTTT
KvanTTT / ManyStringConcatenation.java
Created October 20, 2015 17:07
ANTLR Java7.g4 very slow performance sample (~15 sec Java.g4 versus ~0.15 sec Java8.g4 even with two-stage parsing strategy).
class Test
{
@ApiModelProperty(value =
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789" +
using static System.Console;
using static System.Math;
using static System.DayOfWeek;
using static System.Linq.Enumerable;
namespace CSharp6Samples
{
public class Test
{
// Initializers for auto-properties
@KvanTTT
KvanTTT / CSharpAntlrParser.cs
Last active May 13, 2021 18:31
ANTLR C# runtime code fragment for correct C# code parsing with preprocessor directives (for CSharp grammar in oficial repository: https://github.com/antlr/grammars-v4/tree/master/csharp).
List<IToken> codeTokens = new List<IToken>();
List<IToken> commentTokens = new List<IToken>();
Lexer preprocessorLexer = new CSharpLexer(new AntlrInputStream(sourceCode));
// Collect all tokens with lexer (CSharpLexer.g4).
var tokens = preprocessorLexer.GetAllTokens();
var directiveTokens = new List<IToken>();
var directiveTokenSource = new ListTokenSource(directiveTokens);
var directiveTokenStream = new CommonTokenStream(directiveTokenSource, CSharpLexer.DIRECTIVE);
CSharpPreprocessorParser preprocessorParser = new CSharpPreprocessorParser(directiveTokenStream);
@KvanTTT
KvanTTT / CSharp6FeaturiesWalker.cs
Last active July 7, 2017 20:05
Walker for detection C# 6 syntax features
public class CSharp6FeaturiesWalker : CSharpSyntaxWalker
{
public bool CSharp6Featuries { get; private set; }
public CSharp6FeatureWalker()
{
}
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{