Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,74 @@
using System;
class ColumnAlignerProgram
{
delegate string Justification(string s, int width);
static string[] AlignColumns(string[] lines, Justification justification)
{
const char Separator = '$';
// build input table and calculate columns count
string[][] table = new string[lines.Length][];
int columns = 0;
for (int i = 0; i < lines.Length; i++)
{
string[] row = lines[i].TrimEnd(Separator).Split(Separator);
if (columns < row.Length) columns = row.Length;
table[i] = row;
}
// create formatted table
string[][] formattedTable = new string[table.Length][];
for (int i = 0; i < formattedTable.Length; i++)
{
formattedTable[i] = new string[columns];
}
for (int j = 0; j < columns; j++)
{
// get max column width
int columnWidth = 0;
for (int i = 0; i < table.Length; i++)
{
if (j < table[i].Length && columnWidth < table[i][j].Length)
columnWidth = table[i][j].Length;
}
// justify column cells
for (int i = 0; i < formattedTable.Length; i++)
{
if (j < table[i].Length)
formattedTable[i][j] = justification(table[i][j], columnWidth);
else
formattedTable[i][j] = new String(' ', columnWidth);
}
}
// create result
string[] result = new string[formattedTable.Length];
for (int i = 0; i < result.Length; i++)
{
result[i] = String.Join(" ", formattedTable[i]);
}
return result;
}
static string JustifyLeft(string s, int width) { return s.PadRight(width); }
static string JustifyRight(string s, int width) { return s.PadLeft(width); }
static string JustifyCenter(string s, int width)
{
return s.PadLeft((width + s.Length) / 2).PadRight(width);
}
static void Main()
{
string[] input = {
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$",
"are$delineated$by$a$single$'dollar'$character,$write$a$program",
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$",
"column$are$separated$by$at$least$one$space.",
"Further,$allow$for$each$word$in$a$column$to$be$either$left$",
"justified,$right$justified,$or$center$justified$within$its$column.",
};
foreach (string line in AlignColumns(input, JustifyCenter))
{
Console.WriteLine(line);
}
}
}

View file

@ -0,0 +1,52 @@
using System;
using System.Linq;
enum Justification { Left, Center, Right }
public class Program
{
static void Main()
{
string text =
@"Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
column$are$separated$by$at$least$one$space.
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column.";
AlignColumns(text, Justification.Left);
Console.WriteLine();
AlignColumns(text, Justification.Center);
Console.WriteLine();
AlignColumns(text, Justification.Right);
}
public static void AlignColumns(string text, Justification justification) =>
AlignColumns(text.Split(Environment.NewLine), justification);
public static void AlignColumns(string[] lines, Justification justification) =>
AlignColumns(lines.Select(line => line.Split('$')).ToArray(), justification);
public static void AlignColumns(string[][] table, Justification justification)
{
Console.WriteLine(justification + ":");
int columns = table.Max(line => line.Length);
var columnWidths =
Enumerable.Range(0, columns)
.Select(i => table.Max(line => i < line.Length ? line[i].Length : 0)
).ToArray();
foreach (var line in table) {
Console.WriteLine(string.Join(" ",
Enumerable.Range(0, line.Length)
.Select(i => justification switch {
Justification.Left => line[i].PadRight(columnWidths[i]),
Justification.Right => line[i].PadLeft(columnWidths[i]),
_ => line[i].PadLeft(columnWidths[i] / 2).PadRight(columnWidths[i])
})
));
}
}
}