-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissingData.fs
67 lines (58 loc) · 1.7 KB
/
MissingData.fs
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
module MissingData
open System.IO
type Student = {
Id: string
Surname: string
GivenName: string
MeanScore: float
MinScore: float
MaxScore: float
}
module Float =
let tryFromString s =
if s = "N/A" then
None
else
(float s) |> Some
let fromStringOrDefaultValue dv s =
s
|> tryFromString
|> Option.defaultValue dv
module Student =
let namePart (s: string) =
let names = s.Split(",")
names[0].Trim(), names[1].Trim()
let fromString (s: string) =
let rows = s.Split('\t')
let surname, givenName = namePart rows[0]
let id = rows[1]
// let scores =
// rows
// |> Array.skip 2
// |> Array.choose Float.tryFromString
let scores =
rows
|> Array.skip 2
|> Array.map (Float.fromStringOrDefaultValue 50.0)
let minScore = scores |> Array.min
let meanScore = scores |> Array.average
let maxScore = scores |> Array.max
{
Surname = surname
GivenName = givenName
Id = id
MeanScore = meanScore
MaxScore = maxScore
MinScore = minScore
}
let printSummary student =
printfn $"%s{student.Surname}\t%s{student.GivenName}\t%s{student.Id}\t%.1f{student.MeanScore}\t%.1f{student.MaxScore}\t%.1f{student.MinScore}"
let summarize filePath =
let rows = File.ReadAllLines filePath
let count = (rows |> Array.length) - 1
printfn $"Student count: %i{count}"
rows
|> Array.skip 1
|> Array.map Student.fromString
|> Array.sortBy _.Surname
|> Array.iter Student.printSummary