Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,101 +1,91 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var fn = "readings.txt"
|
||||
const (
|
||||
filename = "readings.txt"
|
||||
readings = 24 // per line
|
||||
fields = readings*2 + 1 // per line
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := os.Open(fn)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
var (
|
||||
badRun, maxRun int
|
||||
badDate, maxDate string
|
||||
fileSum float64
|
||||
fileAccept int
|
||||
)
|
||||
for lr := bufio.NewReader(f); ; {
|
||||
line, pref, err := lr.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if pref {
|
||||
fmt.Println("Unexpected long line.")
|
||||
return
|
||||
}
|
||||
f := strings.Fields(string(line))
|
||||
if len(f) != 49 {
|
||||
fmt.Println("unexpected format,", len(f), "fields.")
|
||||
return
|
||||
}
|
||||
var accept int
|
||||
var sum float64
|
||||
for i := 1; i < 49; i += 2 {
|
||||
flag, err := strconv.Atoi(f[i+1])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if flag > 0 { // value is good
|
||||
if badRun > 0 { // terminate bad run
|
||||
if badRun > maxRun {
|
||||
maxRun = badRun
|
||||
maxDate = badDate
|
||||
}
|
||||
badRun = 0
|
||||
}
|
||||
value, err := strconv.ParseFloat(f[i], 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
sum += value
|
||||
accept++
|
||||
} else { // value is bad
|
||||
if badRun == 0 {
|
||||
badDate = f[0]
|
||||
}
|
||||
badRun++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Line: %s Reject %2d Accept: %2d Line_tot:%9.3f",
|
||||
f[0], 24-accept, accept, sum)
|
||||
if accept > 0 {
|
||||
fmt.Printf(" Line_avg:%8.3f\n", sum/float64(accept))
|
||||
} else {
|
||||
fmt.Println("")
|
||||
}
|
||||
fileSum += sum
|
||||
fileAccept += accept
|
||||
}
|
||||
fmt.Println("\nFile =", fn)
|
||||
fmt.Printf("Total = %.3f\n", fileSum)
|
||||
fmt.Println("Readings = ", fileAccept)
|
||||
if fileAccept > 0 {
|
||||
fmt.Printf("Average = %.3f\n", fileSum/float64(fileAccept))
|
||||
}
|
||||
if badRun > 0 && badRun > maxRun {
|
||||
maxRun = badRun
|
||||
maxDate = badDate
|
||||
}
|
||||
if maxRun == 0 {
|
||||
fmt.Println("\nAll data valid.")
|
||||
} else {
|
||||
fmt.Printf("\nMax data gap = %d, beginning on line %s.\n",
|
||||
maxRun, maxDate)
|
||||
}
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
var (
|
||||
badRun, maxRun int
|
||||
badDate, maxDate string
|
||||
fileSum float64
|
||||
fileAccept int
|
||||
)
|
||||
endBadRun := func() {
|
||||
if badRun > maxRun {
|
||||
maxRun = badRun
|
||||
maxDate = badDate
|
||||
}
|
||||
badRun = 0
|
||||
}
|
||||
s := bufio.NewScanner(file)
|
||||
for s.Scan() {
|
||||
f := strings.Fields(s.Text())
|
||||
if len(f) != fields {
|
||||
log.Fatal("unexpected format,", len(f), "fields.")
|
||||
}
|
||||
var accept int
|
||||
var sum float64
|
||||
for i := 1; i < fields; i += 2 {
|
||||
flag, err := strconv.Atoi(f[i+1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if flag <= 0 { // value is bad
|
||||
if badRun++; badRun == 1 {
|
||||
badDate = f[0]
|
||||
}
|
||||
} else { // value is good
|
||||
endBadRun()
|
||||
value, err := strconv.ParseFloat(f[i], 64)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
sum += value
|
||||
accept++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Line: %s Reject %2d Accept: %2d Line_tot:%9.3f",
|
||||
f[0], readings-accept, accept, sum)
|
||||
if accept > 0 {
|
||||
fmt.Printf(" Line_avg:%8.3f\n", sum/float64(accept))
|
||||
} else {
|
||||
fmt.Println()
|
||||
}
|
||||
fileSum += sum
|
||||
fileAccept += accept
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
endBadRun()
|
||||
|
||||
fmt.Println("\nFile =", filename)
|
||||
fmt.Printf("Total = %.3f\n", fileSum)
|
||||
fmt.Println("Readings = ", fileAccept)
|
||||
if fileAccept > 0 {
|
||||
fmt.Printf("Average = %.3f\n", fileSum/float64(fileAccept))
|
||||
}
|
||||
if maxRun == 0 {
|
||||
fmt.Println("\nAll data valid.")
|
||||
} else {
|
||||
fmt.Printf("\nMax data gap = %d, beginning on line %s.\n",
|
||||
maxRun, maxDate)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
89
Task/Text-processing-1/Java/text-processing-1.java
Normal file
89
Task/Text-processing-1/Java/text-processing-1.java
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import java.io.File;
|
||||
import java.util.*;
|
||||
import static java.lang.System.out;
|
||||
|
||||
public class TextProcessing1 {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Locale.setDefault(new Locale("en", "US"));
|
||||
Metrics metrics = new Metrics();
|
||||
|
||||
int dataGap = 0;
|
||||
String gapBeginDate = null;
|
||||
try (Scanner lines = new Scanner(new File("readings.txt"))) {
|
||||
while (lines.hasNextLine()) {
|
||||
|
||||
double lineTotal = 0.0;
|
||||
int linePairs = 0;
|
||||
int lineInvalid = 0;
|
||||
String lineDate;
|
||||
|
||||
try (Scanner line = new Scanner(lines.nextLine())) {
|
||||
|
||||
lineDate = line.next();
|
||||
|
||||
while (line.hasNext()) {
|
||||
final double value = line.nextDouble();
|
||||
if (line.nextInt() <= 0) {
|
||||
if (dataGap == 0)
|
||||
gapBeginDate = lineDate;
|
||||
dataGap++;
|
||||
lineInvalid++;
|
||||
continue;
|
||||
}
|
||||
lineTotal += value;
|
||||
linePairs++;
|
||||
|
||||
metrics.addDataGap(dataGap, gapBeginDate, lineDate);
|
||||
dataGap = 0;
|
||||
}
|
||||
}
|
||||
metrics.addLine(lineTotal, linePairs);
|
||||
metrics.lineResult(lineDate, lineInvalid, linePairs, lineTotal);
|
||||
}
|
||||
metrics.report();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Metrics {
|
||||
private List<String[]> gapDates;
|
||||
private int maxDataGap = -1;
|
||||
private double total;
|
||||
private int pairs;
|
||||
private int lineResultCount;
|
||||
|
||||
void addLine(double tot, double prs) {
|
||||
total += tot;
|
||||
pairs += prs;
|
||||
}
|
||||
|
||||
void addDataGap(int gap, String begin, String end) {
|
||||
if (gap > 0 && gap >= maxDataGap) {
|
||||
if (gap > maxDataGap) {
|
||||
maxDataGap = gap;
|
||||
gapDates = new ArrayList<>();
|
||||
}
|
||||
gapDates.add(new String[]{begin, end});
|
||||
}
|
||||
}
|
||||
|
||||
void lineResult(String date, int invalid, int prs, double tot) {
|
||||
if (lineResultCount >= 3)
|
||||
return;
|
||||
out.printf("%10s out: %2d in: %2d tot: %10.3f avg: %10.3f%n",
|
||||
date, invalid, prs, tot, (prs > 0) ? tot / prs : 0.0);
|
||||
lineResultCount++;
|
||||
}
|
||||
|
||||
void report() {
|
||||
out.printf("%ntotal = %10.3f%n", total);
|
||||
out.printf("readings = %6d%n", pairs);
|
||||
out.printf("average = %010.3f%n", total / pairs);
|
||||
out.printf("%nmaximum run(s) of %d invalid measurements: %n",
|
||||
maxDataGap);
|
||||
for (String[] dates : gapDates)
|
||||
out.printf("begins at %s and ends at %s%n", dates[0], dates[1]);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue