fork download
  1. let findGroupings lst =
  2. let rec findGroup input previous acc =
  3. match input with
  4. | [] -> acc
  5. | (a,b)::t ->
  6. match previous with
  7. | [] -> if a >= b then
  8. findGroup t [] acc
  9. else
  10. findGroup t [b;a] acc
  11. | h::_ -> if a > h && a < b then
  12. findGroup t (b::(a::previous)) acc
  13. elif a > h && a >=b then
  14. let full = List.rev (a::previous)
  15. findGroup t [] (full::acc)
  16. elif a >= b then
  17. findGroup t [] ((List.rev previous)::acc)
  18. elif a < h then
  19. findGroup t [b;a] (previous::acc)
  20. else // a = h and a < b
  21. findGroup t (b::previous) acc
  22. findGroup lst [] []
  23. |> List.rev
  24.  
  25. do
  26. let test tups =
  27. tups |> findGroupings |> printfn "%A"
  28. stdout.WriteLine ()
  29. test [(1M, 2M); (2M, 3M); (3M, 3M); (4M, 5M); (5M, 6M); (7M, 6M); (8M, 9M); (10M, 9M)]
  30. test [(1M, 2M); (3M, 3M); (4M, 5M); (7M, 6M); (8M, 9M); (10M, 9M); (5M, 6M); (2M, 3M)]
  31. test [(1M, 2M); (7M, 8M); (2M, 3M); (8M, 9M); (3M, 4M); (10M, 11M); (4M, 5M); (13M, 11M); (5M, 6M)]
Success #stdin #stdout 0.05s 31304KB
stdin
Standard input is empty
stdout
[[1M; 2M; 3M]; [4M; 5M; 6M; 7M]; [8M; 9M; 10M]]

[[1M; 2M; 3M]; [4M; 5M; 7M]; [8M; 9M; 10M]; [6M; 5M]]

[[8M; 7M; 2M; 1M]; [9M; 8M; 3M; 2M]; [11M; 10M; 4M; 3M]; [4M; 5M; 13M]]