Abbreviations, automatic: Difference between revisions

→‎{{header|Kotlin}}: Changed code so that lines are now printed as soon as they're processed
(Added Kotlin)
(→‎{{header|Kotlin}}: Changed code so that lines are now printed as soon as they're processed)
Line 156:
fun main(args: Array<String>) {
val lines = File("days_of_week.txt").readLines()
val minLens = IntArray(lines.size)
for ((i, line) in lines.withIndex()) {
if (line.trim().isEmpty()) {
minLens[i] = 0 // denotes blank lineprintln()
continue
}
Line 165 ⟶ 164:
if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}")
if (days.distinct().size < 7) { // implies some days have the same name
minLens[i]println(" = -1 // denotes infinite length$line")
continue
}
Line 171 ⟶ 170:
while (true) {
if (days.map { it.take(len) }.distinct().size == 7) {
minLens[i]println("${"%2d".format(len)} = len$line")
break
}
len++
}
}
for ((i, line) in lines.withIndex()) {
val num = if (minLens[i] >0) "%2d".format(minLens[i])
else if (minLens[i] == 0) " "
else " ∞"
println("$num $line")
}
}</lang>
 
9,482

edits