forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.fs
40 lines (35 loc) · 1.12 KB
/
array.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
/// The program is from http://codereview.stackexchange.com/q/17882
module ArrayEx
open System
let private reorder cap (array : int []) =
if Array.isEmpty array then array
else
let max = array |> Array.max
if max = cap then
let newArray = Array.create array.Length 1
newArray.[array.Length - 1] <- cap
newArray
else
array |> Array.map (fun elem ->
if elem = max then max
else 1)
let private getZeroes todo =
todo
|> Seq.takeWhile (fun elem -> elem = 0)
|> Seq.toArray
let private getNumbers todo cap =
todo
|> Seq.takeWhile (fun elem -> elem <> 0)
|> Seq.toArray
|> reorder cap
let GetEquivalentPermutation(array : int [], cap) =
let rec joinParts finished todo =
if Seq.isEmpty todo then finished |> Seq.toArray
else
let zeroes = getZeroes todo
let nextTodo = todo |> Seq.skip zeroes.Length
let numbers = getNumbers nextTodo cap
let finalTodo = nextTodo |> Seq.skip numbers.Length
let newFinished = Seq.append (Seq.append finished zeroes) numbers
joinParts newFinished finalTodo
joinParts [] array