-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainfall.hs
More file actions
27 lines (21 loc) · 725 Bytes
/
rainfall.hs
File metadata and controls
27 lines (21 loc) · 725 Bytes
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
module Codewars.G964.Rainfall where
import Data.List.Split
import Data.Maybe
mean :: String -> String -> Double
mean t s = case parse t s of
Nothing -> (-1.0)
Just x -> calcMean . parseNums $ x
calcMean :: [Double] -> Double
calcMean c = (sum c)/(realToFrac $ length c)
parse :: String->String -> Maybe String
parse town string = lookup town . map (cat . splitOn ":") . lines $ string
where
cat [a,b]=(a,b)
parseNums :: String->[Double]
parseNums = map (read . last . words) . splitOn ","
variance :: String -> String -> Double
variance t s = case parse t s of
Nothing-> (-1.0)
Just x -> calcVar . parseNums $ x
where
calcVar c= (sum $ map (\x-> (x-(calcMean c))**2) c)/(realToFrac $ length c)