text
stringlengths
0
601k
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_ *. (0.3048 *. 5280.)) | Mile -> (Mile, val_)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, val_ *. (snd (convert_dist ((fst from_unit), 1.) (fst to_unit))) /. (snd (convert_time ((snd from_unit), 1.) (snd to_unit)))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit, (snd (convert_time time (snd speed_unit))) *. speed_val) ;; let rec passes_da_vinci t = let sum_of_squares l = List.fold_left (+.) 0. (List.map (fun a -> a *. a) l) in let get_width b = match b with | Branch (width, _) -> width | Leaf -> 0. in let branch_widths = List.map get_width in match t with | Branch (width, branches) -> ( (sum_of_squares (branch_widths branches)) <= (width *. width) ) && (List.for_all passes_da_vinci branches) | Leaf -> true ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if cur_num > max_num then aux l (cur_el, cur_num) (cur_el, cur_num) else match l with | [] -> if cur_num = max_num then min cur_el max_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) ((min cur_el max_el), max_num) else aux xs (x, 1) (max_el, max_num) in let sorted_l = List.sort compare l in match sorted_l with | x :: xs -> aux xs (x, 1) (x, 1) |_ -> failwith "Undefined input" ;;
let pair_mode (l: 'a list) : 'a * 'a = let (pair_list: ('a * 'a) list) = let rec aux list = match list with | x1 :: x2 :: xs -> (x1, x2) :: aux (x2 :: xs) |_ -> [] in aux l in match l with | x1 :: x2 :: xs -> mode pair_list |_ -> failwith "Undefined Input" ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (to_unit, val_) else if (from_unit = Second && to_unit = Hour) then (Hour, val_ /. 3600.0) else if (from_unit = Hour && to_unit = Second) then (Second, val_ *. 3600.0) else failwith "Undefined input" ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (to_unit, val_) else let dist_meter = match from_unit with | Foot -> val_ *. 0.3048 | Mile -> val_ *. (0.3048 *. 5280.) | Meter -> val_ in match to_unit with | Foot -> (Foot, dist_meter /. 0.3048) | Mile -> (Mile, dist_meter /. (0.3048 *. 5280.)) | Meter -> (Meter, dist_meter) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit, val_) else let speedValue : float = snd (convert_dist ((fst from_unit), val_) (fst to_unit)) /. snd (convert_time ((snd from_unit), 1.) (snd to_unit)) in (to_unit, speedValue) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time_u = convert_time time (snd speed_unit) in (fst speed_unit, speed_val *. (snd time_u)) ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (w, ls) -> let getW tree = match tree with | Leaf -> 0. | Branch (w, _) -> w in let rec sq_sum (t_list : tree list) sum = match t_list with | [] -> sum | x :: xs -> sq_sum xs (sum +. (getW x *. getW x)) in let rec t_iter t_list bool = if not bool then false else match t_list with | [] -> bool | x :: xs -> bool && (t_iter xs (passes_da_vinci x)) in ((w *. w) >= (sq_sum ls 0.)) && (t_iter ls true) ;;
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 | x::xs -> (match x with | n when n = cur_el -> (match x with | i when i = max_el -> aux xs (x, cur_num + 1) (i, max_num + 1) | _ -> (match cur_num with | k when k = max_num -> aux xs (x, cur_num + 1) (x, cur_num + 1) | _ -> aux xs (x, cur_num + 1) (max_el, max_num))) | _ -> aux xs (x, 1) (max_el, max_num)) in match List.sort compare l with | [] -> failwith "list is too short" | x::xs -> aux (List.sort compare xs) (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec build (l: 'a list) (s: ('a*'a) list) : (('a * 'a) list) = match l with | [] -> s | x::[] -> s | x::y::xs -> build (List.append [y] xs) (List.append [(x, y)] s) in let x = build l [] in mode x ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Hour -> (Second, val_ /. 3600.) | from_unit -> (from_unit, val_)) | Hour -> (match to_unit with | Second -> (to_unit, val_ *. 3600.) | from_unit -> (from_unit, val_)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Meter -> (match to_unit with | Mile -> (to_unit, (val_ /. 0.3048) /. 5280.) | Foot -> (to_unit, val_ /. 0.3048) | from_unit -> (from_unit, val_)) | Mile -> (match to_unit with | Meter -> (to_unit, (val_ *. 5280.) *. 0.3048) | Foot -> (to_unit, val_ *. 5280.) | from_unit -> (from_unit, val_)) | Foot -> (match to_unit with | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) | from_unit -> (from_unit, val_)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (Meter, Second) -> (match to_unit with |(Meter, Hour) -> (to_unit, val_ *. 3600.) |(Foot, Second) -> (to_unit, val_ /. 0.3048) |(Foot, Hour) -> (to_unit, val_ /. 0.3048 *. 3600.) |(Mile, Second) -> (to_unit, val_ /. 0.3048 /. 5280.) |(Mile, Hour) -> (to_unit, (val_ /. 0.3048 /. 5280.) *. 3600.) | from_unit -> (from_unit, val_)) | (Meter, Hour) -> (match to_unit with |(Meter, Second) -> (to_unit, val_ /. 3600.) |(Foot, Second) -> (to_unit, val_ /. 0.3048 /. 3600.) |(Foot, Hour) -> (to_unit, val_ /. 0.3048) |(Mile, Second) -> (to_unit, (val_ /. 0.3048 /. 5280.) /. 3600.) |(Mile, Hour) -> (to_unit, val_ /. 0.3048 /. 5280.) | from_unit -> (from_unit, val_)) | (Foot, Second) -> (match to_unit with |(Foot, Hour) -> (to_unit, val_ *. 3600.) |(Meter, Second) -> (to_unit, val_ *. 0.3048) |(Meter, Hour) -> (to_unit, val_ *. 0.3048 *. 3600.) |(Mile, Second) -> (to_unit, val_ /. 5280.) |(Mile, Hour) -> (to_unit, val_ *. (3600. /. 5280.)) | from_unit -> (from_unit, val_)) | (Foot, Hour) -> (match to_unit with |(Foot, Second) -> (to_unit, val_ /. 3600.) |(Meter, Hour) -> (to_unit, val_ *. 0.3048) |(Meter, Second) -> (to_unit, val_ *. 0.3048 /. 3600.) |(Mile, Second) -> (to_unit, val_ /. 5280. /. 3600.) |(Mile, Hour) -> (to_unit, val_ /. 5280.) | from_unit -> (from_unit, val_)) | (Mile, Second) -> (match to_unit with |(Meter, Hour) -> (to_unit, (val_ *. 0.3048 *. 5280.) *. 3600.) |(Foot, Second) -> (to_unit, val_ *. 5280.) |(Foot, Hour) -> (to_unit, val_ *. 5280. *. 3600.) |(Meter, Second) -> (to_unit, (val_ *. 0.3048 *. 5280.)) |(Mile, Hour) -> (to_unit, val_ *. 3600.) | from_unit -> (from_unit, val_)) | (Mile, Hour) -> (match to_unit with |(Mile, Second) -> (to_unit, val_ /. 3600.) |(Foot, Hour) -> (to_unit, val_ *. 5280.) |(Foot, Second) -> (to_unit, val_ *. 5280. /. 3600.) |(Meter, Hour) -> (to_unit, val_ *. (0.3048 *. 5280.)) |(Meter, Second) -> (to_unit, val_ *. (0.3048 *. 5280.) /. 3600.) | from_unit -> (from_unit, val_)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. snd (convert_speed (fst a, snd a) b_unit)) ;;
let passes_da_vinci t = let rec aux b = match b with | [] -> true | z::zs -> (match z with | Leaf -> aux zs | Branch (_, []) -> aux zs | Branch (x, y) -> if x *. x >= sum_of_squares y 0. then aux zs else false) in match t with | Leaf -> true | Branch (_, []) -> true | Branch (x, y) -> if x *. x >= sum_of_squares y 0. then aux y else false ;; let tree_example2 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (0., []) ]) ;; let tree_fail = Branch (5., [ Branch (3., [Branch (1., []); Leaf; Leaf]); Leaf; Branch (4., [Branch (7., [])]) ]) let x = sum_of_squares [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ] 0. ;;
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 begin let cur_num = cur_num + 1 in if cur_num > max_num then let max_num = max_num + 1 in aux xs (x, cur_num) (x, max_num) else aux xs (x, cur_num) (max_el, max_num) end else aux xs (x, 1) (max_el, max_num) in match l with | [] -> failwith "Undefined Input" | _ -> let l = List.sort compare l in aux l (List.nth l 0, 0) (List.nth l 0, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 1 then failwith "Undefined Input" else let remove_first x = match x with | [] -> [] | _::xs -> xs in let l1 = remove_first l in let l2 = List.rev (remove_first (List.rev l)) in let l3 = List.combine l2 l1 in mode l3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (to_unit, val_) else match from_unit with | Second -> (Hour, val_ /. 3600.) | Hour -> (Second, val_ *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (to_unit, val_) else match from_unit with | Foot -> if to_unit = Mile then (Mile, val_ /. 5280.) else (Meter, val_ *. 0.3048) | Meter -> if to_unit = Mile then (Mile, val_ /. 1609.344) else (Foot, val_ *. 3.280839895) | Mile -> if to_unit = Meter then (Meter, val_ *. 1609.344) else (Foot, val_ *. 5280.) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit, val_) else let new_time = convert_time (snd(to_unit), val_) (snd(from_unit)) in let new_distance = convert_dist (fst(from_unit), snd(new_time)) (fst(to_unit)) in (to_unit, snd(new_distance));;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let new_a = convert_speed (fst(a), snd(a)) (b_unit) in let new_speed = snd(new_a) +. b_val in (b_unit, new_speed) ;;
let passes_da_vinci t = let rec sum_ l = match l with | [] -> 0. | Leaf :: tl -> 0. +. sum_ tl | Branch (width, subtree) :: tl -> width ** 2. +. sum_ tl in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && (sum_ subtree <= width**2.0)) then check tl 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 |hd :: tl -> if (hd = cur_el) && (cur_num >= max_num) then aux tl (hd, cur_num + 1) (hd, cur_num + 1) else if cur_el = hd then aux tl (hd, cur_num + 1) (max_el, max_num) else aux tl (hd, 1) (max_el, max_num) in if not ((List.length l) = 0) then aux (List.sort compare l) (List.hd (List.sort compare l), 0) (List.hd (List.sort compare l), 0) else failwith "invalid input" ;; let rec create_subl (l : 'a list) (sub : 'a list) = match l with | x :: [] -> sub | x :: xs -> create_subl xs (x :: sub) ;;
let pair_mode (l: 'a list) : 'a * 'a = let ls1 = create_subl (List.rev l) [] in let ls2 = List.rev (create_subl l []) in if ((List.length l) < 2) then failwith "invalid input" else mode (List.combine ls2 ls1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with |(Second, Second) -> (Second, val_) |(Second, Hour) -> (Hour, val_ /. 3600.) |(Hour, Second) -> (Second, val_ *. 3600.) |(Hour, Hour) -> (Hour, val_) ;;
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.) |(Meter, Meter) -> (Meter, val_) |(Meter, Foot) -> (Foot, val_ /. 0.3048) |(Meter, Mile) -> (Mile, val_ /. 0.3048 /. 5280.) |(Mile, Mile) -> (Mile, val_) |(Mile, Foot) -> (Foot, val_ *. 5280.) |(Mile, Meter) -> (Meter, val_ *. 5280. *. 0.3048) ;;
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 add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let speed1 = convert_speed a b_unit in (b_unit, (snd speed1) +. b_val) ;;
let passes_da_vinci t = check [t] ;; let tree1 = Branch (5., [ Branch (4., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]) ;;
let mode (l: 'a list) :'a = let l= List.sort compare l in match l with |[] -> failwith("invalid input") |a :: l2 -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |a :: l2 -> if a = cur_el then aux l2 (a,cur_num+1) (max_el,max_num) else if max_num < cur_num then aux l2 (a, 1) (cur_el,cur_num) else aux l2 (a,1) (max_el,max_num); |[] -> if max_num < cur_num then cur_el else max_el in aux l2 (a,1) (a,1); ;; let remove (l: 'a list) : 'a list = match l with | [] -> failwith("empty") | x :: l2 -> l2 ;; let rec new_list (l1: 'a list) (l2: 'a list)= match l1,l2 with |[],[] -> [] |x :: l3, y :: l4 -> (x,y) :: (new_list (l3) (l4)) |(_::_, []) -> [] |([],_::_) -> [] ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with |[] -> failwith("invalid input") |_-> let l1 = List.rev (remove (List.rev l)) in let l2 = remove l in mode (new_list l1 l2) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second-> begin match to_unit with |Second -> Second,(val_) |Hour -> Hour, (val_/.3600.); end |Hour-> begin match to_unit with |Second -> Second,(val_*.3600.) |Hour -> Hour, (val_) end ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot-> begin match to_unit with |Foot -> Foot,(val_) |Meter -> Meter, (val_*.0.3048) |Mile -> Mile,(val_/.5280.) end |Mile-> begin match to_unit with |Mile -> Mile,(val_) |Foot -> Foot, (val_*.5280.) |Meter -> Meter, ((val_*.5280.)*.0.3048) end |Meter-> begin match to_unit with |Meter-> Meter,(val_) |Foot->Foot,(val_/.0.3048) |Mile->Mile,((val_/.0.3048)/.5280.) end ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist1,time1) = from_unit in let (dist2,time2) = to_unit in (dist2,time2), snd(convert_time (time2,snd(convert_dist (dist1,val_) dist2)) time1) ;;
let add_speed (a_unit, a_val : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (dist1,time1) = a_unit in let (dist2,time2) = b_unit in (dist2,time2), (b_val+.snd(convert_time (time2,snd(convert_dist (dist1,a_val) dist2)) time1)) ;;
let passes_da_vinci t = let rec sum_ l (acc: float) = match l with | []-> acc; | Leaf :: tl -> sum_ tl acc; | Branch (width, subtree) :: tl -> sum_ tl (acc+.width**2.); 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**2.)) then false else check tl in check [t] ;;
let mode (l: 'a list) : 'a = match l with | [] -> failwith "list has no elements" | _ -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = let ol = List.sort compare l in match ol with | [] -> max_el | x :: xs -> if x = cur_el then let cur_num = cur_num + 1 in if cur_num > max_num then aux xs (cur_el, cur_num) (cur_el, cur_num) else if (cur_num = max_num) then if cur_el < max_el then aux xs (cur_el, cur_num) (cur_el, cur_num) else aux xs (cur_el, cur_num) (max_el, max_num) else aux xs (cur_el, cur_num) (max_el, max_num) else if x < max_el then aux xs (x,1) (x,1) else aux xs (x,1) (max_el, max_num) in aux l (List.hd l,0) (List.hd l,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | x::xs::xss -> let rec make_touple_list (l: 'a list) (n1: 'a) (n2: 'a) (acc : ('a *'a) list) = match l with | [] -> ((n1, n2)::acc) | x :: xs -> make_touple_list xs n2 x ((n1, n2)::acc) in mode (make_touple_list xss x xs []) | _ -> failwith "list too small" ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> ( match to_unit with | Hour -> (Hour, val_) | Second -> (Second, (val_ *. 3600.)) ) | Second -> ( match to_unit with | Hour -> (Hour, (val_ /. 3600.)) | Second -> (Second, 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 (from_dist, from_time) = from_unit in ( let (to_dist, to_time) = to_unit in ( if from_dist != to_dist then let (_, value) = convert_dist (from_dist, val_) to_dist in ( if from_time != to_time then let (_, final_val) = convert_time (to_time, value) from_time in (to_unit, final_val) else (to_unit, value) ) else if from_time != to_time then let (_, value) = convert_time (to_time, val_) from_time in (to_unit, value) else (to_unit, val_) ) ) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, correct_speed) = convert_speed a b_unit in(b_unit, b_val +. correct_speed) ;;
let passes_da_vinci t = match t with | Leaf -> true | Branch (width, subtree) -> ( let rec check_branch sum subtree acc = match subtree with | [] -> sum >= acc | Leaf :: lst -> check_branch sum lst acc | Branch (width, subtree) :: lst -> if check_branch (square width) subtree 0. then check_branch sum lst (acc +. (square width)) else false in check_branch (square width) subtree 0. ) ;;
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 |[] -> failwith "Empty list!" |x :: xs -> match xs with | [] -> if cur_num > max_num then cur_el else max_el | hd::_ -> let cur_el = x in let next_el = hd in if cur_el = next_el then aux xs (next_el, (cur_num + 1)) (max_el, max_num) else if cur_num > max_num then aux xs (next_el, 1) (cur_el, cur_num) else if cur_num = max_num then (if cur_el > max_el then aux xs (next_el, 1) (max_el, max_num) else aux xs (next_el, 1) (cur_el, cur_num)) else aux xs (next_el, 1) (max_el, max_num) in let s = List.sort compare (l) in aux s (List.hd(s), 1) (List.hd(s), 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_mode_help l (p : ('a * 'a) list): ('a * 'a) list = match l with |[] -> failwith "Empty list!" |x :: xs -> match xs with | [] -> p | hd::_ -> pair_mode_help xs ([(x, hd)] @ p) in let c = pair_mode_help l [] in mode (c) ;;
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 | Hour -> (Hour, val_) | Second -> (Second, 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 -> (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 = match from_unit with | (Foot, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), val_) | (Foot, Hour) -> ((Foot, Hour), snd (convert_time (Hour, val_) Second)) | (Meter, Second) -> ((Meter, Second), snd (convert_dist (Foot, val_) Meter)) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist (Foot, snd (convert_time (Hour, val_) Second)) Meter)) | (Mile, Second) -> ((Mile, Second), snd (convert_dist (Foot, val_) Mile)) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist (Foot, snd (convert_time (Hour, val_) Second)) Mile))) | (Foot, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_time (Second, val_) Hour)) | (Foot, Hour) -> ((Foot, Hour) , val_) | (Meter, Second) -> ((Meter, Second), snd (convert_dist (Foot, snd (convert_time (Second, val_) Hour)) Meter)) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist (Foot, val_) Meter)) | (Mile, Second) -> ((Mile, Second), snd (convert_dist (Foot, snd (convert_time (Second, val_) Hour)) Mile)) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist (Foot, val_) Mile))) | (Meter, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist (Meter, val_) Foot)) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist (Meter, snd (convert_time (Hour, val_) Second)) Foot)) | (Meter, Second) -> ((Meter, Second), val_) | (Meter, Hour) -> ((Meter, Hour), snd (convert_time (Hour, val_) Second)) | (Mile, Second) -> ((Mile, Second), snd (convert_dist (Meter, val_) Mile)) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist (Meter, snd (convert_time (Hour, val_) Second)) Mile))) | (Meter, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist (Meter, snd (convert_time (Second, val_) Hour)) Foot)) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist (Meter, val_) Foot)) | (Meter, Second) -> ((Meter, Second), snd(convert_time (Second, val_) Hour)) | (Meter, Hour) -> ((Meter, Hour) , val_) | (Mile, Second) -> ((Mile, Second), snd (convert_dist (Meter, snd (convert_time (Second, val_) Hour)) Mile)) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist (Meter, val_) Mile))) | (Mile, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist (Mile, val_) Foot)) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist (Mile, snd (convert_time (Hour, val_) Second)) Foot)) | (Meter, Second) -> ((Meter, Second), snd (convert_dist (Mile, val_) Meter)) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist (Mile, snd (convert_time (Hour, val_) Second)) Meter)) | (Mile, Second) -> ((Mile, Second), val_) | (Mile, Hour) -> ((Mile, Hour), snd (convert_time (Hour, val_) Second))) | (Mile, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist (Mile, snd (convert_time (Second, val_) Hour)) Foot)) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist (Mile, val_) Foot)) | (Meter, Second) -> ((Meter, Second), snd (convert_dist (Mile, snd (convert_time (Second, val_) Hour)) Meter)) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist (Mile, val_) Meter)) | (Mile, Second) -> ((Mile, Second), snd (convert_time (Second, val_) Hour)) | (Mile, Hour) -> ((Mile, Hour), val_)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let new_speed = convert_speed a b_unit in (b_unit, snd(new_speed) +. b_val) ;;
let passes_da_vinci t = let rec compute_squares (l: tree list) : float = match l with | [] -> 0. | Leaf :: tl -> 0. +. compute_squares tl | Branch (width, subtree) :: tl -> width ** 2. +. compute_squares tl in let rec check_da_vinci (l:tree list) : bool = match l with | [] -> true | Leaf:: xs -> check_da_vinci xs | Branch (width, subtree) :: xs -> if(not (check_da_vinci subtree) || compute_squares subtree > (width ** 2.)) then false else check_da_vinci xs in if( t = Leaf) then true else check_da_vinci [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 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 match List.sort compare l with | [] -> failwith "empty" | x::xs -> aux xs (x,1) (x,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l2 = List.rev l in let l3 = List.tl l2 in let l4 = List.rev l3 in let l5 = List.combine l4 l1 in match l with | [] -> failwith "empty" |_ -> mode l5 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then to_unit,val_ else match to_unit with | Second -> Second,val_ *. 3600. | Hour -> Hour,val_ /. 3600. ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then to_unit,val_ else match from_unit with | Foot -> (match to_unit with | Meter -> Meter,val_ *.0.3048 | Mile -> Mile,val_ /.5280. ) | Meter -> (match to_unit with | Foot -> Meter,val_ /.0.3048 | Mile -> Mile,val_ /.1609.344) | Mile -> (match to_unit with | Foot -> Foot,val_*.5280. | Meter -> Meter,val_ *.1609.344);;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist1 = fst(from_unit) in let time1 = snd(from_unit) in let dist2 = fst(to_unit) in let time2 = snd(to_unit) in let val_dist_same = snd(convert_time(time2,val_)time1) in let val_time_same = snd(convert_dist(dist1,val_)dist2) in let val2 = snd(convert_time(time2,val_time_same)time1) in if from_unit = to_unit then to_unit,val_ else if dist1 = dist2 then to_unit,val_dist_same else if time1 = time2 then to_unit,val_time_same else to_unit,val2 ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let unit1 = fst(time) in let val1 = snd(time) in let dist1 = fst(speed_unit) in let time1 = snd(speed_unit) in let travel = snd(convert_time(unit1,val1) time1) in dist1, speed_val *. travel ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch(_,_) -> false ;;
let null_value (x : 'a) = x;;
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 if (cur_num+1) > max_num then aux tl (hd, cur_num+1) (hd, cur_num+1) else aux tl (hd, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (hd, 1) else aux tl (hd, 1) (max_el, max_num) in match (List.sort compare l) with | [] -> failwith "Empty list." | hd :: tl -> aux tl (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (match (List.rev l) with | [] -> failwith "Empty list." | hd :: tl -> List.rev tl) (match l with | [] -> failwith "Empty list." | hd :: tl -> tl)) ;;
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 = match from_unit with | (Foot, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Second ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Second ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Second ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Second ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Second ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Second ))) Mile )))) | (Foot, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Hour ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Hour ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Hour ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Hour ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Hour ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Hour ))) Mile )))) | (Meter, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Second ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Second ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Second ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Second ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Second ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Second ))) Mile )))) | (Meter, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Hour ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Hour ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Hour ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Hour ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Hour ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Hour ))) Mile )))) | (Mile, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Second ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Second ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Second ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Second ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Second ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Second ))) Mile )))) | (Mile, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Hour ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Hour ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Hour ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Hour ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Hour ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Hour ))) Mile )))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match speed_unit with | (Foot, Second) -> (fst(speed_unit), snd(convert_time time Second) *. speed_val) | (Foot, Hour) -> (fst(speed_unit), snd(convert_time time Hour) *. speed_val) | (Meter, Second) -> (fst(speed_unit), snd(convert_time time Second) *. speed_val) | (Meter, Hour) -> (fst(speed_unit), snd(convert_time time Hour) *. speed_val) | (Mile, Second) -> (fst(speed_unit), snd(convert_time time Second) *. speed_val) | (Mile, Hour) -> (fst(speed_unit), snd(convert_time time Hour) *. speed_val) ;; let rec passes_da_vinci t = notimplemented () ;;
let null_value (x : 'a) = x;;
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 if (cur_num+1) > max_num then aux tl (hd, cur_num+1) (hd, cur_num+1) else aux tl (hd, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (hd, 1) else aux tl (hd, 1) (max_el, max_num) in match (List.sort compare l) with | [] -> failwith "Empty list." | hd :: tl -> aux tl (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (match (List.rev l) with | [] -> failwith "Empty list." | hd :: tl -> List.rev tl) (match l with | [] -> failwith "Empty list." | hd :: tl -> tl)) ;;
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 = match from_unit with | (Foot, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Second ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Second ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Second ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Second ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Second ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Second ))) Mile )))) | (Foot, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Hour ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Hour ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Hour ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Hour ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Foot, (snd(convert_time (Second, val_) Hour ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Foot, (snd(convert_time (Hour, val_) Hour ))) Mile )))) | (Meter, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Second ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Second ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Second ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Second ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Second ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Second ))) Mile )))) | (Meter, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Hour ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Hour ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Hour ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Hour ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Meter, (snd(convert_time (Second, val_) Hour ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Meter, (snd(convert_time (Hour, val_) Hour ))) Mile )))) | (Mile, Second) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Second ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Second ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Second ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Second ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Second ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Second ))) Mile )))) | (Mile, Hour) -> (match to_unit with | (Foot, Second) -> ((Foot, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Hour ))) Foot ))) | (Foot, Hour) -> ((Foot, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Hour ))) Foot ))) | (Meter, Second) -> ((Meter, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Hour ))) Meter ))) | (Meter, Hour) -> ((Meter, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Hour ))) Meter ))) | (Mile, Second) -> ((Mile, Second), (snd(convert_dist (Mile, (snd(convert_time (Second, val_) Hour ))) Mile ))) | (Mile, Hour) -> ((Mile, Hour), (snd(convert_dist (Mile, (snd(convert_time (Hour, val_) Hour ))) Mile )))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match speed_unit with | (Foot, Second) -> (fst(speed_unit), snd(convert_time time Second) *. speed_val) | (Foot, Hour) -> (fst(speed_unit), snd(convert_time time Hour) *. speed_val) | (Meter, Second) -> (fst(speed_unit), snd(convert_time time Second) *. speed_val) | (Meter, Hour) -> (fst(speed_unit), snd(convert_time time Hour) *. speed_val) | (Mile, Second) -> (fst(speed_unit), snd(convert_time time Second) *. speed_val) | (Mile, Hour) -> (fst(speed_unit), snd(convert_time time Hour) *. speed_val) ;; let rec passes_da_vinci t = notimplemented () ;;
let curIsMax ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) : bool = if cur_num = max_num then cur_el < max_el else cur_num > max_num;;
let mode (l: 'a list) : 'a = match List.sort compare(l) with | [] -> failwith "Empty list" | x::xs -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> (if curIsMax (cur_el, cur_num) (max_el, 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 curIsMax (cur_el, cur_num) (max_el, max_num) then aux xs (x,1) (cur_el, cur_num) else aux xs (x,1) (max_el, max_num) )) in aux xs (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> failwith "Not enough elements in list" | _::_ -> mode (List.combine (List.rev(List.tl(List.rev(l)))) (List.tl(l))) ;;
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 (_, updated_dist) = convert_dist (fst from_unit, val_) (fst to_unit) in let (_, updated_dist_time) = convert_time (snd to_unit, updated_dist) (snd from_unit) in (to_unit, updated_dist_time) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (_, updated_time_value) = convert_time (fst time, snd time) (snd speed_unit) in (fst speed_unit, speed_val *. updated_time_value) ;; let square (x : float) : float = x *. x;; let rec computeSquares (l : tree list) (s: float) : float = match l with | [] -> s | x::xs -> match x with | Leaf -> computeSquares xs s | Branch (val_ , _) -> computeSquares xs (s +. square val_) ;; let rec passes_da_vinci t = let rec childrenTraversal (l : tree list ) : bool = match l with | [] -> true | x::xs -> (passes_da_vinci x) && (childrenTraversal xs) in match t with | Leaf -> true | Branch (val_ , children) -> (square val_ >= (computeSquares children 0.)) && (childrenTraversal children) ;;
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 cur_num > max_num then aux tl (hd, 0) (cur_el, cur_num) else aux tl (hd, 0) (max_el, max_num) ;;
let mode (l: 'a list) : 'a = match (List.sort compare l) with |[] -> failwith "Invalid input." |hd :: tl -> aux tl (hd, 0) (hd, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "Invalid input." else let l2 = (List.rev l) in mode (List.combine (List.rev (List.tl l2)) (List.tl l) ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, (val_ /. 3600.)) | (Second, Second) -> (Second, val_) | (Hour, Second) -> (Second, (val_ *. 3600.)) | (Hour, Hour) -> (Hour, val_) ;;
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 , (val_ *. 0.3048)) | (Foot , Mile) -> (Mile, (val_ /. 5280.)) | (Meter , Foot ) -> (Foot , (val_ /. 0.3048)) | (Meter , Mile) -> (Mile, ((val_ /. 0.3048) /. 5280.)) | (Mile , Foot ) -> (Foot , (val_ *. 5280.)) | (Mile , Meter ) -> (Meter , ((val_ *. 5280.) *. 0.3048)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, (snd (convert_time ((snd(to_unit)), snd (convert_dist (fst(from_unit), val_) (fst(to_unit)))) (snd(from_unit))))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. (snd(convert_speed (a) (b_unit)))) ;;
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 -> true | Branch (width, subtree) :: tl -> if ((check subtree) && ((sum_ subtree 0.) <= (width ** 2.))) then check tl else false in check [t] ;;
let mode (l: 'a list) : 'a = if l = [] then failwith "Exception: Invalid input." else let list = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | a::x -> ( if a = cur_el then ( if cur_num+1 <= max_num then aux x (cur_el, cur_num+1) (max_el, max_num) else aux x (cur_el, cur_num+1) (cur_el, cur_num+1) ) else aux x (a,1) (max_el, max_num) ) in aux (list) ((List.hd list),0) (List.hd list,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l <= 1) then failwith "Invalid list size" else let rec help (l: 'a list) (l2: ('a * 'a) list) (prev: 'a) : ('a*'a) list= match l with | [] -> l2 | a::b::rest -> help ((b::rest)) (((a,b)::l2)) a | a::[] -> l2 in let list = help l [] (List.hd l) in mode list ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (from_unit, val_) else match from_unit with | Second -> (to_unit, val_ /. 3600.) | Hour -> (to_unit, val_ *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (from_unit, val_) else match from_unit with | Foot -> ( match to_unit with | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) ) | Meter -> ( match to_unit with | Foot -> (to_unit, val_ /. 0.3048) | Mile -> (to_unit, val_ /. 0.3048 /. 5280.) ) | Mile -> ( match to_unit with | Meter -> (to_unit, val_ *. 0.3048 *. 5280.) | Foot -> (to_unit, val_ *. 5280.) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_dist_unit, from_time_unit) = from_unit in let (to_dist_unit, to_time_unit) = to_unit in let (p, final_time) = convert_time (from_time_unit, 1. ) to_time_unit in let (q, final_dist) = convert_dist (from_dist_unit, 1.) to_dist_unit in (to_unit, val_ /. final_time *. final_dist) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (d_unit, t_unit) = speed_unit in let (tmu, time_amt) = convert_time time t_unit in (d_unit, time_amt *. speed_val ) ;; let rec passes_da_vinci t = let rec parse_branch (l: tree list) (counter:float): float = match l with | (Leaf)::rest -> parse_branch rest counter | (Branch (curr_width, in_list))::rest -> ( parse_branch rest counter +. (curr_width**2.) ) | (Leaf)::[] -> ( counter ) | (Branch (curr_width, in_list))::[] -> ( counter +. (curr_width**2.) ) | [] -> counter in let helper t : bool = match t with | Leaf -> true | Branch (width, b_list) -> ( let child_width = parse_branch b_list 0. in (child_width) <= (width**2.) ) in if helper t then match t with | Leaf -> true | Branch (width, b_list) -> ( match b_list with | [] -> true | Leaf::rest -> true | Branch(curr_width, in_list)::rest -> if ( helper (Branch(curr_width, in_list)) ) then passes_da_vinci (Branch(curr_width, in_list)) else false | Leaf::[] -> true | Branch(curr_width, in_list)::[] -> (helper (Branch(curr_width, in_list))) ) 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) | hd :: tl -> (if hd = cur_el then aux tl (cur_el, cur_num + 1) (max_el, max_num) else (if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num))) in match l with | [] -> failwith "Invalid input." | hd::tl -> aux (List.sort compare l) (hd, 0) (hd, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid input." | hd::tl -> mode (List.combine (List.rev(List.tl(List.rev l))) (List.tl l)) ;;
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 = Hour then (to_unit, val_) else (to_unit, val_*.3600.)) ;;
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 = Meter then (to_unit, val_) else if to_unit = Foot then (to_unit, val_/.0.3048) else (to_unit, val_/.0.3048/.5280.)) | Mile -> (if to_unit = Mile then (to_unit, val_) else if to_unit = Foot then (to_unit, val_*.5280.) else (to_unit, val_*.5280.*.0.3048)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit, val_) else (to_unit, snd(convert_dist (fst(from_unit), snd(convert_time(snd(to_unit), val_) (snd(from_unit)))) (fst(to_unit)))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. snd(convert_speed(fst(a), snd(a)) b_unit)) ;;
let passes_da_vinci (t: tree): bool = match t with | Leaf -> true | Branch(width, subtree) -> (width *. width >= (add_square_width subtree) && check subtree) ;;