Kevin Berridge avatar Kevin Berridge committed 7d701a2

adds tests, fixes pfister case

Comments (0)

Files changed (1)

   if str.Length >= 3 then
     Seq.take 3 str |> Array.ofSeq
   else
-    (Array.append (Array.create 2 '0') (Array.ofList str)).[0..2]
+    (Array.append (Array.ofList str) (Array.create 2 '0')).[0..2]
     
 let soundex (input: string) =
-  let firstC = input.[0]
+  let firstC = System.Char.ToUpper(input.[0])
   let name = 
-    input.[1..] |> Seq.toList
+    input.ToLower() |> Seq.toList
     |> List.map replaceWithCode
     |> removeAllDups
+    |> List.tail
     |> removeNonSoundexChars
     |> padOrChop
 
-  firstC, name
+  new System.String(Array.append [|firstC|] name)
 
-let input = "Ashcraft"
-let (firstC, name) = soundex input
-printfn "%c, %A" firstC name
+let tests = [
+  "Robert", "R163";
+  "Rupert", "R163";
+  "Rubin", "R150";
+  "Ashcraft", "A261";
+  "Ashcroft", "A261";
+  "Tymczak", "T522";
+  "Pfister", "P236";
+]
+
+let test (name, expected) =
+  let actual = soundex name
+  if actual = expected then
+    None
+  else
+    Some(sprintf "%s expected %s got %s" name expected actual)
+
+List.map test tests
+  |> List.iter (fun t -> 
+      if Option.isSome t then
+        printfn "%s" t.Value
+      else
+        ()
+    )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.