Align columns: Difference between revisions

Improved first D version
(Removed redundant second D version)
(Improved first D version)
Line 623:
<lang d>import std.stdio, std.string, std.algorithm, std.typetuple;
 
void showTextTablesmain(string txt) {
auto text =
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n" ~
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n" ~
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n"~
"column$are$separated$by$at$least$one$space.\n" ~
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n" ~
"justified,$right$justified,$or$center$justified$within$its$column.";
 
string[][] parts;
foreach (line; txttext.splitlines())
parts ~= chomp(line.split("$"), [""]);
auto maxCol = reduce!max(map!q{ a.length }(parts));
Line 638 ⟶ 646:
 
// Display the justified parts
foreach (j, justify; [&rjustifyljustify!string, &center!string,
&ljustifyrjustify!string]) {
writeln(["Left", "Right", "Center"][j],
" column-aligned output:\n");
foreach (line; parts) {
foreach (idx, word; line)
Line 647 ⟶ 657:
writeln("- ".repeat(52));
}
}
 
void main() {
auto text =
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n" ~
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n" ~
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n"~
"column$are$separated$by$at$least$one$space.\n" ~
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n" ~
"justified,$right$justified,$or$center$justified$within$its$column.";
 
showTextTables(text);
}</lang>
Output:
<pre>Left column-aligned output:
<pre> Given a text file of many lines, where fields within a line
 
are delineated by a single 'dollar' character, write a program
Given thata aligns text each column file of fields many lines, by ensuring where that fields within wordsa in eachline
are column are separated delineated by at least a one space. single 'dollar' character, write a program
that Further, aligns allow each for eachcolumn of word fields by in ensuring that a columnwords in to be either lefteach
justified,column are right justified, or centerseparated justified by within at itsleast column one space.
GivenFurther, allow a for text each word filein of manya lines, column where to fields within abe either lineleft
justified, right justified, or center justified within its column.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Right column-aligned output:
 
Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program
Line 675 ⟶ 677:
justified, right justified, or center justified within its column.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Center column-aligned output:
Given a text file of many lines, where fields within a line
 
are delineated by a single 'dollar' character, write a program
that Given aligns each a column of text fieldsfile by of many ensuring that lines, words where fields within in a eachline
column are delineated separated by at least one space. a single 'dollar' character, write a program
Further, allow that for aligns each column word inof fields a by ensuring columnthat towords in be either lefteach
justified, right column justified, or are center justifiedseparated within by its at column least one space.
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </pre>
 
Anonymous user