Kernighans large earthquake problem

Revision as of 02:22, 21 April 2018 by rosettacode>Paddy3118 (New draft task with awk and python examples.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Brian Kernighan, in a lecture at the University of Nottingham, described a problem on which this task is based.

Kernighans large earthquake problem is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Problem

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:

8/27/1883    Krakatoa            8.8
5/18/1980    MountStHelens       7.6
3/13/2009    CostaRica           5.1
Task
  1. Create a program or script invocation to find all the events with magnitude greater than 6
  2. Assuming an appropriate name e.g. "data.txt" for the file:
    1. Either: Show how your program is invoked to process a data file of that name.
    2. Or: Incorporate the file name into the program, (as it is assumed that the program is single use).

AWK

<lang awk> awk '$3 > 6' data.txt</lang>


Python

Typed into a bash shell or similar: <lang python>python -c ' with open("data.txt") as f:

   for ln in f:
       if float(ln.strip().split()[2]) > 6:
           print(ln.strip())'</lang>