How to resolve the algorithm Longest string challenge step by step in the Kotlin programming language
How to resolve the algorithm Longest string challenge step by step in the Kotlin programming language
Table of Contents
Problem Statement
This "longest string challenge" is inspired by a problem that used to be given to students learning Icon. Students were expected to try to solve the problem in Icon and another language with which the student was already familiar. The basic problem is quite simple; the challenge and fun part came through the introduction of restrictions. Experience has shown that the original restrictions required some adjustment to bring out the intent of the challenge and make it suitable for Rosetta Code.
Write a program that reads lines from standard input and, upon end of file, writes the longest line to standard output. If there are ties for the longest line, the program writes out all the lines that tie. If there is no input, the program should produce no output.
Implement a solution to the basic problem that adheres to the spirit of the restrictions (see below). Describe how you circumvented or got around these 'restrictions' and met the 'spirit' of the challenge. Your supporting description may need to describe any challenges to interpreting the restrictions and how you made this interpretation. You should state any assumptions, warnings, or other relevant points. The central idea here is to make the task a bit more interesting by thinking outside of the box and perhaps by showing off the capabilities of your language in a creative way. Because there is potential for considerable variation between solutions, the description is key to helping others see what you've done. This task is likely to encourage a variety of different types of solutions. They should be substantially different approaches. Given the input: the output should be (possibly rearranged):
Because of the variety of languages on Rosetta Code and the wide variety of concepts used in them, there needs to be a bit of clarification and guidance here to get to the spirit of the challenge and the intent of the restrictions. The basic problem can be solved very conventionally, but that's boring and pedestrian. The original intent here wasn't to unduly frustrate people with interpreting the restrictions, it was to get people to think outside of their particular box and have a bit of fun doing it. The guiding principle here should be to be creative in demonstrating some of the capabilities of the programming language being used. If you need to bend the restrictions a bit, explain why and try to follow the intent. If you think you've implemented a 'cheat', call out the fragment yourself and ask readers if they can spot why. If you absolutely can't get around one of the restrictions, explain why in your description. Now having said that, the restrictions require some elaboration.
To make this a bit more concrete, here are a couple of specific examples: In C, a string is an array of chars, so using a couple of arrays as strings is in the spirit while using a second array in a non-string like fashion would violate the intent. In APL or J, arrays are the core of the language so ruling them out is unfair. Meeting the spirit will come down to how they are used. Please keep in mind these are just examples and you may hit new territory finding a solution. There will be other cases like these. Explain your reasoning. You may want to open a discussion on the talk page as well.
At the end of the day for the implementer this should be a bit of fun. As an implementer you represent the expertise in your language, the reader may have no knowledge of your language. For the reader it should give them insight into how people think outside the box in other languages. Comments, especially for non-obvious (to the reader) bits will be extremely helpful. While the implementations may be a bit artificial in the context of this task, the general techniques may be useful elsewhere.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Longest string challenge step by step in the Kotlin programming language
This source code reads all the lines of a text file and calculates the longest one.
It then prints each line ordered by length, with the longest one being the first.
First, the code reads the file lines.txt
, and saves the contents in a variable lines
, which is a string.
Then, it iterates over the lines in the file, saving the longest line in the variable longest
.
The function longer
is used to compare the length of two strings, returning true if the first one is longer than the second one.
The function plus
is used to concatenate two strings.
Finally, the code prints the lines in the file, ordered by length, with the longest one being the first.
An alternative way to do this is to use the readLines
function, which returns a list of strings, and then use the groupBy
function to group the lines by length, and the maxBy
function to get the group with the maximum length.
Finally, the joinToString
function is used to concatenate the lines in each group.
Source code in the kotlin programming language
// version 1.1.0
import java.io.File
import java.util.*
fun longer(a: String, b: String): Boolean =
try {
a.substring(b.length)
false
}
catch (e: StringIndexOutOfBoundsException) {
true
}
fun main(args: Array<String>) {
var lines = ""
var longest = ""
val sc = Scanner(File("lines.txt"))
while(sc.hasNext()) {
val line = sc.nextLine()
if (longer(longest, line)) {
longest = line
lines = longest
}
else if (!longer(line, longest))
lines = lines.plus("\n").plus(line) // using 'plus' to avoid using '+'
}
sc.close()
println(lines);
println()
// alternatively (but cheating as library functions will use comparisons and lists under the hood)
println(File("lines.txt").readLines().groupBy { it.length }.maxBy { it.key }!!.value.joinToString("\n"))
}
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Factor programming language
You may also check:How to resolve the algorithm Multi-base primes step by step in the Factor programming language
You may also check:How to resolve the algorithm Terminal control/Inverse video step by step in the BaCon programming language
You may also check:How to resolve the algorithm Abundant odd numbers step by step in the R programming language