How to resolve the algorithm Text processing/2 step by step in the Nim programming language
How to resolve the algorithm Text processing/2 step by step in the Nim programming language
Table of Contents
Problem Statement
The following task concerns data that came from a pollution monitoring station with twenty-four instruments monitoring twenty-four aspects of pollution in the air. Periodically a record is added to the file, each record being a line of 49 fields separated by white-space, which can be one or more space or tab characters. The fields (from the left) are: i.e. a datestamp followed by twenty-four repetitions of a floating-point instrument value and that instrument's associated integer flag. Flag values are >= 1 if the instrument is working and < 1 if there is some problem with it, in which case that instrument's value should be ignored. A sample from the full data file readings.txt, which is also used in the Text processing/1 task, follows: Data is no longer available at that link. Zipped mirror available here
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Text processing/2 step by step in the Nim programming language
Source code in the nim programming language
import strutils, tables
const NumFields = 49
const DateField = 0
const FlagGoodValue = 1
var badRecords: int # Number of records that have invalid formatted values.
var totalRecords: int # Total number of records in the file.
var badInstruments: int # Total number of records that have at least one instrument showing error.
var seenDates: Table[string, bool] # Table to keep track of what dates we have seen.
proc checkFloats(floats: seq[string]): bool =
## Ensure we can parse all records as floats (except the date stamp).
for index in 1..<NumFields:
try:
# We're assuming all instrument flags are floats not integers.
discard parseFloat(floats[index])
except ValueError:
return false
true
proc areAllFlagsOk(instruments: seq[string]): bool =
## Ensure that all sensor flags are ok.
# Flags start at index 2, and occur every 2 fields.
for index in countup(2, NumFields, 2):
# We're assuming all instrument flags are floats not integers
var flag = parseFloat(instruments[index])
if flag < FlagGoodValue: return false
true
# Note: we're not checking the format of the date stamp.
# Main.
var currentLine = 0
for line in "readings.txt".lines:
currentLine.inc
if line.len == 0: continue # Empty lines don't count as records.
var tokens = line.split({' ', '\t'})
totalRecords.inc
if tokens.len != NumFields:
badRecords.inc
continue
if not checkFloats(tokens):
badRecords.inc
continue
if not areAllFlagsOk(tokens):
badInstruments.inc
if seenDates.hasKeyOrPut(tokens[DateField], true):
echo tokens[DateField], " duplicated on line ", currentLine
let goodRecords = totalRecords - badRecords
let goodInstruments = goodRecords - badInstruments
echo "Total Records: ", totalRecords
echo "Records with wrong format: ", badRecords
echo "Records where all instruments were OK: ", goodInstruments
You may also check:How to resolve the algorithm Command-line arguments step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Red programming language
You may also check:How to resolve the algorithm AVL tree step by step in the Haskell programming language
You may also check:How to resolve the algorithm XML/Output step by step in the Python programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Rascal programming language