How to resolve the algorithm Kernighans large earthquake problem step by step in the Kotlin programming language
Published on 22 June 2024 08:30 PM
How to resolve the algorithm Kernighans large earthquake problem step by step in the Kotlin programming language
Table of Contents
Problem Statement
Brian Kernighan, in a lecture at the University of Nottingham, described a problem on which this task is based.
You are given a a data file of thousands of lines; each of three whitespace
separated fields: a date, a one word name and the magnitude of the event.
Example lines from the file would be lines like:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kernighans large earthquake problem step by step in the Kotlin programming language
This Kotlin code reads a file called data.txt
and prints all the lines where the third column (field 2 when splitting the line by spaces) is a double greater than 6.0.
- The first line imports the
java.io.File
class, which is used to read the file. - The second line defines a regular expression
r
that matches one or more whitespace characters. - The third line prints a message to the console.
- The fourth line uses the
forEachLine
function to iterate over each line of the filedata.txt
. - Inside the loop, the line is split into fields by the regular expression
r
. - The third field (index 2) is converted to a double using the
toDouble()
function. - If the double is greater than 6.0, the line is printed to the console.
Source code in the kotlin programming language
// Version 1.2.40
import java.io.File
fun main(args: Array<String>) {
val r = Regex("""\s+""")
println("Those earthquakes with a magnitude > 6.0 are:\n")
File("data.txt").forEachLine {
if (it.split(r)[2].toDouble() > 6.0) println(it)
}
}
You may also check:How to resolve the algorithm Palindrome detection step by step in the Falcon programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the Microsoft Small Basic programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the Crystal programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the EMal programming language