Commits

jjmeyer0 committed 8ad4694

Added initial commit for this project.

Comments (0)

Files changed (4)

data_structures/binary_tree.ml

+module Binary_tree = struct
+
+	type 'a t = Leaf | Node 'a * a t * 'a t
+
+	let empty = Leaf;;
+
+	let insert v = function
+		| Leaf -> Node (v, empty, empty)
+		| Node (v', l, r) as t ->
+			if v > v' then Node (v', l, insert v r)
+			else if v < v' then Node (v', insert v l, r)
+			else if v = v' then t 
+	;;
+
+	let delete v = function
+		| Leaf -> empty
+		| Node (v', l, r) -> 
+			if v = v' then empty
+			else if v > v' then Node (v', l, delete v r)
+			else if v < v' then Node (v', delete v l, r)
+	;;
+
+	let tree_of_list values = List.fold_right insert values empty;;
+	
+end

data_structures/trie.ml

+module Trie = struct
+	type 'a t = TNone | Exact | Next of char option * (char * 'a t) list
+
+	let empty = TNone
+
+	let find word t = None
+
+	let insert word t = t
+
+	let delete word t = t
+end
+
+
+
+let _ = 
+	let t = Trie.insert "hat" (Trie.insert "cat" Trie.empty) in
+
+	assert ((Trie.find "hat" t) = (Some "hat"));
+	assert ((Trie.find "cat" t) = (Some "cat"));
+	assert ((Trie.find "ffffff" t) = None);
+	assert ((Trie.find "cats" t) = None);
+;;

validation/cusip_validator.ml

+open Core.Std;;
+
+let char_list cusip = List.rev (String.to_list_rev cusip);;
+
+let map_char c = match Char.get_digit c with
+	| (Some _) as c' -> c'
+	| None when Char.is_alpha c -> Some ((Char.to_int (Char.uppercase c)) - 55)
+	| None -> None
+;;
+
+let value_list = List.map ~f:map_char;;
+
+let is_valid_format cusip =
+	if (String.length cusip) <> 8 then false
+	else (
+		match char_list cusip with
+			| a::b::c::_ -> Char.is_digit a && Char.is_digit b && Char.is_digit c
+			| _ -> false
+	)
+;;
+
+let get_check_digit cusip = 
+	if not (is_valid_format cusip) then None
+	else ( 
+		let digits = value_list (char_list cusip) in
+		if List.exists ~f:(fun f -> f = None) digits then None
+		else (
+			let values = List.mapi ~f:(fun i e -> if i mod 2 = 0 then e else e * 2) (List.filter_opt digits) in
+			let sum = List.fold_left ~f:(fun v v' -> v + v' / 10 + v' mod 10) ~init:0 values in
+			let check_digit = (10 - (sum mod 10)) mod 10 in
+			Some check_digit	
+		)
+	)
+;;
+
+let validate cusip = 
+	if (String.length cusip) <> 9 then false
+	else (
+		match get_check_digit (String.slice cusip 0 8) with
+			| None -> false
+			| Some check_digit' -> 
+				match Char.get_digit (Char.of_string (String.slice cusip 8 9)) with
+					| None -> false
+					| Some check_digit -> check_digit = check_digit'
+	)
+;;
+
+(* Tests 
+let _ = 
+	let valid_cusips_check_digit = [
+		"14149YAR",9; "126650BG",4; "254709AC",2; "437076AQ",5; "441060AG",5;
+		"50075NAN",4; "574599BE",5; "617446B9",9; "637640AC",7; "713291AL",6;
+		"852061AE",0; "887317AA",3; "925524BF",6; "125509BG",3; "125896AV",2;
+	];
+
+	(* Make sure all valid test cases calculate the correct check digit *)
+	List.iter ~f:(fun (l,r) -> assert (Cusip_validator.get_check_digit l = Some r)) valid_cusips_check_digit;
+
+	let valid_full_cusip = List.map ~f:(fun (l,r) -> l ^ (string_of_int r)) valid_cusips_check_digit in
+
+	(* Make sure a 9 digit cusip properly validates. *)
+	List.iter ~f:(fun c -> assert (validate c)) valid_full_cusip
+;;*)

validation/cusip_validator.mli

Empty file added.