-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.fsx
More file actions
61 lines (56 loc) · 1.9 KB
/
main.fsx
File metadata and controls
61 lines (56 loc) · 1.9 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
open System
open System.Collections.Generic
exception NegativityError of string
//the entered string
let orginalStr = "//[@]\n2@3,8\n5\n7,25\n50@100@800\n1000"
//split the given string to the data an the delimerter
let splitString (str:string) =
if str.Contains("//") then
str.Split([|"\n"|],2, StringSplitOptions.RemoveEmptyEntries)
else
[|str|]
//get the splitter, or ',' by default
let getSplitter (str:string []) =
if str.Length <> 1 then
let splitter = str.[0].Split([|"//"|], StringSplitOptions.RemoveEmptyEntries).[0]
splitter.Split('[', ']') |> Array.filter (fun x -> x <> "") |> Array.append [|","; "\n"|]
else
[|","; "\n"|]
//get the data (numbers)
let getString (str:string []) =
if str.Length <> 1 then
str.[1]
else
str.[0]
//get the numbers from the data string
let split (splitBy:string[]) (str:string) =
str.Split(splitBy, StringSplitOptions.RemoveEmptyEntries)
//get the first two numbers only
let sliceArr (start:int) (length:int) (arr:string[]) =
if arr.Length > (start + length) then
Array.sub arr start length
else
arr
//check for negative numbers
let checkNegativity (arr:int []) =
let negatives = arr |> Array.filter (fun x -> x < 0)
if (negatives.Length > 0) then
let error = negatives |> Array.map string |> String.concat ", "
raise(NegativityError("Negatives not allowed " + error))
arr
//the add function
let add orginalStr =
let str = orginalStr |> splitString |> getString
let splitBy = orginalStr |> splitString |> getSplitter
let mutable sum = 1
try
sum <- str
|> split splitBy
|> Array.map int
|> Array.filter (fun x -> x < 1000)
|> checkNegativity
|> Array.sum
with
| NegativityError err -> failwith err
sum
printfn "Sum is: %d" (add orginalStr)