How to resolve the algorithm Word wrap step by step in the Dyalect programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Word wrap step by step in the Dyalect programming language

Table of Contents

Problem Statement

Even today, with proportional fonts and complex layouts, there are still cases where you need to wrap text at a specified column.

The basic task is to wrap a paragraph of text in a simple way in your language.
If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia. Show your routine working on a sample of text at two different wrap columns.

Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm.
If your language provides this, you get easy extra credit, but you must reference documentation indicating that the algorithm is something better than a simple minimum length algorithm. If you have both basic and extra credit solutions, show an example where the two algorithms give different results.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Word wrap step by step in the Dyalect programming language

Source code in the dyalect programming language

let loremIpsum = <[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius sapien
vel purus hendrerit vehicula. Integer hendrerit viverra turpis, ac sagittis arcu
pharetra id. Sed dapibus enim non dui posuere sit amet rhoncus tellus
consectetur. Proin blandit lacus vitae nibh tincidunt cursus. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam tincidunt
purus at tortor tincidunt et aliquam dui gravida. Nulla consectetur sem vel
felis vulputate et imperdiet orci pharetra. Nam vel tortor nisi. Sed eget porta
tortor. Aliquam suscipit lacus vel odio faucibus tempor. Sed ipsum est,
condimentum eget eleifend ac, ultricies non dui. Integer tempus, nunc sed
venenatis feugiat, augue orci pellentesque risus, nec pretium lacus enim eu
nibh.]>
 
func wrap(text, lineWidth) {
    String.Concat(values: wrapWords(text.Split('\s', '\r', '\n'), lineWidth))
}
and wrapWords(words, lineWidth) {
    var currentWidth = 0
 
    for word in words {
        if currentWidth != 0 {
            if currentWidth + word.Length() < lineWidth {
                currentWidth += 1
                yield " "
            } else {
                currentWidth = 0
                yield "\n"
            }
        }
        currentWidth += word.Length()
        yield word
    }
}
and printWrap(at) {
    print("Wrap at \(at):")
    print(wrap(loremIpsum, at))
    print()
}
 
printWrap(at: 72)
printWrap(at: 80)

  

You may also check:How to resolve the algorithm Power set step by step in the E programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Fantom programming language
You may also check:How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the Ring programming language
You may also check:How to resolve the algorithm File modification time step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the J programming language