Rep-string: Difference between revisions
Content added Content deleted
(Added F# version) |
|||
Line 582: | Line 582: | ||
(no repetition) |
(no repetition) |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
<lang fsharp>let isPrefix p (s : string) = s.StartsWith(p) |
|||
let getPrefix n (s : string) = s.Substring(0,n) |
|||
let repPrefixOf str = |
|||
let rec isRepeatedPrefix p s = |
|||
if isPrefix p s then isRepeatedPrefix p (s.Substring (p.Length)) |
|||
else isPrefix s p |
|||
let rec getLongestRepeatedPrefix n = |
|||
if n = 0 then None |
|||
elif isRepeatedPrefix (getPrefix n str) str then Some(getPrefix n str) |
|||
else getLongestRepeatedPrefix (n-1) |
|||
getLongestRepeatedPrefix (str.Length/2) |
|||
[<EntryPoint>] |
|||
let main argv = |
|||
printfn "Testing for rep-string (and showing the longest repeated prefix in case):" |
|||
[ |
|||
"1001110011" |
|||
"1110111011" |
|||
"0010010010" |
|||
"1010101010" |
|||
"1111111111" |
|||
"0100101101" |
|||
"0100100" |
|||
"101" |
|||
"11" |
|||
"00" |
|||
"1" |
|||
] |> |
|||
List.map (fun s -> |
|||
match repPrefixOf s with | None -> s + ": NO" | Some(p) -> s + ": YES ("+ p + ")") |
|||
|> List.iter (printfn "%s") |
|||
0</lang> |
|||
{{out}} |
|||
<pre>Testing for rep-string (and showing the longest repeated prefix in case): |
|||
1001110011: YES (10011) |
|||
1110111011: YES (1110) |
|||
0010010010: YES (001) |
|||
1010101010: YES (1010) |
|||
1111111111: YES (11111) |
|||
0100101101: NO |
|||
0100100: YES (010) |
|||
101: NO |
|||
11: YES (1) |
|||
00: YES (0) |
|||
1: NO</pre> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |