Anonymous user
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
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;
parts ~= chomp(line.split("$"), [""]);
auto maxCol = reduce!max(map!q{ a.length }(parts));
Line 638 ⟶ 646:
// Display the justified parts
foreach (j, justify; [&
&
writeln(["Left", "Right", "Center"][j],
" column-aligned output:\n");
foreach (line; parts) {
foreach (idx, word; line)
Line 647 ⟶ 657:
writeln("- ".repeat(52));
}
▲ 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.";
}</lang>
Output:
<pre>Left column-aligned output:
Given
are
that
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
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </pre>
|