Last active
January 30, 2024 12:45
-
-
Save codejockie/44a987201e59492d93a84a01246d7321 to your computer and use it in GitHub Desktop.
Array Tabulator
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
public static class MyExtensions | |
{ | |
public static void TimeAgo(this DateTime date) | |
{ | |
// 1000 millisecs = 1 sec | |
// 60 secs = 1 min | |
// 60 mins = 1 hr | |
// 24 hrs = 1 day | |
var diff = DateTime.Now - date; | |
var millis = diff.TotalMilliseconds; | |
var millisInDay = 86400 * 1000; | |
var daysInMillis = millis / millisInDay; | |
var remainder = daysInMillis % 1.0; | |
var fractionInDay = remainder * 24; | |
var days = Math.Floor(daysInMillis); | |
var minutes = Math.Round(60 * fractionInDay); | |
var hours = minutes / 60; | |
if (days == 0) | |
{ | |
if (minutes == 0) | |
{ | |
var secs = minutes * 60; | |
Console.WriteLine($"""{secs} second{(secs == 1 ? "" : "s")} ago"""); | |
} | |
else if (minutes < 60) | |
{ | |
Console.WriteLine($"""{minutes} minute{(minutes == 1 ? "" : "s")} ago"""); | |
} | |
else | |
{ | |
var hrs = Math.Floor(minutes / 60); | |
var mins = Math.Round(minutes % 60); | |
var hoursElapsed = $"""{hrs} hour{(hrs == 1 ? "" : "s")}"""; | |
if (mins > 0) | |
{ | |
hoursElapsed += $""" {mins} minute{(mins == 1 ? "" : "s")}"""; | |
} | |
Console.WriteLine($"""{hoursElapsed} ago"""); | |
} | |
} | |
else | |
{ | |
var mins = Math.Round(minutes % 60); | |
var years = Math.Round(days / 365); | |
var timeElapsed = $"""{years} year{(years == 1 ? "" : "s")}"""; | |
if (years < 1) | |
{ | |
timeElapsed = $"""{days} day{(days == 1 ? "" : "s")}"""; | |
} | |
else | |
{ | |
var daysElapsed = Math.Round((days % 365 * 24) / 24); | |
timeElapsed += $""" {daysElapsed} day{(daysElapsed == 1 ? "" : "s")}"""; | |
} | |
if (hours > 1) | |
{ | |
var hrs = Math.Floor(hours); | |
mins = Math.Round(hours % 1.0 * 60); | |
timeElapsed += $""" {hrs} hour{(hrs == 1 ? "" : "s")}"""; | |
} | |
if (mins > 0) | |
{ | |
timeElapsed += $""" {mins} minute{(mins == 1 ? "" : "s")}"""; | |
} | |
Console.WriteLine($"""{timeElapsed} ago"""); | |
} | |
} | |
/// <summary>Prints the content of an array.</summary> | |
public static void Print<T>(this T[] array) | |
{ | |
Console.WriteLine($"[{string.Join(", ", array)}]"); | |
} | |
/// <summary>Renders a jagged array in tabularised format.</summary> | |
/// <remark>Assumes a monospaced font (fixed-width) font is used in the standard output.</remark> | |
public static void Tabularise<T>(this T[][] array) where T : INumber<T> | |
{ | |
var rows = array.Length; | |
var cols = array[0].Length; | |
var line = ""; | |
var spaces = 4; | |
/* | |
For a 5 * 5 array, there are 5 columns. | |
Rendering with spaces on either side of the content of a cell, that is 5 * (2 + 1) characters. | |
In addition we render the pipe symbol (|) on the left side of each content and one more at the end of the row. | |
That adds up to 5 * (2 + 1) + 6 (| characters) = 21 characters in total. | |
Given spaces = 4, columns = 5 => (columns * spaces) | |
(5 * 4) + 1 = 21 (— em dashes) characters for the line. | |
*/ | |
// Create line | |
for (int i = 0; i < (cols * spaces) + 1; i++) | |
{ | |
line += "—"; | |
} | |
Console.WriteLine($"A {rows}x{cols} matrix"); | |
Console.WriteLine(line); | |
for (var i = 0; i < rows; i++) | |
{ | |
var data = string.Join(" | ", array[i]); | |
Console.WriteLine($"| {data} |\n{line}"); | |
} | |
} | |
/// <summary>Renders a 2D array in a tabularised format.</summary> | |
/// <remark>Assumes a monospaced font (fixed-width) font is used in the standard output.</remark> | |
public static void Tabularise<T>(this T[,] array) where T : INumber<T> | |
{ | |
var rows = array.GetLength(0); | |
var cols = array.GetLength(1); | |
var line = ""; | |
var spaces = 4; | |
// Create line | |
for (int i = 0; i < (cols * spaces) + 1; i++) | |
{ | |
line += "—"; | |
} | |
Console.WriteLine($"A {rows}x{cols} matrix"); | |
Console.WriteLine(line); | |
for (var i = 0; i < rows; i++) | |
{ | |
var data = string.Join(" | ", GetRow(array, i)); | |
Console.WriteLine($"| {data} |\n{line}"); | |
} | |
T[] GetRow(T[,] matrix, int rowNumber) | |
{ | |
return Enumerable.Range(0, cols) | |
.Select(x => matrix[rowNumber, x]) | |
.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment