【发布时间】:2016-11-12 01:53:17
【问题描述】:
在past posting 中,我询问了Bash 中用于将文本列逐行对齐的命令。我很清楚,所需的任务(即,按行对齐不同大小和内容的文本列)比最初预期的要复杂得多,而且建议的 answer 虽然对于过去的发布是可以接受的,但在大多数情况下是不够的经验数据集。因此,我想向社区查询以下伪代码。具体来说,我想知道是否可以优化以下伪代码以及以何种方式优化。
假设一个文件包含 n 列字符串。某些字符串可能丢失,其他字符串可能重复。最长的列可能不是文件中列出的第一个列,而是参考列。必须保持此引用列的行顺序。
> cat file # where n=3; first row contains column headers
CL1 CL2 CL3
foo foo bar
bar baz qux
baz qux
qux foo
bar
伪代码尝试 1(完全不足):
Shuffle columns so that columns ordered by size (i.e., longest column is first in matrix)
Rownames = strings of first column (i.e., of longest column)
For rownames
For (colname among columns 2:end)
if (string in current cell == rowname) {keep string in location}
if (string in current cell != rowname) {
if (string in current cell == rowname of next row) {add row to bottom of table; move each string of current column one row down}
if (string in current cell != rowname of next row) {add row to bottom of table; move each string of all other columns one row down}
}
按大小排列列:
> cat file_columns_ordered_by_size
CL2 CL1 CL3
foo foo bar
baz bar qux
qux baz
foo qux
bar
寻求的输出:
> my_code_here file_columns_ordered_by_size
CL2 CL1 CL3
foo foo
bar bar
baz baz
qux qux qux
foo
bar
【问题讨论】:
标签: optimization pattern-matching pseudocode text-alignment