Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
53
Task/Align-columns/Beads/align-columns.beads
Normal file
53
Task/Align-columns/Beads/align-columns.beads
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
beads 1 program 'Align columns'
|
||||
|
||||
const
|
||||
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.'''
|
||||
|
||||
var
|
||||
words : array^2 of str
|
||||
widths : array of num
|
||||
|
||||
calc div_line
|
||||
var s = '+'
|
||||
loop across:widths val:w
|
||||
s = s & str_repeat('-',w) & '+'
|
||||
log s
|
||||
|
||||
calc show_table(
|
||||
justify
|
||||
)
|
||||
loop across:words index:i
|
||||
var s = '|'
|
||||
loop across:widths index:j val:w
|
||||
var
|
||||
word = words[i,j]
|
||||
if word == U
|
||||
word = ''
|
||||
case justify
|
||||
| RIGHT
|
||||
s = s & pad_left(word,w)
|
||||
| LEFT
|
||||
s = s & pad_right(word,w)
|
||||
| CENTER
|
||||
w = w - str_len(word)
|
||||
s = s & str_repeat(' ',idiv(w,2)) & word & str_repeat(' ',idiv(w,2)+mod(w,2))
|
||||
s = s & '|'
|
||||
log s
|
||||
div_line
|
||||
|
||||
calc main_init
|
||||
var w
|
||||
split_lines_words (text, words, delim:'$')
|
||||
loop across:words index:i
|
||||
loop across:words[i] index:j
|
||||
w = str_len(words[i,j])
|
||||
widths[j] = max(w,widths[j])
|
||||
loop across:[LEFT CENTER RIGHT] val:v
|
||||
log "\n{v} justified\n"
|
||||
div_line
|
||||
show_table(v)
|
||||
Loading…
Add table
Add a link
Reference in a new issue