text
stringlengths
0
601k
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Mile) -> (Mile, val_ /. 0.3048 /. 5280.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Mile, Meter) -> (Meter, val_ *. 5280. *. 0.3048) | (Mile, Foot) -> (Foot, val_ *. 5280.) | _ -> (from_unit, val_) ;; let get_val ((unit, val_) : 'a value) : float = val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((fd, ft), (td, tt)) = (from_unit, to_unit) in (to_unit, get_val (convert_dist (fd, get_val (convert_time (tt, val_) ft)) td)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. get_val (convert_speed a b_unit)) ;;
let passes_da_vinci t = let rec test root queue = match (test_ss root, queue) with (false, _) -> false | (true, []) -> true | (true, a :: l) -> test a (l @ get_children a) in test t (get_children t) ;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> (if cur_num > max_num then cur_el else max_el) | hd::tl -> (if cur_el = hd && max_num>=cur_num then aux tl (cur_el, cur_num + 1)(max_el, max_num) else (if cur_el = hd && max_num<cur_num then aux tl (cur_el, cur_num + 1)(cur_el, cur_num) else aux tl (hd, 1)(max_el, max_num))) in if (List.length l > 0) then aux l(List.hd l,0)(List.hd l,0) else failwith "Cannot determine mode of list that is less than one element long.";;
let pair_mode (l: 'a list) : 'a * 'a = let l_hd = List.rev (List.tl (List.rev l)) in let l_tl = List.tl l in let comb_list = List.combine l_hd l_tl in if (List.length l > 1) then mode(comb_list) else failwith "Cannot determine pair mode of list that is less than two elements long.";;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> Hour, val_ *. (1./.3600.) | Hour, Second -> Second, val_ *. 3600. | Hour, Hour -> Hour, val_ | Second, Second -> Second, val_ ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (Meter, val_ *. 0.3048) | Foot, Mile -> Mile, val_ *. (1. /. 5280.) | Meter, Foot -> Foot, val_ *. (1. /. 0.3048) | Meter, Mile -> Mile, val_ *. ((1. /. 0.3048) *. (1. /. 5280.)) | Mile, Foot -> Foot, val_ *. 5280. | Mile, Meter -> Meter, val_ *. (5280. *. 0.3048) | Foot, Foot -> Foot, val_ | Meter, Meter -> Meter, val_ | Mile, Mile -> Mile, val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let to_distance = fst(to_unit) in let to_time = snd(to_unit) in let x = snd(convert_dist (fst(from_unit), 1.) to_distance) in let y = snd(convert_time (snd(from_unit), 1.) to_time) in to_unit, val_*.(x /. y) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = b_unit, snd(convert_speed a b_unit) +. b_val ;;
let passes_da_vinci t = let rec sum_total tail sum : float = match tail with | [] -> sum +. 0. | Leaf :: tl -> sum_total tl (sum +. 0.) | Branch (width, subtree) :: tl -> sum_total tl (sum +. (width *. width)) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> (if ((check subtree) && (sum_total subtree 0. <= width *. width)) then true else ( if ((check subtree) && (sum_total subtree 0. > width *. width)) then false else check tl) ) in check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if x = cur_el then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "The list must be bigger given the nature of the function." | x :: xs -> aux xs (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "The list must be bigger given the nature of the function." else let (_::xs) = l in let (_::xy) = List.rev l in let xy = List.rev xy in let pairs = List.combine xy xs in mode pairs ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let y = 3600. in match from_unit, to_unit with | Second, Hour -> (Hour, val_ /. y) | Hour, Second -> (Second, val_ *. y) | _ -> from_unit, val_ ;;
let convert_dist (from_unit, val_ : dist_unit value) to_unit : dist_unit value = let x = 0.3048 in let y = 5280. in let z = x *. y in let wanted_val = match (from_unit, to_unit) with | Foot, Meter -> val_ *. x | Foot, Mile -> val_ /. y | Meter, Mile -> val_ /. z | Meter, Foot -> val_ /. x | Mile, Meter -> val_ *. z | Mile, Foot -> val_ *. y | _ -> val_ in (to_unit, wanted_val) ;;
let convert_speed (from_unit, val_ : speed_unit value) to_unit : speed_unit value = let (_, convert_dist_val) = convert_dist (fst from_unit, val_) (fst to_unit) in let (_, factor) = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, convert_dist_val /. factor);;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let x = convert_time time (snd speed_unit) in (fst speed_unit, snd x *. speed_val) ;; let rec passes_da_vinci t = let square_width t = match t with | Leaf -> 0. | Branch (width, _) -> width *. width in match t with | Leaf -> true | Branch (width, trees) -> let sum = List.fold_left (fun acc x -> acc +. square_width x) 0. trees in if sum > width *. width then false else List.for_all passes_da_vinci trees ;;
let invalid_input () = failwith "Not a valid input.";;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x::xs -> if ((compare cur_el x) = 0) then aux xs (cur_el, (cur_num + 1)) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in let sorted_list = List.sort compare l in match sorted_list with | x::xs -> aux xs (x, 1) (x, 0) | _ -> invalid_input () ;; let pair_mode_helper (l: 'a list) = match l with | [] -> invalid_input () | x::xs -> xs ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> invalid_input () | x::xs -> let l1 = xs in let l2 = List.rev (pair_mode_helper (List.rev l)) in let consec_pair_list = List.combine l2 l1 in mode consec_pair_list ;; let wrong_unit () = failwith "Not a valid unit.";;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Hour -> (Hour, val_ /. 3600.) | Second -> (Second, val_) | _ -> wrong_unit ()) | Hour -> (match to_unit with | Hour -> (Hour, val_) | Second -> (Second, val_ *. 3600.) | _ -> wrong_unit ()) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) | _ -> wrong_unit ()) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, (val_ /. 0.3048) /. 5280.) | _ -> wrong_unit ()) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) | Mile -> (Mile, val_) | _ -> wrong_unit ()) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let converted_distance = convert_dist ((fst from_unit), 1.) (fst to_unit) in let converted_time = convert_time ((snd from_unit), 1.) (snd to_unit) in let (_, dist_val) = converted_distance in let (_, time_val) = converted_time in ( ((fst to_unit), (snd to_unit)), val_ *. dist_val /. time_val) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted_speed = convert_speed a b_unit in (b_unit, (snd converted_speed) +. b_val) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl (acc +. 0.) | Branch (width, _) :: tl -> sum_ tl (acc +. width ** 2.) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> ((check subtree) && (sum_ subtree 0.) <= width ** 2.) in check [t] ;;
let mode (l: 'a list) : 'a = let sorted_l = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if (cur_num > max_num) then cur_el else max_el | x::t -> if x = cur_el then aux t (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux t (x,1) (cur_el, cur_num) else aux t (x,1) (max_el, max_num) in let x::t = sorted_l in aux t (x,1) (x,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pair l acc = match l with | [] -> acc | _::[] -> acc | a::(b::_ as t) -> pair t ((a,b)::acc) in mode (pair l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with |Second, Second -> Second, val_ |Hour, Hour -> Hour, val_ |Second, Hour -> Hour, (val_ /. 3600.) |Hour, Second -> Second, (val_ *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with |Foot, Foot -> Foot, val_ |Foot, Meter -> Meter, (val_ *.0.3048) |Foot, Mile -> Mile, (val_ /. 5280.) |Mile, Mile -> Mile, val_ |Mile, Foot -> Foot, (val_ *. 5280.) |Mile, Meter -> Meter, (val_ *.1609.344) |Meter, Meter -> Meter, val_ |Meter, Foot -> Meter, (val_ /. 0.3048) |Meter, Mile -> Mile, (val_ /. 1609.344) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_dist,from_time),(to_dist,to_time)) = (from_unit, to_unit) in let (dist, dist_val) = convert_dist (from_dist, val_) to_dist in let (time, time_val) = convert_time (from_time, 1.) to_time in (dist, time), (dist_val /. time_val) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit, to_time) = speed_unit in let (from_time, from_time_val) = time in let (_ , time_val) = convert_time (from_time, from_time_val) to_time in dist_unit, (speed_val *. time_val) ;; let sum_of_squares l = let rec sum l acc = match l with | [] -> acc | x::t -> sum t (acc +. x**2.) in sum l 0. ;; let visit_children l = let rec visit l l_width = match l with | [] -> sum_of_squares l_width | x::t -> match x with | Leaf -> visit t (l_width @ [0.]) | Branch (width, _) -> visit t (l_width @ [width]) in visit l [] ;; let rec passes_da_vinci t = let rec helper_func t2 v = let rec helper l v = match l with | [] -> v | x::xs -> let y = helper_func x v in helper xs y in match t2 with | Leaf -> v | Branch(value ,children) -> let sumssqr_val = value**2. >= visit_children children in match v with | true -> helper children sumssqr_val | false -> helper children false in match t with |Leaf -> true |Branch (_,_) -> helper_func t true ;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) (pos: int) = if pos = List.length l then max_el else if cur_el = List.nth l pos then aux l (cur_el, (cur_num + 1)) (max_el, max_num) (pos + 1) else if cur_num > max_num then aux l ((List.nth l (pos)), 0) (cur_el, cur_num) pos else aux l ((List.nth l (pos)), 0) (max_el, max_num) pos in aux l ((List.nth l 0), 0) ((List.nth l 0), 0) 0 ;;
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l3 = List.rev l in let l2 = List.tl l3 in let l4 = List.combine l1 l2 in mode l4 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> ( match to_unit with |Second -> to_unit, val_ |Hour -> to_unit, (val_ /. 3600.) ) |Hour -> ( match to_unit with |Hour -> to_unit, val_ |Second -> to_unit, (val_ *. 3600.) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> (match to_unit with |Foot -> to_unit, val_ |Meter -> to_unit, (val_ *. 0.3048) |Mile -> to_unit, (val_ /. 5280.) ) |Meter -> (match to_unit with |Foot -> to_unit, (val_ /. 0.3048) |Meter -> to_unit, val_ |Mile -> to_unit, (val_ /. 0.3048 /. 5280.) ) |Mile -> (match to_unit with |Foot -> to_unit, (val_ *. 5280.) |Meter -> to_unit, (val_ *. 5280. *. 0.3048) |Mile -> to_unit, val_ ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist = fst from_unit in let from_time = snd from_unit in let to_dist = fst to_unit in let to_time = snd to_unit in let conv_dist = convert_dist (from_dist, val_) to_dist in let conv_time = convert_time (to_time, snd(conv_dist)) from_time in (to_unit, snd(conv_time)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_speed_unit = fst a in let a_value = snd a in let a_conv = convert_speed (a_speed_unit, a_value) b_unit in let speed_sum = snd a_conv +. b_val in b_unit, speed_sum ;;
let passes_da_vinci t = let branches_sum l = match l with |[] -> 0. |Leaf::tl -> 0. |Branch(width, subtree) :: tl -> (width *. width) in let sum_ l = match l with |[] -> 0. |Leaf::tl -> 0. |Branch(width, subtree)::tl -> branches_sum subtree in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> let sum = sum_ subtree in if ((check subtree) && (sum <= width *. width)) then false else check tl in check t ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd::tl -> (if hd=cur_el then(if cur_num=0 then aux tl (cur_el, 1) (cur_el, 1) else if cur_num=max_num then aux tl (cur_el, cur_num+1) (cur_el, cur_num+1) else aux tl (cur_el, cur_num+1) (max_el, max_num)) else aux tl (hd, 1) (max_el, max_num)) in match l with | [] -> failwith "Undefined Input" | l -> let l2 = List.sort compare (l) in aux l2 (List.hd l2, 0) (List.hd l2, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined Input" | [x] -> failwith "Undefined Input" | hd1::tl1 -> let tl2 = (List.tl (List.rev l)) in mode (List.combine (List.rev tl2) tl1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with | (Second, Second) -> (Second,val_) | (Hour, Hour) -> (Hour,val_) | (Second, Hour) -> (Hour,(val_ /. 3600.)) | (Hour, Second) -> (Second,(val_ *. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Foot) -> (Foot, val_) | (Meter, Meter) -> (Meter,val_) | (Mile, Mile) -> (Mile,val_) | (Foot, Meter) -> (Meter,(0.3048 *. val_)) | (Meter, Foot) -> (Foot,(val_ /. 0.3048)) | (Foot, Mile) -> (Mile,(val_ /. 5280.)) | (Mile, Foot) -> (Foot,(val_ *. 5280.)) | (Meter, Mile) -> (Mile,(val_ /. 0.3048 /. 5280.)) | (Mile, Meter) -> (Meter,(val_*.5280.*.0.3048)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (du1,du2,tu1,tu2) = (fst(from_unit),fst(to_unit),snd(from_unit),snd(to_unit)) in let val_t = snd(convert_time (tu2,val_) tu1) in let val_td = snd(convert_dist (du1, val_t) du2) in ((du2,tu2),val_td) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a2_val = snd(convert_speed a b_unit) in let rtn_val = a2_val +. b_val in (b_unit,rtn_val) ;;
let passes_da_vinci t = let rec helper tree_list acc = match tree_list with | [] -> acc | hd::tl -> match hd with | Leaf -> helper tl acc | Branch (f, _) -> (helper tl (acc +. f *. f)) in let rec check tree_list = match tree_list with | [] -> true | Leaf::tl -> check tl | Branch (width, subtree) :: tl -> let rst = width *. width in (if not((check subtree) && ((helper subtree 0.) <= rst)) then false else check tl) in check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] ->if cur_num>max_num then cur_el else if cur_num=max_num && cur_el < max_el then cur_el else max_el |x::xs -> if x==cur_el then if (cur_num+1>max_num) then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else if ((cur_num+1) = max_num && cur_el <=max_el) then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else aux xs (cur_el, cur_num+1) (max_el, max_num) else if (1>max_num) then aux xs (x,1) (x,1) else if (1=max_num && x <=max_el) then aux xs (x,1) (x,1) else aux xs (x,1) (max_el, max_num) in if List.length l<=0 then failwith "Empty List!" else aux (List.sort compare l) (List.hd l, 0) (List.hd l, 0) ;; let rec headlist (l: 'a list) :(l1: 'a list) = match l with |[]->;;
let pair_mode (l: 'a list) : 'a * 'a = let rec aux l1 (l1: 'a list) (l2: 'a list)= l2=List.tl l l1=[] ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = notimplemented () ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = notimplemented () ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = notimplemented () ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = notimplemented () ;; let rec passes_da_vinci t = notimplemented () ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] ->if cur_num>max_num then cur_el else if cur_num=max_num && cur_el < max_el then cur_el else max_el |x::xs -> if x=cur_el then if (cur_num+1>max_num) then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else if ((cur_num+1) = max_num && cur_el <=max_el) then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else aux xs (cur_el, cur_num+1) (max_el, max_num) else if (1>max_num) then aux xs (x,1) (x,1) else if (1=max_num && x <=max_el) then aux xs (x,1) (x,1) else aux xs (x,1) (max_el, max_num) in if List.length l<=0 then failwith "Empty List!" else aux (List.sort compare l) (List.hd l, 0) (List.hd l, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with |[] -> failwith "Empty List!" |[x] ->failwith "At least 2 entries!" |_ ->let l1=List.tl l in let l2=List.rev(List.tl (List.rev l)) in mode(List.combine l2 l1 ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit=Second then (to_unit, val_) else (to_unit,val_/.3600.) |Hour -> if to_unit=Second then (to_unit, val_*.3600.) else (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> if to_unit =Foot then (to_unit, val_) else if to_unit =Meter then (to_unit, val_*.0.3048) else (to_unit, val_/.5280.) |Meter -> if to_unit =Foot then (to_unit, val_/.0.3048) else if to_unit =Meter then (to_unit, val_) else (to_unit, val_/.1609.344) |Mile -> if to_unit =Foot then (to_unit, val_*.5280.) else if to_unit =Meter then (to_unit, val_*.1609.344) else (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ftu= snd from_unit in let fdu= fst from_unit in let ttu=snd to_unit in let tdu=fst to_unit in let val_1= snd (convert_dist (fdu, val_ ) tdu) in (to_unit,snd (convert_time(ttu, val_1) ftu)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let spval =b_val +.snd (convert_speed (a) b_unit) in (b_unit, spval) ;;
let passes_da_vinci t = let rec sum_ l acc= match l with |[]->acc |Leaf :: tl-> sum_ tl acc |Branch(width,subtree) ::tl->sum_ tl (acc+.width*.width) in let rec check l = match l with |[]-> true |Leaf :: tl-> check tl |Branch(width,subtree) ::tl-> if width*.width >= sum_ subtree 0. then let a = check subtree in if a then check tl else false else false in check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | h::t -> if cur_el = h then aux t (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in aux (List.sort compare (List.tl l)) (List.hd l, 1) (List.hd l, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pairs el list = match list with | [] -> [] | [i] -> (el, i)::[] | h::t -> (el, h)::(pairs h t) in mode (pairs (List.hd l) (List.tl l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, _) -> ( match to_unit with | Hour -> (Hour, val_ /. 3600.) | _ -> (Second, val_) ) | (Hour, _) -> ( match to_unit with | Second -> (Second, val_ *. 3600.) | _ -> (Hour, val_) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_) with | (Foot, _) -> ( match to_unit with | Meter -> (Meter, (val_ *. 0.3048)) | Mile -> (Mile, (val_ /. 5280.)) | _ -> (Foot, val_) ) | (Meter, _) -> ( match to_unit with | Foot -> (Foot, (val_ /. 0.3048)) | Mile -> (Mile, ((val_ /. 0.3048) /. 5280.)) | _ -> (Meter, val_) ) | (Mile, _) -> ( match to_unit with | Meter -> (Meter, ((val_ *. 5280.) *. 0.3048)) | Foot -> (Foot, (val_ *. 5280.)) | _ -> (Mile, val_) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let fst (x, y) = x in let scd (x, y) = y in match (from_unit, val_) with | ((Foot, t), val_) -> (match fst to_unit with | Meter -> if (scd to_unit) = t then ((Meter, t), scd (convert_dist (Foot, val_) Meter)) else ((Meter, (scd to_unit)), scd (convert_dist (Foot, (scd (convert_time (scd to_unit, val_) t))) Meter)) | Mile -> if (scd to_unit) = t then ((Mile, t), scd (convert_dist (Foot, val_) Mile)) else ((Mile, (scd to_unit)), scd (convert_dist (Foot, (scd (convert_time (scd to_unit, val_) t))) Mile)) | Foot -> if (scd to_unit) = t then ((Foot, t), val_) else ((Foot, (scd to_unit)), scd (convert_time (scd to_unit, val_) t)) ) | ((Meter, t), val_) -> (match fst to_unit with | Foot -> if (scd to_unit) = t then ((Meter, t), scd (convert_dist (Meter, val_) Foot)) else ((Foot, (scd to_unit)), scd (convert_dist (Meter, (scd (convert_time (scd to_unit, val_) t))) Foot)) | Mile -> if (scd to_unit) = t then ((Mile, t), scd (convert_dist (Meter, val_) Mile)) else ((Mile, (scd to_unit)), scd (convert_dist (Meter, (scd (convert_time (scd to_unit, val_) t))) Mile)) | Meter -> if (scd to_unit) = t then ((Meter, t), val_) else ((Meter, (scd to_unit)), scd (convert_time (scd to_unit, val_) t)) ) | ((Mile, t), val_) -> (match fst to_unit with | Meter -> if (scd to_unit) = t then ((Meter, t), scd (convert_dist (Mile, val_) Meter)) else ((Meter, (scd to_unit)), scd (convert_dist (Mile, (scd (convert_time (scd to_unit, val_) t))) Meter)) | Foot -> if (scd to_unit) = t then ((Foot, t), scd (convert_dist (Mile, val_) Foot)) else ((Foot, (scd to_unit)), scd (convert_dist (Mile, (scd (convert_time (scd to_unit, val_) t))) Foot)) | Mile -> if (scd to_unit) = t then ((Mile, t), val_) else ((Mile, (scd to_unit)), scd (convert_time (scd to_unit, val_) t)) ) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let fst (x, y) = x in let scd (x, y) = y in if scd speed_unit = fst time then (fst speed_unit, speed_val *. (scd time)) else (fst speed_unit, speed_val *. scd (convert_time time (scd speed_unit))) ;; let rec passes_da_vinci t = notimplemented () ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | h :: tl -> if h = cur_el then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (h, 1) (cur_el, cur_num) else aux tl (h, 1) (max_el, max_num) in match l with | [] -> failwith "undefined input" | h :: tl -> aux (List.sort compare l) (h, 0) (h, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec generate_bigram (bi_l : (('a * 'a) list)) (l : 'a list) (element : 'a)= match l with | [] -> bi_l | h :: tl -> generate_bigram ((element,h) :: bi_l) tl h in if (List.length l) < 2 then failwith "list too short" else mode (generate_bigram [] (List.tl l) (List.hd l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if to_unit = Hour then (to_unit, val_ /. 3600.) else (from_unit, val_) | Hour -> if to_unit = Second then (to_unit, val_ *. 3600.) else (from_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (from_unit, val_) | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.)) | Meter -> (match to_unit with | Foot -> (to_unit, val_ /. 0.3048) | Meter -> (from_unit, val_) | Mile -> (to_unit, val_ /. 0.3048 /. 5280.)) | Mile -> (match to_unit with | Foot -> (to_unit, val_ *. 5280.) | Meter -> (to_unit, val_ *. 5280. *. 0.3048) | Mile -> (from_unit, val_)) ;; let get_st_el (x,_) = x;; let get_nd_el (_,y) = y;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, (get_nd_el (convert_dist (get_st_el from_unit, val_) (get_st_el to_unit))) /. (get_nd_el (convert_time (get_nd_el from_unit, 1.0) (get_nd_el to_unit)))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, get_nd_el (convert_speed a b_unit) +. b_val) ;;
let passes_da_vinci t = match t with | Leaf -> true | Branch (width, subtree) -> if width *. width >= cal_sos subtree then true && (search_tree subtree) else false ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if cur_el = x then aux xs (cur_el,(cur_num+1)) (max_el,max_num) else if cur_num > max_num then aux xs (x,1) (cur_el,cur_num) else aux xs (x,1) (max_el,max_num) in aux (List.sort compare l) ((List.hd (List.sort compare l)),0) ((List.hd (List.sort compare l)),0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) = 2 then ((List.nth l 0),(List.nth l 1)) else let rec pair_list l (cur_list: ('a * 'a) list) = match l with | x1 :: x2 :: xs -> pair_list (List.cons x2 xs) (List.cons (x1,x2) cur_list) | _ -> mode cur_list in pair_list l [] ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> (match from_unit with | Second -> (to_unit, val_) | Hour -> (to_unit, (val_ *. 3600.)) ) | Hour -> (match from_unit with | Second -> (to_unit, val_ /. 3600.) | Hour -> (to_unit, val_) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> (match from_unit with | Foot -> (to_unit, val_) | Meter -> (to_unit, (val_ /. 0.3048)) | Mile -> (to_unit, (val_ *. 5280.)) ) | Meter -> (match from_unit with | Foot -> (to_unit, (val_ *. 0.3048)) | Meter -> (to_unit, val_) | Mile -> (to_unit, (val_ *. 5280. *. 0.3048)) ) | Mile -> (match from_unit with | Foot -> (to_unit, (val_ /. 5280.)) | Meter -> (to_unit, (val_ /. 0.3048 /. 5280.)) | Mile -> (to_unit, val_) ) ;; let first_el (pair : 'a * 'b) : 'a = let (first,_) = pair in first ;; let second_el (pair : 'a * 'b) : 'b = let (_,second) = pair in second ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let val_ = second_el (convert_dist ((first_el from_unit),val_) (first_el to_unit)) in let val_ = second_el (convert_time ((second_el to_unit),val_) (second_el from_unit)) in (to_unit, val_) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let val_ = b_val +. (second_el (convert_speed a b_unit)) in (b_unit, val_) ;;
let passes_da_vinci t = match t with | Leaf -> true | Branch (width,subtree) -> if (width ** 2.) < (branch_sum t) then false else let rec check_all tree_list = match tree_list with | [] -> true | x :: xs -> ( match x with | Leaf -> check_all xs | Branch (width, _) -> if (width ** 2.) < (branch_sum x) then false else check_all xs) in check_all (all_branch subtree []) ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> if cur_num > max_num then cur_el else max_el |hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num +1) (max_el, max_num) else if max_num >= cur_num then aux tl (hd, 1) (max_el, max_num) else aux tl (hd, 1) (cur_el, cur_num) in let sorted = List.sort compare l in match sorted with |[] -> failwith "empty list!" |hd :: tl -> aux tl (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "more elements required" else let l1 = List.tl l in let l2 = List.rev (List.tl (List.rev l)) in let tupleList = List.combine l2 l1 in mode tupleList ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit = Hour then (Hour, (val_ /. 3600.)) else (Second, val_) |Hour -> if to_unit = Second then (Second, (val_ *. 3600.)) else (Hour, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> if to_unit = Meter then (Meter, val_ *. 0.3048) else if to_unit = Mile then (Mile, val_ /. 5280.) else (Foot, val_) |Meter -> if to_unit = Foot then (Foot, val_ /. 0.3048) else if to_unit = Mile then (Mile, val_ /. (5280. *. 0.3048)) else (Meter, val_) |Mile -> if to_unit = Meter then (Meter, val_ *. (0.3048 *. 5280.)) else if to_unit = Foot then (Foot, val_ *. 5280.) else (Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let distance = convert_dist (fst from_unit, val_) (fst to_unit) in let time = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, snd distance /. snd time) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let timeInSpeedUnit = convert_time time (snd speed_unit) in (fst speed_unit, speed_val *. snd timeInSpeedUnit) ;; let rec passes_da_vinci t = let rec sum l acc = match l with | [] -> acc | Leaf :: tl -> sum tl acc | Branch (width, _) :: tl -> sum tl (acc +. width *. width) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not(check subtree && sum subtree 0. <= width *. width) then false else check tl in check [t] ;;
let mode (l: 'a list) : 'a = let rec aux lst ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | element :: lst -> if cur_el = element then aux lst (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux lst (element, 1) (cur_el, cur_num) else aux lst (element, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input." | element :: lst -> aux lst (element, 1) (element, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec run (lst: 'a list) (maintain: ('a * 'a) list) (last: 'a) = match lst with | [] -> mode maintain | element :: lst -> run lst ((last, element) :: maintain) element in match l with | [] -> failwith "Undefined input." | element :: lst -> run lst ([]) element ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) ) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) ) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /. 0.3048 /. 5280.) ) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) | Mile -> (Mile, val_) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist: dist_unit value) = convert_dist ((fst from_unit), 1.) (fst to_unit) in let (time: time_unit value) = convert_time ((snd from_unit), 1.) (snd to_unit) in (((fst to_unit), (snd from_unit)), val_ *. (snd dist) /. (snd time)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speed: speed_unit value) = convert_speed ((fst (fst a), snd (fst a)), snd a) b_unit in (b_unit, snd speed +. b_val) ;;
let passes_da_vinci t = let rec sum (lst: tree list) (add: float): float = match lst with | Branch (diameter, _) :: next -> sum next (diameter *. diameter +. add) | Leaf :: next -> sum next add | _ -> add in let rec check_branch (branch: tree list): bool = (match branch with | Branch (diameter, lst) :: next -> if ((diameter *. diameter) >= sum lst 0.) then check_branch lst && check_branch next else false | Leaf :: next -> check_branch next | _ -> true ) in check_branch [t] ;;
let mode (l: 'a list) : 'a = let rec aux lst ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | element :: lst -> if cur_el = element then aux lst (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux lst (element, 1) (cur_el, cur_num) else aux lst (element, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input." | element :: lst -> aux lst (element, 1) (element, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec run (lst: 'a list) (maintain: ('a * 'a) list) (last: 'a) = match lst with | [] -> mode maintain | element :: lst -> run lst ((last, element) :: maintain) element in match l with | [] -> failwith "Undefined input." | one :: [] -> failwith "Undefined input." | element :: lst -> run lst ([]) element ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) ) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) ) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /. 0.3048 /. 5280.) ) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) | Mile -> (Mile, val_) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist: dist_unit value) = convert_dist ((fst from_unit), 1.) (fst to_unit) in let (time: time_unit value) = convert_time ((snd from_unit), 1.) (snd to_unit) in (((fst to_unit), (snd from_unit)), val_ *. (snd dist) /. (snd time)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speed: speed_unit value) = convert_speed ((fst (fst a), snd (fst a)), snd a) b_unit in (b_unit, snd speed +. b_val) ;;
let passes_da_vinci t = let rec sum (lst: tree list) (add: float): float = match lst with | Branch (diameter, _) :: next -> sum next (diameter *. diameter +. add) | Leaf :: next -> sum next add | _ -> add in let rec check_branch (branch: tree list): bool = (match branch with | Branch (diameter, lst) :: next -> if ((diameter *. diameter) >= sum lst 0.) then check_branch lst && check_branch next else false | Leaf :: next -> check_branch next | _ -> true ) in check_branch [t] ;;
let mode (l: 'a list) : 'a = let rec aux lst ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | element :: lst -> if cur_el = element then aux lst (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux lst (element, 1) (cur_el, cur_num) else aux lst (element, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input." | element :: lst -> aux lst (element, 1) (element, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec run (lst: 'a list) (maintain: ('a * 'a) list) (last: 'a) = match lst with | [] -> mode maintain | element :: lst -> run lst ((last, element) :: maintain) element in match l with | [] -> failwith "Undefined input." | one :: [] -> failwith "Input size too small." | element :: lst -> run lst ([]) element ;;