blob: 6393aadc45240e72bc4d0c4999bd2fd886ba6f1b (
plain)
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
|
namespace Common
module Util =
open System.Globalization
open FParsec
open FSharpPlus
let parse parser input =
match run parser input with
| Success (result, _, _) -> result
| _ -> failwith "Invalid input format!"
let countDistinct seq = seq |> Set |> Set.count
let countWhere pred = Seq.filter pred >> Seq.length
let charToInt = CharUnicodeInfo.GetDigitValue
let cutInHalf xs =
let half = Seq.length xs / 2
[ Seq.take half xs; Seq.skip half xs ]
let splitStringToTuple sep str =
match Seq.toList <| String.split [ sep ] str with
| [ x; y ] -> x, y
| _ -> failwith "Invalid string format!"
let matrixToString m =
m
|> Seq.map (Seq.map string >> String.concat "")
|> String.concat "\n"
let topN n xs =
let rec insertSorted x =
function
| h :: t -> min h x :: (insertSorted (max h x) t)
| _ -> [ x ]
Seq.fold
(fun acc x ->
if List.length acc < n then
insertSorted x acc
elif List.head acc < x then
insertSorted x <| List.tail acc
else
acc)
List.empty
xs
|