How to resolve the algorithm Align columns step by step in the Go programming language
How to resolve the algorithm Align columns step by step in the Go 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 Go programming language
This program reads a string and prints the string with the fields aligned in columns. The program first creates a new formatter, passing the string to be formatted. The formatter then splits the string into lines and words, and calculates the width of each column. The formatter then prints the string with the fields aligned in columns, using the specified justification (left, middle, or right).
Here is a breakdown of the code:
- The
package main
line declares that this is the main program file. - The
import
lines import the necessary libraries. - The
const text
line declares a constant string that will be formatted. - The
type formatter
line declares a new type calledformatter
. - The
newFormatter
function creates a new formatter from a string. - The
const
lines declare constants for the different justification options. - The
(f formatter)
line declares a new formatter variable. - The
for _, line := range strings.Split(text, "\n")
loop splits the string into lines. - The
words := strings.Split(line, "$")
line splits each line into words. - The
for words[len(words)-1] == ""
loop removes any empty words from the end of the line. f.text = append(f.text, words)
line appends the words to the formatter's text array.- The
for i, word := range words
loop iterates over the words in the line. - The
if i == len(f.width)
condition checks if this is the first word in the line. - The
f.width = append(f.width, len(word))
line adds the width of the word to the formatter's width array. - The
else if len(word) > f.width[i]
condition checks if the word is wider than the current width of the column. - The
f.width[i] = len(word)
line updates the width of the column to the width of the word. - The
return &f
line returns a pointer to the formatter. - The
(f formatter)
line declares a new formatter variable. - The
f := newFormatter(text)
line creates a new formatter from the text. - The
f.print(left)
line prints the string with the fields aligned in columns, using left justification. - The
f.print(middle)
line prints the string with the fields aligned in columns, using middle justification. - The
f.print(right)
line prints the string with the fields aligned in columns, using right justification.
Source code in the go programming language
package main
import (
"fmt"
"strings"
)
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.`
type formatter struct {
text [][]string
width []int
}
func newFormatter(text string) *formatter {
var f formatter
for _, line := range strings.Split(text, "\n") {
words := strings.Split(line, "$")
for words[len(words)-1] == "" {
words = words[:len(words)-1]
}
f.text = append(f.text, words)
for i, word := range words {
if i == len(f.width) {
f.width = append(f.width, len(word))
} else if len(word) > f.width[i] {
f.width[i] = len(word)
}
}
}
return &f
}
const (
left = iota
middle
right
)
func (f formatter) print(j int) {
for _, line := range f.text {
for i, word := range line {
fmt.Printf("%-*s ", f.width[i], fmt.Sprintf("%*s",
len(word)+(f.width[i]-len(word))*j/2, word))
}
fmt.Println("")
}
fmt.Println("")
}
func main() {
f := newFormatter(text)
f.print(left)
f.print(middle)
f.print(right)
}
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the K programming language
You may also check:How to resolve the algorithm IBAN step by step in the Raku programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Plain English programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Zoea programming language