Advertisement
Guest User

anagram_test_md5

a guest
Jul 22nd, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.63 KB | None | 0 0
  1.  type SeqMonad() =
  2.     member t.Bind(m,f) = Seq.concat(Seq.map f m)
  3.     member t.Return v = seq{ yield v }
  4.   let seqMonad = SeqMonad()
  5.  
  6.   let permutations ls =
  7.     let rec insertions x = function
  8.       | []             -> [[x]]
  9.       | (y :: ys) as l -> (x::l)::(List.map (fun x -> y::x) (insertions x ys))
  10.    let rec permutations' = function
  11.     | []      -> seq [ [] ]
  12.     | x :: xs -> Seq.concat (Seq.map (insertions x) (permutations' xs))
  13.    ls |> permutations'
  14.  
  15. let md5 s =
  16.   System.BitConverter
  17.     .ToString(
  18.       System.Security.Cryptography.MD5
  19.         .Create()
  20.         .ComputeHash(buffer = System.Text.Encoding.UTF8.GetBytes(s = s)))
  21.     .Replace("-", System.String.Empty)
  22.     .ToLower()
  23.  
  24. let factorial n =
  25.   let rec fact acc = function | 0 -> acc | i -> fact (acc * i) (i - 1)
  26.   (1,n) ||> fact
  27.  
  28. let unitTestPermutations () =
  29.   "FooBar"
  30.   |> Seq.toList
  31.   |> fun xs -> xs |> permutations |> Seq.length,
  32.               xs |> Seq.length   |> factorial
  33.   |> fun (x,y) -> x = y
  34.  
  35. let unitTestMD5 () =  
  36.   // [ mon@mbai7 tmp ] md5 -s "FooBar"
  37.   // MD5 ("FooBar") = f32a26e2a3a8aa338cd77b6e1263c535
  38.   "FooBar" |> md5 |> fun x -> x = "f32a26e2a3a8aa338cd77b6e1263c535"
  39.  
  40. (unitTestPermutations() && unitTestMD5()) |> function
  41.   | true -> ()
  42.   | false -> failwith "Must be n! permuations per string"
  43.  
  44. let cache file =
  45.   use reader = System.IO.File.OpenText(file)
  46.   let rec cache' acc = function
  47.      | true -> acc
  48.      | false -> cache' (acc |> Set.add(reader.ReadLine())) reader.EndOfStream
  49.   cache' Set.empty reader.EndOfStream
  50.  
  51.  let root     = __SOURCE_DIRECTORY__
  52.  let wordList = System.IO.Path.Combine(root,"wordlist.txt")
  53.  let anagram  = @"poultry outwits ants"
  54.  let hash     = @"4624d200580677270a54ccff86b9610e"
  55.  let words    = anagram.Split(char " ")
  56.  let cached   = wordList |> cache
  57.  
  58.  words |> Array.map(fun x  -> x  |> Seq.toList |> permutations)
  59.        |> Array.map(fun xs -> xs |> Seq.map(fun ys -> ys |> List.map string))
  60.        |> Array.map(fun xs -> xs |> Seq.map(fun ys -> ys |> List.reduce(+)))
  61.        |> Array.map(fun xs -> xs |> Seq.filter(fun x -> (x,cached) ||> Set.contains))
  62.        |> fun xs -> xs.[0],xs.[1],xs.[2]
  63.        |> fun (xs,ys,zs) -> seqMonad{let! x = xs
  64.                                      let! y = ys
  65.                                      let! z = zs
  66.                                      return (x,y,z)}
  67.        |> Seq.map(fun (x,y,z) -> x + " " + y + " " + z)
  68.        |> Seq.map(fun x -> x |> md5, x)
  69.        |> Seq.filter(fun (x,y) -> x = hash)
  70.        |> Seq.map(fun (x,y) -> y)
  71.        |> Seq.truncate 1
  72.        |> fun x -> x |> printfn "%A"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement