This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LibraryManager; | |
public sealed record Book(string Name, bool CanBeBorrowed, int? AgeRating) : ILibraryItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder