all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
35
Task/Soundex/OCaml/soundex-1.ocaml
Normal file
35
Task/Soundex/OCaml/soundex-1.ocaml
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
let c2d = function
|
||||
| 'B' | 'F' | 'P' | 'V' -> "1"
|
||||
| 'C' | 'G' | 'J' | 'K' | 'Q' | 'S' | 'X' | 'Z' -> "2"
|
||||
| 'D' | 'T' -> "3"
|
||||
| 'L' -> "4"
|
||||
| 'M' | 'N' -> "5"
|
||||
| 'R' -> "6"
|
||||
| _ -> ""
|
||||
|
||||
let rec dbl acc = function
|
||||
| [] -> (List.rev acc)
|
||||
| [c] -> List.rev(c::acc)
|
||||
| c1::(c2::_ as tl) ->
|
||||
if c1 = c2
|
||||
then dbl acc tl
|
||||
else dbl (c1::acc) tl
|
||||
|
||||
let pad s =
|
||||
match String.length s with
|
||||
| 0 -> s ^ "000"
|
||||
| 1 -> s ^ "00"
|
||||
| 2 -> s ^ "0"
|
||||
| 3 -> s
|
||||
| _ -> String.sub s 0 3
|
||||
|
||||
let soundex_aux rem =
|
||||
pad(String.concat "" (dbl [] (List.map c2d rem)))
|
||||
|
||||
let soundex s =
|
||||
let s = String.uppercase s in
|
||||
let cl = ref [] in
|
||||
String.iter (fun c -> cl := c :: !cl) s;
|
||||
match dbl [] (List.rev !cl) with
|
||||
| c::rem -> (String.make 1 c) ^ (soundex_aux rem)
|
||||
| [] -> invalid_arg "soundex"
|
||||
31
Task/Soundex/OCaml/soundex-2.ocaml
Normal file
31
Task/Soundex/OCaml/soundex-2.ocaml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
let tests = [
|
||||
"Soundex", "S532";
|
||||
"Example", "E251";
|
||||
"Sownteks", "S532";
|
||||
"Ekzampul", "E251";
|
||||
"Euler", "E460";
|
||||
"Gauss", "G200";
|
||||
"Hilbert", "H416";
|
||||
"Knuth", "K530";
|
||||
"Lloyd", "L300";
|
||||
"Lukasiewicz", "L222";
|
||||
"Ellery", "E460";
|
||||
"Ghosh", "G200";
|
||||
"Heilbronn", "H416";
|
||||
"Kant", "K530";
|
||||
"Ladd", "L300";
|
||||
"Lissajous", "L222";
|
||||
"Wheaton", "W350";
|
||||
"Ashcraft", "A226";
|
||||
"Burroughs", "B622";
|
||||
"Burrows", "B620";
|
||||
"O'Hara", "O600";
|
||||
]
|
||||
|
||||
let () =
|
||||
print_endline " Word \t Code Found Status";
|
||||
List.iter (fun (word, code1) ->
|
||||
let code2 = soundex word in
|
||||
let status = if code1 = code2 then "OK " else "Arg" in
|
||||
Printf.printf " \"%s\" \t %s %s %s\n" word code1 code2 status
|
||||
) tests
|
||||
Loading…
Add table
Add a link
Reference in a new issue