Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
6a6e2eb
Move to .NET 10 and optimize build
pascalecu May 12, 2026
3c854fb
Migrate Baklava to C# 14
pascalecu May 12, 2026
341f3c1
Migrate Base64 Encode Decode to C# 14
pascalecu May 12, 2026
978d335
Migrate Binary Search to C# 14
pascalecu May 12, 2026
c8a5c52
Use ReadOnlySpan for parsing list of integer
pascalecu May 12, 2026
4ca0a8d
Return numbers as out param in TryParseSortedList
pascalecu May 12, 2026
63fd22e
Migrate Bubble Sort to C# 14
pascalecu May 12, 2026
a88e364
Migrate Capitalize to C# 14
pascalecu May 12, 2026
06b9b95
Move parsing tokens to inner helper
pascalecu May 12, 2026
bd1649f
Migrate Convex Hull to C# 14
pascalecu May 12, 2026
ae404f6
Migrate Depth First Search to C# 14
pascalecu May 12, 2026
6dcb67a
Migrate Dijkstra to C# 14
pascalecu May 12, 2026
30f81c7
Migrate Duplicate Character Counter to C# 14
pascalecu May 12, 2026
7afa6fb
Migrate Even Odd to C# 14
pascalecu May 12, 2026
37dbbd4
Migrate Factorial to C# 14
pascalecu May 12, 2026
dba6f6c
Migrate Fibonacci to C# 14
pascalecu May 12, 2026
e569139
Migrate File Input Output to C# 14
pascalecu May 12, 2026
523f9bd
Migrate Fizz Buzz to C# 14
pascalecu May 12, 2026
eb67a00
Migrate Fraction Math to C# 14
pascalecu May 12, 2026
6e725fc
Migrate Hello World to C# 14
pascalecu May 12, 2026
74ab703
Migrate Insertion Sort to C# 14
pascalecu May 12, 2026
4850367
Use List<T>.BinarySearch and clean up parsing
pascalecu May 12, 2026
992bb1f
Improve Bubble Sort readability
pascalecu May 12, 2026
bfd462c
Improve Convex Hull readability and parsing
pascalecu May 12, 2026
28ac79c
Move to CSR format and improve parsing
pascalecu May 12, 2026
daf54aa
Switch to adjacency list and improve parsing
pascalecu May 12, 2026
e2a3547
Make parsing more readable in insertion sort
pascalecu May 12, 2026
0a3ba31
Migrate Job Sequencing to C# 14 + use DSU
pascalecu May 12, 2026
acc4f7f
Migrate Josephus Problem to C# 14
pascalecu May 12, 2026
1b04c91
Migrate Linear Search to C# 14
pascalecu May 12, 2026
78ed3b9
Migrate Longest Common Subsequence to C# 14
pascalecu May 12, 2026
9c981b7
Migrate Longest Palindromic Substring to C# 14
pascalecu May 12, 2026
54b3fc5
Migrate Longest Word to C# 14
pascalecu May 12, 2026
8bc55dc
Migrate Maximum Array Rotation to C# 14
pascalecu May 12, 2026
315196a
Migrate Maximum Subarray to C# 14
pascalecu May 12, 2026
99e588d
Migrate Merge Sort to C# 14
pascalecu May 12, 2026
75dd216
Migrate Minimum Spanning Tree to C# 14
pascalecu May 12, 2026
1a6704d
Migrate Palindromic Number to C# 14
pascalecu May 12, 2026
0c852f8
Migrate Prime Number to C# 14
pascalecu May 12, 2026
c9b34d0
Migrate Quick Sort to C# 14
pascalecu May 12, 2026
6de2d46
Migrate Remove All Whitespace to C# 14
pascalecu May 12, 2026
a85c808
Migrate Reverse String to C# 14
pascalecu May 12, 2026
f21cb94
Migrate Roman Numeral to C# 14
pascalecu May 12, 2026
caf0ea6
Migrate Rot13 to C# 14
pascalecu May 12, 2026
92d3ad3
Migrate Selection Sort to C# 14
pascalecu May 12, 2026
67a70e5
Migrate Transpose Matrix to C# 14
pascalecu May 12, 2026
b08f7be
Migrate Zeckendorf to C# 14
pascalecu May 12, 2026
c18edc6
Shorten Base64 Encode Decode
pascalecu May 12, 2026
d92f62b
Shorten Binary Search
pascalecu May 12, 2026
2f32d9c
Shorten Bubble Sort
pascalecu May 12, 2026
e3a51e5
Shorten Capitalize
pascalecu May 12, 2026
f9f4dc0
Shorten Convex Hull
pascalecu May 12, 2026
80b41b8
Shorten Depth First Search
pascalecu May 12, 2026
3064b2a
Shorten Dijkstra
pascalecu May 13, 2026
6d2f984
Shorten Duplicate Character Counter
pascalecu May 13, 2026
e01d2e5
Move to Console.Error in Even Odd
pascalecu May 13, 2026
05bc07a
Shorten Factorial
pascalecu May 13, 2026
19df7a5
Shorten Fibonacci
pascalecu May 13, 2026
832c594
Shorten Fizz Buzz
pascalecu May 13, 2026
5049cd6
Shorten Fraction Math
pascalecu May 13, 2026
9381d4c
Shorten Insertion Sort
pascalecu May 13, 2026
964835f
Shorten Job Sequencing
pascalecu May 13, 2026
894b49a
Shorten Josephus Problem
pascalecu May 13, 2026
ad7ff34
Shorten Linear Search
pascalecu May 13, 2026
ae29c33
Shorten Longest Common Subsequence
pascalecu May 13, 2026
6ab4ca7
Shorten Longest Palindromic Substring
pascalecu May 13, 2026
b22d6f2
Shorten Longest Word
pascalecu May 13, 2026
4974204
Shorten parsing in more places
pascalecu May 13, 2026
dc191a1
Fix Dijkstra
pascalecu May 13, 2026
4da7f49
Fix Linear Search
pascalecu May 13, 2026
f2f24aa
Shorten matrix parsing
pascalecu May 13, 2026
535face
Fix Transpose Matrix
pascalecu May 13, 2026
c7b6db6
Fix Maximum Array Rotation
pascalecu May 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions archive/c/c-sharp/Baklava.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
using System;
const int Height = 10;
const char Symbol = '*';

class CSharp
{

static void Main (string[] args)
{

for (SByte i = 0; i < 10; i++)
Console.WriteLine (
new string (' ', (10 - i)) + new string ('*', (i * 2 + 1))
);
Span<char> stars = stackalloc char[Height * 2 + 1];
stars.Fill(Symbol);

for (SByte i = 10; -1 < i; i--)
Console.WriteLine (
new string (' ', (10 - i)) + new string ('*', (i * 2 + 1))
);
static void PrintRow(int level, int height, ReadOnlySpan<char> stars)
{
Console.Write(new string(' ', height - level));
Console.WriteLine(stars[..(level * 2 + 1)]);
}

}
for (int i = 0; i < Height; i++)
PrintRow(i, Height, stars);

}
for (int i = Height; i >= 0; i--)
PrintRow(i, Height, stars);
83 changes: 25 additions & 58 deletions archive/c/c-sharp/Base64EncodeDecode.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,34 @@
using System.Text;

public class Base64EncodeDecode
return args switch
{
public static void Usage()
{
Console.WriteLine("Usage: please provide a mode and a string to encode/decode");
Environment.Exit(1);
}
["encode", var value] when !string.IsNullOrWhiteSpace(value)
=> Encode(value),

["decode", var value] when !string.IsNullOrWhiteSpace(value)
=> Decode(value),

private static bool IsValidBase64(string input)
{
if (string.IsNullOrWhiteSpace(input) || input.Length % 4 != 0)
return false;
_ => Usage()
};

foreach (char c in input)
{
if (!"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".Contains(c))
return false;
}

int padCount = input.EndsWith("==") ? 2 :
input.EndsWith('=') ? 1 : 0;

int firstPadIndex = input.IndexOf('=');
return firstPadIndex == -1 || firstPadIndex >= input.Length - padCount;
}

public static int Main(string[] args)
{
if (args.Length != 2)
{
Usage();
return 1;
}

string mode = args[0].ToLowerInvariant();
string value = args[1];

if (string.IsNullOrWhiteSpace(mode) || string.IsNullOrWhiteSpace(value))
{
Usage();
return 1;
}
static int Encode(string value)
{
Console.WriteLine(Convert.ToBase64String(Encoding.ASCII.GetBytes(value)));
return 0;
}

try
{
string result = mode switch
{
"encode" => Convert.ToBase64String(Encoding.UTF8.GetBytes(value)),
"decode" => IsValidBase64(value)
? Encoding.UTF8.GetString(Convert.FromBase64String(value))
: throw new ArgumentException("Input is not valid Base64."),
_ => throw new ArgumentException("Unknown mode. Use 'encode' or 'decode'.")
};
static int Decode(string value)
{
byte[] buffer = new byte[value.Length];
if (!Convert.TryFromBase64String(value, buffer, out int written))
return Usage();

Console.WriteLine(result);
return 0;
}
catch
{
Usage();
return 1;
}
}
Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, written));
return 0;
}

static int Usage()
{
Console.Error.WriteLine("Usage: please provide a mode and a string to encode/decode");
return 1;
}
85 changes: 35 additions & 50 deletions archive/c/c-sharp/BinarySearch.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,43 @@
using System;
using System.Linq;
using System.Collections.Generic;

public class BinarySearch
if (args is not [var input, var targetRaw]
|| !int.TryParse(targetRaw, out int target)
|| !TryParseSorted(input.AsSpan(), out var numbers))
{
public static bool Search(List<int> list, int toFind)
{
int lowerBound = 0;
int upperBound = list.Count - 1;
while (lowerBound <= upperBound)
{
int midpoint = (lowerBound + upperBound) / 2;
if (list[midpoint] == toFind)
{
return true;
}
else if (list[midpoint] < toFind)
{
lowerBound = midpoint + 1;
}
else
{
upperBound = midpoint - 1;
}
}
return false;
}
return Usage();
}

public static void ErrorAndExit()
{
Console.WriteLine("Usage: please provide a list of sorted integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")");
Environment.Exit(1);
}
Console.WriteLine(numbers.BinarySearch(target) >= 0);
return 0;

static bool TryParseSorted(ReadOnlySpan<char> span, out List<int> numbers)
{
numbers = new(span.Count(',') + 1);

int last = int.MinValue;

public static void Main(string[] args)
while (!span.IsEmpty)
{
try
{
var list = args[0].Split(',').Select(i => Int32.Parse(i.Trim())).ToList();
var toFind = Int32.Parse(args[1]);

for (int i = 0; i < list.Count - 1; i++)
{
if (list[i] > list[i + 1])
{
ErrorAndExit();
}
}

Console.WriteLine(Search(list, toFind));
}
catch
{
ErrorAndExit();
}
int comma = span.IndexOf(',');
var token = comma >= 0 ? span[..comma] : span;

span = comma >= 0 ? span[(comma + 1)..] : [];

if (!int.TryParse(token, out int n) || n < last)
return false;

numbers.Add(n);
last = n;
}

return numbers.Count > 0;
}

static int Usage()
{
Console.Error.WriteLine(
"""Usage: please provide a list of sorted integers ("1, 4, 5, 11, 12") and the integer to find ("11")"""
);

return 1;
}
103 changes: 52 additions & 51 deletions archive/c/c-sharp/BubbleSort.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;

class CSharp
if (args is not [var input] || !TryParseList(input.AsSpan(), out var numbers))
return Usage();

BubbleSort(numbers);

Console.WriteLine(string.Join(", ", numbers));
return 0;

static bool TryParseList(ReadOnlySpan<char> span, out List<int> numbers)
{
public static List<int> BubbleSort(List<int> xs)
{
var acc = xs.ToList();
var last = acc.ToList();
do
{
last = acc.ToList();
acc = PassList(last.ToList());
}
while(!acc.SequenceEqual(last));
return acc;
}
numbers = new(span.Count(',') + 1);

public static List<int> PassList(List<int> xs)
while (!span.IsEmpty)
{
if (xs.Count() <= 1)
return xs;
var x0 = xs[0];
var x1 = xs[1];
if (x1 < x0)
{
xs.RemoveAt(1);
return new List<int>() {x1}.Concat(PassList(xs)).ToList();
}
else
{
xs.RemoveAt(0);
return new List<int>() {x0}.Concat(PassList(xs)).ToList();
}
}

public static void ErrorAndExit()
{
Console.WriteLine("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"");
Environment.Exit(1);
int comma = span.IndexOf(',');
var token = comma >= 0 ? span[..comma] : span;

span = comma >= 0 ? span[(comma + 1)..] : [];

if (!int.TryParse(token, out int n))
return false;

numbers.Add(n);
}

public static void Main(string[] args)

return numbers.Count > 1;
}

static void BubbleSort(List<int> list)
{
Span<int> span = CollectionsMarshal.AsSpan(list);
int n = span.Length;

for (int i = 0; i < n - 1; i++)
{
if (args.Length != 1)
ErrorAndExit();
try
{
var xs = args[0].Split(',').Select(i => Int32.Parse(i.Trim())).ToList();
if (xs.Count() <= 1)
ErrorAndExit();
var sortedXs = BubbleSort(xs);
Console.WriteLine(string.Join(", ", sortedXs));
}
catch
bool swapped = false;

for (int j = 0; j < n - i - 1; j++)
{
ErrorAndExit();
if (span[j] <= span[j + 1])
continue;

(span[j], span[j + 1]) = (span[j + 1], span[j]);
swapped = true;
}

if (!swapped)
return;
}
}


static int Usage()
{
Console.Error.WriteLine("""
Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"
"""
);
return 1;
}
28 changes: 10 additions & 18 deletions archive/c/c-sharp/Capitalize.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using System;
using System.Linq;

namespace SamplePrograms
if (args is not [string input, ..] || string.IsNullOrWhiteSpace(input))
{
class Program
{
static void Main(string[] args)
{
if (!args.Any() || args[0] == "")
{
Console.WriteLine("Usage: please provide a string");
return;
}
string input = args[0];
string output = input.First().ToString().ToUpper() + input.Substring(1);
Console.WriteLine(output);
}
}
Console.Error.WriteLine("Usage: please provide a string");
return;
}

char c = input[0];

if (char.IsLower(c))
input = char.ToUpperInvariant(c) + input[1..];

Console.WriteLine(input);
Loading
Loading