-
Notifications
You must be signed in to change notification settings - Fork 0
/
percentil.go
111 lines (101 loc) · 2.51 KB
/
percentil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/coc1961/percentil/calc"
)
func main() {
var pointerPercentil = flag.Int("p", 0, "Percentil a Calcular debe ser un número > 0 y < 100")
var pointerInputFile = flag.String("f", "", "Nombre de Archivo que contiene la tabla de datos, si se indica - se lee desde stdin")
flag.Parse()
if *pointerPercentil < 1 || *pointerPercentil > 99 {
flag.PrintDefaults()
os.Exit(1)
return
}
if *pointerInputFile == "" {
flag.PrintDefaults()
os.Exit(1)
return
}
var fReader io.Reader = bufio.NewReader(os.Stdin)
if *pointerInputFile != "-" {
var err error
fReader, err = os.Open(*pointerInputFile)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", err.Error())
os.Exit(1)
}
}
reader := csv.NewReader(fReader)
reader.Comma = ','
reader.FieldsPerRecord = -1
arr := make([][]string, 0)
for {
line, error := reader.Read()
if error == io.EOF {
break
} else if error != nil {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", error.Error())
os.Exit(1)
}
arr = append(arr, line)
}
if len(arr) == 0 {
return
}
per := calc.New()
if len(arr[0]) == 1 {
//No Agrupado
data := make([]float64, 0, len(arr))
for _, x := range arr {
if fl, err := strconv.ParseFloat(strings.Trim(x[0], " "), 64); err == nil {
data = append(data, fl)
} else {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", err.Error())
os.Exit(1)
}
}
if per.SetTable(data).Error() == nil {
if per.Calc(*pointerPercentil).Error() == nil {
fmt.Fprintf(os.Stdout, "%.2f", per.Result())
return
}
}
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", per.Error().Error())
os.Exit(1)
} else if len(arr[0]) == 2 {
//Agrupado
data := make([][]float64, 0, len(arr))
for _, x := range arr {
if len(x) < 2 {
continue
}
if fl, err := strconv.ParseFloat(strings.Trim(x[0], " "), 64); err == nil {
if fl1, err := strconv.ParseFloat(strings.Trim(x[1], " "), 64); err == nil {
data = append(data, []float64{fl, fl1})
} else {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", err.Error())
os.Exit(1)
}
} else {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", err.Error())
os.Exit(1)
}
}
if per.SetTable(data).Error() == nil {
if per.Calc(*pointerPercentil).Error() == nil {
fmt.Fprintf(os.Stdout, "%.2f", per.Result())
return
}
}
fmt.Fprintf(flag.CommandLine.Output(), "%s\n\n", per.Error().Error())
os.Exit(1)
}
}