How to resolve the algorithm Kernighans large earthquake problem step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Kernighans large earthquake problem step by step in the Swift 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 Swift programming language

Source code in the swift programming language

import Foundation

guard let path = Array(CommandLine.arguments.dropFirst()).first else {
  fatalError()
}

let fileData = FileManager.default.contents(atPath: path)!
let eventData = String(data: fileData, encoding: .utf8)!

for line in eventData.components(separatedBy: "\n") {
  guard let lastSpace = line.lastIndex(of: " "), // Get index of last space
        line.index(after: lastSpace) != line.endIndex, // make sure the last space isn't the end of the line
        let magnitude = Double(String(line[line.index(after: lastSpace)])),
        magnitude > 6 else { // Finally check the magnitude
    continue
  }

  print(line)
}


  

You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the Swift programming language
You may also check:How to resolve the algorithm Enumerations step by step in the AmigaE programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the FunL programming language
You may also check:How to resolve the algorithm Leap year step by step in the D programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the NewLISP programming language