Skip to content

Instantly share code, notes, and snippets.

View aannenko's full-sized avatar

Andrii Annenko aannenko

View GitHub Profile
@aannenko
aannenko / NaiveDictionary.cs
Last active December 17, 2024 09:05
A Dictionary class that uses an array of linked lists to hold the key-value pairs
var myDict = new NaiveDictionary<int, string?>();
myDict[1] = "abc";
myDict[1] = "def";
myDict[2] = "def";
myDict[3] = "def";
myDict[4] = "def";
myDict[5] = "def";
myDict[6] = "def";
myDict[7] = "def";
@aannenko
aannenko / GetPermutationsHeapAlg.cs
Last active February 2, 2025 09:36
Returns all string permutations using the Heap's algorithm (linear).
GetPermutations(['a', 'b', 'c', 'd']).Dump();
static IEnumerable<string> GetPermutations(char[] chars)
{
yield return new string(chars);
if (chars.Length < 2)
yield break;
var counter = new int[chars.Length];
@aannenko
aannenko / GetPermutationsRecursive.cs
Created December 5, 2024 20:28
Returns all string permutations using the Heap's algorithm (recursive).
char[] chars = ['a', 'b', 'c', 'd'];
GetPermutationsRecursive(chars.Length, chars).Dump();
static IEnumerable<string> GetPermutationsRecursive(int take, char[] chars)
{
if (take is 1)
{
yield return new string(chars);
}
else
@aannenko
aannenko / CountPairsWithDifference.cs
Last active December 5, 2024 10:59
Given an array of distinct integer values, count the number of pairs of integers that have difference k.
// Given an array of distinct integer values, count the number of pairs of integers that
// have difference k. For example, given the array [1, 7, 5, 9, 2, 12, 3] and the difference
// k = 2, there are four pairs with difference 2: (1, 3), (3, 5), (5, 7), (7, 9).
CountPairsWithDifference([1, 7, 5, 9, 2, 12, 3], 2).Dump(); // 4
CountPairsWithDifference([int.MaxValue, int.MinValue, int.MaxValue - 2, int.MinValue + 2], 2).Dump(); // 2
CountPairsWithDifference([1, 7, 5, 9, 2, 12, 3], 0).Dump(); // 0
CountPairsWithDifference([1, 7, 5, 9, 2, 12, 3], -2).Dump(); // 4
int CountPairsWithDifference(int[] integers, int diff)
@aannenko
aannenko / FindCubicSumPairs.cs
Created December 5, 2024 09:39
Find all positive solutions to the equation a^3 + b^3 = c^3 + d^3 where a, b, c, and d are integers between 1 and 1000.
// Find all positive solutions to the equation a^3 + b^3 = c^3 + d^3
// where a, b, c, and d are integers between 1 and 1000.
FindCubicSumPairs(1, 1000).Dump();
static IEnumerable<(int A, int B, int C, int D)> FindCubicSumPairs(int min, int max)
{
var sumToPairs = new Dictionary<double, List<(int, int)>>();
for (int a = min; a <= max; a++)
{
@aannenko
aannenko / CountPermutations.cs
Last active December 3, 2024 14:28
How many permutations of one string are there in the other string (hash and counter comparison)
// Given a smaller string "pattern" and a bigger string "text",
// design an algorithm to find all permutations of the shorter
// string within the longer one. Return the location of each permutation.
CountPermutations("cbabadcbbabbcbabaabccbabc", "abbc").Dump();
// Output: [0, 6, 9, 11, 12, 20, 21]
CountPermutations("encyclopedia", "dep").Dump();
// Output: [7]
@aannenko
aannenko / FindMagnet-BenchmarkDotNet.txt
Last active January 14, 2025 10:00
Find magnet link on a web page
BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.2605)
13th Gen Intel Core i9-13900K, 1 CPU, 32 logical and 24 physical cores
.NET SDK 9.0.101
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
DefaultJob : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
| Method | Mean | Error | StdDev | Ratio | Gen0 | Gen1 | Allocated | Alloc Ratio |
|------------------ |----------:|----------:|----------:|------:|-------:|-------:|----------:|------------:|
| FindMagnetInLines | 24.945 μs | 0.2800 μs | 0.2619 μs | 1.00 | 4.1504 | 0.0610 | 78168 B | 1.000 |
@aannenko
aannenko / Program.cs
Last active June 17, 2024 13:21
.NET Web API with Swagger trimmed
using Microsoft.AspNetCore.Routing.Constraints;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(
options => options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default));
builder.Services.Configure<RouteOptions>(
options => options.SetParameterPolicy<RegexInlineRouteConstraint>("regex"));
@aannenko
aannenko / Book.cs
Created June 4, 2024 14:00
Library management OOP/SOLID
namespace LibraryManager;
public sealed record Book(string Name, bool CanBeBorrowed, int? AgeRating) : ILibraryItem;
@aannenko
aannenko / RotatedBinarySearch.cs
Last active June 2, 2024 09:05
Binary search with support for rotated arrays with duplicates
Console.WriteLine(RotatedBinarySearch([6, 7, 8, 9, 10, 1, 2, 3, 5], 3)); // 7
Console.WriteLine(RotatedBinarySearch([1, 1, 1, 1, 1, 1, 2, 1, 1, 1], 2)); // 6
static int RotatedBinarySearch(int[] ints, int searchedValue)
{
var low = 0;
var high = ints.Length - 1;
while (low <= high)
{
var mid = low + (high - low) / 2;