-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCalc.fs
More file actions
40 lines (35 loc) · 1.64 KB
/
StringCalc.fs
File metadata and controls
40 lines (35 loc) · 1.64 KB
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
module StringCalc
open System
exception InvalidExpressionException of string
exception NegativeNumbersException of string * int list
let INVALID_EXRESSION_ERR_CODE = -1001
let NEGATIVE_NUMBERS_ERR_CODE = -1002
let add numbers : int =
try
match List.filter (fun num -> Int16.TryParse num = (false, 0s)) numbers with
| [] ->
let intNumbersList = List.map ((fun num -> num.ToString() |> Convert.ToInt16) >> (int)) numbers
match List.filter(fun num -> num < 0 ) intNumbersList with
| [] ->
List.sum (List.filter(fun num -> num <= 1000) intNumbersList)
| negativeNumbersList ->
raise(NegativeNumbersException("negatives not allowed", negativeNumbersList))
| _ -> raise(InvalidExpressionException("Invalid Expression"))
with
| InvalidExpressionException (msg) ->
printfn "%s\n" msg
INVALID_EXRESSION_ERR_CODE
| NegativeNumbersException (msg, negativeNumbers) ->
printfn "%s %A\n" msg negativeNumbers
NEGATIVE_NUMBERS_ERR_CODE
let StringCalc (expression : string) =
match expression.Length with
| 0 -> 0
| _ ->
match expression.StartsWith "//" with
| true ->
let delimiters = expression.Substring(2, expression.IndexOf('\n') - 2).Split([|"[";"]"|], System.StringSplitOptions.RemoveEmptyEntries)
let numbers = expression.Substring(expression.IndexOf('\n') + 1)
add(numbers.Split(delimiters, System.StringSplitOptions.None) |> Seq.toList)
| false ->
add(expression.Split([|",";"\n"|], System.StringSplitOptions.None) |> Seq.toList)