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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Word wrap step by step in the Sidef 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 Sidef programming language

Source code in the sidef programming language

class String {
    method wrap(width) {
        var txt = self.gsub(/\s+/, " ")
        var len = txt.len
        var para = []
        var i = 0
        while (i < len) {
            var j = (i + width)
            while ((j < len) && (txt.char_at(j) != ' ')) { --j }
            para.append(txt.substr(i, j-i))
            i = j+1
        }
        return para.join("\n")
    }
}

var text = 'aaa bb cc ddddd'
say text.wrap(6)


class SmartWordWrap {

    has width = 80

    method prepare_words(array, depth=0, callback) {

        var root = []
        var len = 0
        var i = -1

        var limit = array.end
        while (++i <= limit) {
            len += (var word_len = array[i].len)

            if (len > width) {
                if (word_len > width) {
                    len -= word_len
                    array.splice(i, 1, array[i].split(width)...)
                    limit = array.end
                    --i; next
                }
                break
            }

            root << [
                array.first(i+1).join(' '),
                self.prepare_words(array.slice(i+1), depth+1, callback)
            ]

            if (depth.is_zero) {
                callback(root[0])
                root = []
            }

            break if (++len >= width)
        }

        root
    }

    method combine(root, path, callback) {
        var key = path.shift
        path.each { |value|
            root << key
            if (value.is_empty) {
                callback(root)
            }
            else {
                value.each { |item|
                    self.combine(root, item, callback)
                }
            }
            root.pop
        }
    }

    method wrap(text, width) {

        self.width = width
        var words = (text.kind_of(Array) ? text : text.words)

        var best = Hash(
            score => Inf,
            value => [],
        )

        self.prepare_words(words, callback: { |path|
            self.combine([], path, { |combination|
                var score = 0
                combination.first(-1).each { |line|
                    score += (width - line.len -> sqr)
                }

                if (score < best{:score}) {
                    best{:score} = score
                    best{:value} = []+combination
                }
            })
        })

        best{:value}.join("\n")
    }
}

var sww = SmartWordWrap()

var words = %w(aaa bb cc ddddd)
var wrapped = sww.wrap(words, 6)

say wrapped


  

You may also check:How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the Python programming language
You may also check:How to resolve the algorithm Lychrel numbers step by step in the BASIC programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the C# programming language