Last active
August 29, 2015 14:22
-
-
Save jeremyabbott/ed18a42aa760d8cf4c47 to your computer and use it in GitHub Desktop.
Get Loop and Tail lengths from Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Text.RegularExpressions; | |
let buildList args = | |
Regex.Matches(args, "(\d\,\s?\d)") | |
|> Seq.cast<Match> | |
|> Seq.map (fun m -> m.Value.Replace(" ", "").Split([|','|])) | |
|> Seq.toList | |
let findDuplicatePointer list = | |
let rec findCycleRec (oldList:list<array<string>>) (newList:list<array<string>>) = | |
match oldList with | |
| [] -> None | |
| x::xs when List.exists (fun (i:array<string>) -> i.[0] = x.[1]) newList -> Some x | |
| x::xs -> findCycleRec xs (x::newList) | |
findCycleRec list [] | |
let getListInfo list maybeElement = | |
let index = | |
match maybeElement with | |
| Some (x:array<string>) -> (List.findIndex (fun (i:array<string>) -> i.[1] = x.[1]) list) | |
| None -> -1 | |
let tailLength = index + 1 | |
let loopLength = (List.length list) - tailLength | |
(tailLength, loopLength) | |
let printListInfo listArgs = | |
let list = buildList listArgs | |
let element = findDuplicatePointer list | |
printfn "%A" (getListInfo list element) | |
[<EntryPoint>] | |
let main argv = | |
printListInfo "[1, 2], [2, 3], [3, 4], [4, 2]" | |
printListInfo "[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 3]" | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment