Skip to content

Instantly share code, notes, and snippets.

View admir-live's full-sized avatar

Admir Mujkic admir-live

View GitHub Profile
using System;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Console.WriteLine("Starting main method.");
LogApiCall("API endpoint called.");
@admir-live
admir-live / CalculateFactorial.cs
Created September 11, 2023 04:05
Calculates the factorial of a given non-negative integer using a recursive approach.
/// <summary>
/// Calculates the factorial of a given non-negative integer using a recursive approach.
/// </summary>
/// <param name="number">The non-negative integer for which the factorial is to be calculated.</param>
/// <returns>The factorial of the given number.</returns>
/// <exception cref="ArgumentException">Thrown when the provided number is negative.</exception>
public static int CalculateFactorial(int number)
{
if (number < 0)
{
@admir-live
admir-live / FindFileInDirectory.cs
Created September 11, 2023 04:09
Recursively searches for a file with the specified name within a directory and its subdirectories.
/// <summary>
/// Recursively searches for a file with the specified name within a directory and its subdirectories.
/// </summary>
/// <param name="directoryPath">The path of the directory to start the search from.</param>
/// <param name="fileName">The name of the file to search for.</param>
/// <returns>The full path of the found file or null if the file is not found.</returns>
public static string FindFileInDirectory(string directoryPath, string fileName)
{
if (string.IsNullOrWhiteSpace(directoryPath) || string.IsNullOrWhiteSpace(fileName))
{
@admir-live
admir-live / ComputeFibonacci.cs
Created September 11, 2023 04:11
Computes the nth Fibonacci number using a recursive approach.
/// <summary>
/// Computes the nth Fibonacci number using a recursive approach.
/// </summary>
/// <param name="n">The position of the Fibonacci number to compute.</param>
/// <returns>The nth Fibonacci number.</returns>
public static int ComputeFibonacci(int n)
{
if (n < 0)
{
throw new ArgumentOutOfRangeException(nameof(n), "Input must be a non-negative integer.");
@admir-live
admir-live / FindMaximumValue.cs
Created September 11, 2023 04:16
This function finds the maximum value in an array using a recursive approach
public static int FindMaximumValue(int[] numbers, int startIndex, int endIndex)
{
// Base case: If the array has only one element, return that element.
if (startIndex == endIndex)
return numbers[startIndex];
int middleIndex = (startIndex + endIndex) / 2;
int leftMax = FindMaximumValue(numbers, startIndex, middleIndex);
int rightMax = FindMaximumValue(numbers, middleIndex + 1, endIndex);
@admir-live
admir-live / CalculateSumBetweenIndices.cs
Created September 11, 2023 04:18
Calculates the sum of elements in a list between two indices using recursion.
/// <summary>
/// Calculates the sum of elements in a list between two indices using recursion.
/// </summary>
/// <param name="numbers">The list of numbers.</param>
/// <param name="startIndex">The starting index for the sum.</param>
/// <param name="endIndex">The ending index for the sum.</param>
/// <param name="accumulatedSum">The current accumulated sum (used for recursion).</param>
/// <returns>The sum of elements between the specified indices.</returns>
public static int CalculateSumBetweenIndices(List<int> numbers, int startIndex, int endIndex, int accumulatedSum = 0)
{
@admir-live
admir-live / ReverseString.cs
Created September 16, 2023 16:51
ReverseString
public static string ReverseString(string input)
{
int length = input.Length;
char[] charArray = new char[length];
for (int i = 0; i < length; i++)
{
charArray[i] = input[length - 1 - i];
}
| Method | Mean | Error | StdDev |
|---------------------- |----------:|---------:|---------:|
| ReverseUsingCharArray | 55.24 ns | 1.167 ns | 1.747 ns |
| ReverseUsingLinq | 388.46 ns | 7.526 ns | 8.365 ns |
public class StringReversalBenchmarks
{
private const string TestString = "This is a test string for benchmarking purposes.";
[Benchmark]
@admir-live
admir-live / Solution.sql
Created September 25, 2023 18:19
Solution.sql
MERGE INTO target_table AS target
USING source_table AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET target.column1 = source.column1, target.column2 = source.column2
WHEN NOT MATCHED THEN
INSERT (id, column1, column2) VALUES (source.id, source.column1, source.column2);
@admir-live
admir-live / Client.js
Created September 28, 2023 19:22
SignalR Example
// And the client-side JavaScript to connect to this hub:
const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
connection.on("ReceiveMessage", function (message) {
console.log("New message: " + message);
});
connection.start();