How to resolve the algorithm Abbreviations, automatic step by step in the Wren programming language
How to resolve the algorithm Abbreviations, automatic step by step in the Wren programming language
Table of Contents
Problem Statement
The use of abbreviations (also sometimes called synonyms, nicknames, AKAs, or aliases) can be an easy way to add flexibility when specifying or using commands, sub─commands, options, etc.
It would make a list of words easier to maintain (as words are added, changed, and/or deleted) if the minimum abbreviation length of that list could be automatically (programmatically) determined.
For this task, use the list (below) of the days-of-the-week names that are expressed in about a hundred languages (note that there is a blank line in the list). Caveat: The list (above) most surely contains errors (or, at the least, differences) of what the actual (or true) names for the days-of-the-week.
To make this Rosetta Code task page as small as possible, if processing the complete list, read the days-of-the-week from a file (that is created from the above list).
Notes concerning the above list of words
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abbreviations, automatic step by step in the Wren programming language
Source code in the wren programming language
import "io" for File
import "./pattern" for Pattern
import "./seq" for Lst
import "./fmt" for Fmt
var p = Pattern.new("+1/s")
var lines = File.read("days_of_week.txt").split("\n").map { |l| l.trim() }
var i = 1
for (line in lines) {
if (line == "") {
if (i != lines.count) System.print()
} else {
var days = p.splitAll(line)
if (days.count != 7) Fiber.abort("There aren't seven days in line %(i).")
if (Lst.distinct(days).count < 7) { // implies some days have the same name
System.print(" ∞ %(line)")
} else {
var len = 1
while (true) {
if (Lst.distinct(days.map { |d| d.take(len).join() }.toList).count == 7) {
Fmt.print("$2d $s", len, line)
break
}
len = len + 1
}
}
}
i = i + 1
}
You may also check:How to resolve the algorithm Run-length encoding step by step in the C# programming language
You may also check:How to resolve the algorithm Mind boggling card trick step by step in the Java programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the Ring programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Quackery programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Simula programming language