How to resolve the algorithm Align columns step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Align columns step by step in the zkl programming language

Table of Contents

Problem Statement

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. Use the following text to test your programs:

Note that:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Align columns step by step in the zkl programming language

Source code in the zkl programming language

fcn format(text,how){
   words:=text.split("$").apply("split").flatten();
   max:=words.reduce(fcn(p,n){ n=n.len(); n>p and n or p },0);
   wordsPerCol:=80/(max+1);
   fmt:=(switch(how){
      case(-1){ "%%-%ds ".fmt(max).fmt }
      case(0) { fcn(max,w){
            a:=(max-w.len())/2; b:=max-w.len() - a; String(" "*a,w," "*b);
         }.fp(max)
      }
      case(1){ "%%%ds ".fmt(max).fmt }
   });
   w:=words.walker(); d:=Data(0,Int);
   do{ w.pump(wordsPerCol,d,fmt).append("\n") } while(not w.atEnd);
   d.text;
}

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.\n";

format(text,-1).print();
format(text, 0).print();
format(text, 1).print();

  

You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Animate a pendulum step by step in the E programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Arturo programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the ECL programming language