Advertisement
ptrelford

Permute digits

Jul 14th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.72 KB | None | 0 0
  1. let letters =
  2.     [
  3.     '0', ['0']
  4.     '1', ['1']
  5.     '2', ['A';'B';'C']
  6.     '3', ['D';'E';'F']
  7.     '4', ['G';'H';'I']
  8.     '5', ['J';'K';'L']
  9.     '6', ['M';'N';'O']
  10.     '7', ['P';'Q';'R';'S']
  11.     '8', ['T';'U';'V']
  12.     '9', ['W';'X';'Y';'Z']
  13.     ]
  14.     |> dict
  15.  
  16. let rec permuteRec = function
  17.     | "" -> [""]
  18.     | s ->
  19.         let hd, tl = s.[0], s.Substring(1)
  20.         [for s in permuteRec(tl) do
  21.             for c in letters.[hd] -> c.ToString() + s]
  22.  
  23. let permute (digits:string) =
  24.     let words = ref [""]
  25.     for digit in digits do
  26.         words :=
  27.             [for word in words.Value do
  28.                 for c in letters.[digit] ->
  29.                 c.ToString() + word]
  30.     words.Value
  31.  
  32. permute "3663"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement