Skip to content

Instantly share code, notes, and snippets.

View aannenko's full-sized avatar

Andrii Annenko aannenko

View GitHub Profile
@aannenko
aannenko / GetMagnetLink.cs
Last active July 13, 2024 10:40
Read magnet link from web page
@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;
@aannenko
aannenko / BinarySearch.cs
Created May 30, 2024 14:20
Binary search, iterative and recursive
var ints = new int[] { 2, 3, 4, 10, 40 };
Console.WriteLine(IndexOfUsingIterativeBinarySearch(ints, 3)); // 1
Console.WriteLine(IndexOfUsingRecursiveBinarySearch(ints, 0, ints.Length - 1, 3)); // 1
static int IndexOfUsingIterativeBinarySearch(int[] ints, int searchedValue)
{
int low = 0;
int high = ints.Length - 1;
while (low <= high)
{
@aannenko
aannenko / Node.cs
Last active May 6, 2024 07:30
Trie
public sealed class Node
{
private readonly Dictionary<char, Node> _children = [];
public IReadOnlyCollection<Node> Children => _children.Values;
public string? Word { get; private set; }
public Node GetOrAddChild(char letter, string? word = null)
{
// Introduction to Algorithms, Third Edition - 2009
using System;
using System.Collections.Generic;
Console.WriteLine(string.Join(' ', new[] { 2, 8, 7, 1, 3, 5, 6, 4 }.QuickSort()));
Console.WriteLine(string.Join(' ', new[] { 31, 41, 59, 26, 41, 58 }.QuickSort()));
static class ArrayExtensions
{
// Introduction to Algorithms, Third Edition - 2009
using System;
using System.Collections.Generic;
Console.WriteLine(string.Join(' ', new[] { 5, 2, 4, 6, 1, 3 }.InsertionSortOptimized()));
Console.WriteLine(string.Join(' ', new[] { 31, 41, 59, 26, 41, 58 }.InsertionSortOptimized()));
static class ArrayExtensions
{
@aannenko
aannenko / AwaiterTaskSource.cs
Last active November 11, 2023 19:45
One Task, many awaiters - starts a task, issues awaiter-tasks for it, tracks the task until it's finished or all awaiter-tasks are cancelled.
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
public sealed class AwaiterTaskSource<TResult>
{
private readonly CancellationTokenSource _cancellationTokenSource;
private int _awaitersCount = 0;
@aannenko
aannenko / BruteForce.cs
Last active October 16, 2021 05:20
Efficient Brute Force generator that can be consumed by foreach. Replace contents of Program.cs in your .NET 5 project with the following code and run.
using System;
foreach (var text in new BruteForceEnumerable(2, "abc"))
Console.WriteLine(text.ToString());
public class BruteForceEnumerable
{
private readonly int _maxTextLength;
private readonly string _characters;