text
stringlengths
0
601k
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if (to_unit = from_unit) then (from_unit,val_) else if to_unit = Hour then (Hour, val_ /. 3600.) else (Second, val_ *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if (to_unit = from_unit) then (from_unit,val_) else match to_unit with | Mile -> if from_unit = Foot then (Mile, val_ /. 5280.) else (Mile, (val_ /. 0.3048) /. 5280.) | Foot -> if from_unit = Meter then (Foot,(val_ /. 0.3048)) else (Foot,(val_*.5280.)) | Meter -> if from_unit = Foot then (Meter,(val_ *. 0.3048)) else (Meter,((val_ *. 5280.)*. 0.3048)) ;; let get_dist ((d,t): dist_unit * time_unit): dist_unit = d;; let get_time ((d,t): dist_unit * time_unit): time_unit = t;; let get_val ((form,val_): 'a * float): float = val_;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let t1 = get_time from_unit in let d1 = get_dist from_unit in let t2 = get_time to_unit in let d2 = get_dist to_unit in (to_unit,(get_val(convert_dist (d1,val_) d2)) /.(get_val(convert_time (t1,1.) t2)));;
let dist_traveled (time : time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let tim = get_time speed_unit in let dis = get_dist speed_unit in (dis, get_val(convert_time time tim) *.speed_val) let rec passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ subtree acc +. width ** 2. in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (((check subtree) && (sum_ subtree 0. <= (width ** 2. ))) == false) 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 | h::t -> if h = cur_el 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 match List.sort compare l with | [] -> failwith "Undefined input." | h::t -> aux t (h, 1) (h, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match (l, List.rev l) with | ([], _) | (_, []) -> failwith "Undefined input." | (h::[], _) -> failwith "Undefined input." | (h1::t1, h2::t2) -> mode (List.combine (List.rev t2) t1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Second) | (Hour, Hour) -> (to_unit, val_) | (Second, Hour) -> (to_unit, val_ /. 3600.) | (Hour, Second) -> (to_unit, val_ *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Foot) | (Meter, Meter) | (Mile, Mile) -> (to_unit, val_) | (Foot, Meter) -> (to_unit, val_ *. 0.3048) | (Foot, Mile) -> (to_unit, val_ /. 5280.) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | (Meter, Mile) -> (to_unit, val_ /. 1609.344) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Mile, Meter) -> (to_unit, val_ *. 1609.344) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match (snd from_unit, snd to_unit) with | (Second, Second) | (Hour, Hour) -> (to_unit, snd (convert_dist (fst from_unit, val_) (fst to_unit))) | (Second, Hour) -> (to_unit, snd (convert_dist (fst from_unit, val_) (fst to_unit)) *. 3600.) | (Hour, Second) -> (to_unit, snd (convert_dist (fst from_unit, val_) (fst to_unit)) /. 3600.) ;;
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 summation l acc = match l with | [] -> acc | Leaf :: tl -> summation tl acc | Branch (width, subtree) :: tl -> summation tl acc +. (width**2.) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && (summation subtree 0. <= width**2.)) then check tl else false in check [t] ;;
let mode (l: 'a list) : 'a = if l = [] then failwith "input is invalid, cannot be empty list"; let y = List.sort compare (l) in let x = List.hd (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 max_el else cur_el | ob_el :: l -> if cur_el = ob_el then aux l (cur_el , cur_num+1) (max_el ,max_num) else if max_num < cur_num then aux l (ob_el, 1) (cur_el, cur_num) else aux l (ob_el, 1) (max_el, max_num) in aux y (x , 0) (x, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if ( List.length(l) < 2 ) then failwith "input is invalid (list length is less than 2)"; let l1 = List.tl (l) in let l2 = List.rev(List.tl (List.rev(l))) in let y = List.combine (l2) (l1) in mode(y) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> ( if to_unit = Second then (Second , (val_) ) else (Hour , (val_ /. 3600.) )) | Hour -> ( if to_unit = Hour then (Hour , (val_) ) else (Second , (3600. *. 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 (Foot , (val_) ) else if to_unit = Meter then (Meter , (val_ *. 0.3048) ) else (Mile, (val_ /. 5280.) ) | Meter -> if to_unit = Meter then (Meter , (val_) ) else if to_unit = Mile then (Mile , val_ /. 5280. /. 0.3048) else (Foot, (val_ /. 0.3048) ) | Mile -> ( if to_unit = Mile then (Mile , (val_) ) else if to_unit = Foot then (Foot , (5280. *. val_) ) else (Meter, (val_ *. 5280. *. 0.3048) ) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let time = snd(from_unit) in let dist = fst(from_unit) in let new_time = snd(to_unit) in let new_dist = fst(to_unit) in let converted_time = convert_time (time, 1.) new_time in let converted_dist = convert_dist (dist, 1.) new_dist in let new_time_unit = fst(converted_time) in let new_dist_unit = fst(converted_dist) in let new_time_float = snd(converted_time) in let new_dist_float = snd(converted_dist) in let new_speed_float = val_ *. new_dist_float /. new_time_float in (( new_dist_unit, new_time_unit), new_speed_float) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let speed_time = snd(speed_unit) in let speed_dist = fst(speed_unit) in let new_time = convert_time time speed_time in let new_time_value = snd(new_time) in let calculated_dist = (speed_dist, new_time_value *. speed_val) in calculated_dist ;; let tree_example2 = (Branch (5., [ Branch (3., [Leaf; Leaf; Leaf; Leaf]); Leaf; Branch (4., [Leaf]); ])) let tree_example3 = (Branch (5., [ Branch (3., [Leaf; Leaf; Leaf; Leaf]); Leaf; Branch (4., [Leaf]); Branch (4., [Leaf]); Branch (4., [Leaf]); Branch (4., [Leaf]); ])) let tree_example4 = (Leaf);; let tree_example5 = (Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Branch (1., [Leaf]); Branch (1., [Leaf]); Branch (1., [Leaf]); Branch (1., [Leaf]); ])) let tree_example6 = (Branch (1., [ ])) 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 | x :: xs -> (match x with | z when z = max_el -> aux xs (z, max_num+1) (z, max_num+1) | y when y = cur_el -> (if cur_num+1 > max_num 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)) | _ -> aux xs (x, 1) (max_el, max_num)) in if l == [] then failwith "This function must take a non empty list" else let l1 = List.sort compare l in let a = List.hd l1 in aux l1 (a, 0) (a, 0);;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List must have at least two elements" else let l1 = List.tl l in let l2 = List.rev (List.tl (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 = match from_unit, to_unit with | Second, Hour -> (Hour ,val_ /. 3600.) | Hour, Second -> (Second, val_ *. 3600.) | _, _ -> (to_unit, 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_ /. 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) | _, _ -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit, to_unit with | (x, Second), (y, Second) -> ((y, Second), snd(convert_dist (x, val_) y)) | (x, Hour), (y, Hour) -> ((y, Hour), snd(convert_dist (x, val_) y)) | (x, Hour), (y, Second) -> ((y, Second), snd(convert_dist (x, val_) y) /. 3600.) | (x, Second), (y, Hour) -> ((y, Hour), snd(convert_dist (x, val_) y) *. 3600.) ;;
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 passes_da_vinci_list (l : tree list) = match l with | [] -> true | Leaf :: tl -> passes_da_vinci_list tl | Branch (width, subtree) :: tl -> (if not ((width *. width >= square_of_st subtree) && (passes_da_vinci_list subtree)) then false else passes_da_vinci_list tl) in passes_da_vinci_list [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 -> (match x with | z when z = max_el -> aux xs (z, max_num+1) (z, max_num+1) | y when y = cur_el -> (if cur_num+1 > max_num 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)) | _ -> aux xs (x, 1) (max_el, max_num)) in if l == [] then failwith "This function must take a non empty list" else let l1 = List.sort compare l in let a = List.hd l1 in aux l1 (a, 0) (a, 0);;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List must have at least two elements" else let l1 = List.tl l in let l2 = List.rev (List.tl (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 = match from_unit, to_unit with | Second, Hour -> (Hour ,val_ /. 3600.) | Hour, Second -> (Second, val_ *. 3600.) | _, _ -> (to_unit, 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_ /. 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) | _, _ -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit, to_unit with | (x, Second), (y, Second) -> ((y, Second), snd(convert_dist (x, val_) y)) | (x, Hour), (y, Hour) -> ((y, Hour), snd(convert_dist (x, val_) y)) | (x, Hour), (y, Second) -> ((y, Second), snd(convert_dist (x, val_) y) /. 3600.) | (x, Second), (y, Hour) -> ((y, Hour), snd(convert_dist (x, val_) y) *. 3600.) ;;
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 passes_da_vinci_list (l : tree list) = match l with | [] -> true | Leaf :: tl -> passes_da_vinci_list tl | Branch (width, subtree) :: tl -> (if not ((width *. width >= square_of_st subtree) && (passes_da_vinci_list subtree)) then false else passes_da_vinci_list tl) in passes_da_vinci_list [t] ;;
let num el l = List.length (List.filter (fun x -> (x == List.nth l el)) l) ;;
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 max_el else cur_el | hd :: tl when (hd = cur_el) -> aux (tl) (hd, (cur_num + 1)) (max_el, max_num) | hd :: tl -> (if cur_num <= max_num then (aux (tl) (hd, 1) (max_el, max_num)) else (aux (tl) (hd, 1) (cur_el, cur_num))) in let sort l = List.sort compare l in if (l == []) then failwith "Invalid input" else aux (sort l) (List.hd (sort l), 0) (List.hd (sort l), 0);;
let pair_mode (l: 'a list) : 'a * 'a = let rec tuples l new_l = if (List.length l < 2) then (new_l) else (tuples (List.tl l) (List.append new_l [(List.hd l, List.hd (List.tl l))])) in if (List.length l < 2) then failwith "Invalid input" else mode (tuples 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.) | (Hour, Second) -> (Second, val_ *. 3600.) | (_, _) -> (to_unit, 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) | (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) | (_, _) -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (a, b) = from_unit in let (c, d) = to_unit in let (dist_u, dist_val) = convert_dist (a, val_) c in let (time_u, time_val) = convert_time (d, dist_val) b in ((dist_u, time_u), time_val) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((_, _), value) = convert_speed a b_unit in ((b_unit), b_val +. value) ;;
let 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 ** 2.)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree && (sum_ subtree 0. <= width ** 2.)) == false) 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 cur_el = x then aux xs (x, 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 "Error: inputted list is empty." | x::xs -> aux xs (x, 1) (x, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec func l = match l with | [] -> [] | x::[] -> [] | x::xs -> (x, List.hd xs)::func xs in match l with | [] -> failwith "Invalid bigram list" | x::[] -> failwith "Invalid bigram list" | _ -> let a = func l in mode a ;;
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_) | _ -> failwith "Invalid unit conversion") | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) | _ -> failwith "Invalid unit conversion") ;;
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.) | _ -> failwith "Invalid unit conversion") | Meter -> (match to_unit with | Meter -> (Meter, val_) | Foot -> (Foot, val_ /. 0.3048) | Mile -> (Mile, val_ /. 0.3048 /. 5280.) | _ -> failwith "Invalid unit conversion") | Mile -> (match to_unit with | Mile -> (Mile, val_) | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 0.3048 *. 5280.) | _ -> failwith "Invalid unit conversion") ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let d_from_unit = fst from_unit in let t_from_unit = snd from_unit in let d_to_unit = fst to_unit in let t_to_unit = snd to_unit in let dist = convert_dist (d_from_unit, val_) d_to_unit in let time = convert_time (t_from_unit, 1.) t_to_unit in ((d_to_unit, t_to_unit), (snd dist) /. (snd time)) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let converted_time = convert_time time (snd speed_unit) in let result = speed_val *. (snd converted_time) in (fst speed_unit, result) ;; let get_direct_children (tr: tree) : tree list = match tr with | Leaf -> [] | Branch(x,y) -> y | _ -> [] ;; let rec get_sum_squares (l: tree list) (total: float) : float = match l with | [] -> total | Branch(x,y)::xs -> get_sum_squares (xs) (total +. (x *. x)) | Leaf::xs -> get_sum_squares (xs) (total) ;; let rec recurse_tree (queue: tree list) = match queue with | [] -> true | _ -> let head = List.hd queue in let queue = List.tl queue in let queue = List.append queue (get_direct_children head) in match head with | Branch(x,y) -> let sumsquares = get_sum_squares y 0. in if sumsquares > x then false else recurse_tree queue | Leaf -> recurse_tree queue ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch(x,y) -> let queue = [t] in recurse_tree queue ;;
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 (x, 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 "This list is empty" | x::xs -> aux xs (x, 1) (x,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Invalid input" else let l1 = List.tl l in let l2 = List.rev (List.tl (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 = match ((from_unit, val_), to_unit) with | ((Second, val_), Second) -> (Second, val_) | ((Second, val_), Hour) -> (Hour, (val_/.3600.0)) | ((Hour, val_), Hour) -> (Hour, val_) | ((Hour, val_), Second) -> (Second, (val_*.3600.0)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match ((from_unit, val_), to_unit) with | ((Foot, val_), Foot) -> (Foot, val_) | ((Foot, val_), Meter) -> (Meter, (val_*.0.3048)) | ((Foot, val_),Mile) -> (Mile, (val_/.5280.0)) | ((Meter, val_), Meter) -> (Meter, val_) | ((Meter, val_), Foot) -> (Foot, (val_ /. 0.3048)) | ((Meter, val_), Mile) -> (Mile, (val_/.0.3048)/.5280.0) |((Mile, val_), Mile) -> (Mile, val_) |((Mile, val_), Meter) -> (Meter, (val_*.0.3048 *. 5280.0)) | ((Mile, val_), Foot) -> (Foot, val_*.5280.0) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist0 = fst from_unit in let time0 = snd from_unit in let dist = fst to_unit in let time = snd to_unit in let d = snd (convert_dist (dist0, val_) dist) in let t = snd (convert_time(time0, 1.0) time) in (to_unit, d /. t) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_speed = snd (convert_speed (fst a, snd a) b_unit) in (b_unit, b_val +. a_speed) ;;
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (w,s) :: tl -> if (check s && sum_ (Branch (w,s)) <= w*.w) 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) = function | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux l (cur_el, cur_num + 1) (max_el,max_num) tl else if cur_num > max_num then aux l (hd,1) (cur_el, cur_num) tl else aux l (hd,1) (max_el,max_num) tl in match List.sort compare l with | [] -> notimplemented() | hd :: tl -> aux l (hd,1) (hd,0) tl ;;
let pair_mode (l: 'a list) : 'a * 'a = let list1 = List.tl l in let list2 = List.rev (List.tl (List.rev l )) in let combine = List.combine list2 list1 in mode combine ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second , _ ) -> if to_unit = Hour then ( Hour, val_ /. 3600.) else (from_unit, val_) | (Hour, _ ) -> if to_unit = Second then ( Second, val_ *. 3600.) else (from_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if to_unit = Foot then match (from_unit, val_) with | ( Mile, _ ) -> ( Foot, val_ *. 5280.) | ( Meter, _ ) -> ( Foot, val_ /. 0.3048) | ( Foot, _) -> ( Foot, val_) else if to_unit = Meter then match (from_unit, val_) with | ( Foot, _ ) -> ( Meter, val_ *. 0.3048) | ( Mile, _ ) -> ( Meter, val_ *. (5280. *. 0.3048)) | ( Meter, _) -> ( Meter, val_) else match (from_unit, val_) with | ( Foot, _ ) -> ( Mile, val_ /. 5280.) | ( Meter, _) -> ( Mile, val_ /. (0.3048 *. 5280.)) | ( Mile, _ ) -> ( Mile, val_ ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist,time) = from_unit in let (to_dist, to_time) = to_unit in let ( _ , dist_value) = convert_dist (dist, val_) to_dist in match time with | Second -> if to_time = Hour then (to_unit, dist_value *. 3600.) else (to_unit, dist_value) | Hour -> if to_time = Second then (to_unit, dist_value /. 3600.) else (to_unit, dist_value) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_unit,a_val) = a in let ( _ , a_in_b_value) = convert_speed (a_unit, a_val) b_unit in ( b_unit, a_in_b_value +. b_val) ;;
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 ** 2.) ) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> match check subtree with | false -> false | true -> match sum_ subtree 0. <= width ** 2. with | false -> false | true -> 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 | h::t -> if h = cur_el && cur_num+1 > max_num then aux t (cur_el, cur_num+1) (cur_el, cur_num+1) else if h = cur_el && cur_num+1 <= max_num then aux t (cur_el, cur_num+1) (max_el, max_num) else aux t (h,1) (max_el,max_num) in match (List.sort compare(l)) with | [] -> failwith "This not works." | h::t -> aux (List.sort compare(t)) (h,1) (h,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec make_l (l1: 'a list) (l2: ('a * 'a) list) : ('a * 'a) list = match l1 with | [] -> failwith "This not works." | h::t -> if (List.length l1)> 1 then make_l t (List.append [(h,(List.hd t))] l2) else l2 in if (List.length l < 2) then failwith "This not works. " else mode (make_l l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if (to_unit = from_unit) then (from_unit,val_) else match to_unit with | Hour -> (Hour,(val_ /. 3600.)) | Second -> (Second,(val_ *. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if (to_unit = from_unit) then (from_unit,val_) else match to_unit with | Foot -> if (from_unit = Meter)then (Foot,(val_ /. 0.3048)) else (Foot,(val_*.5280.)) | Meter -> if (from_unit = Foot)then (Meter,(val_ *. 0.3048)) else (Meter,((val_ *. 5280.)*. 0.3048)) | Mile -> if (from_unit = Foot)then (Mile,(val_ /. 5280.)) else (Mile,((val_ /. 0.3048) /. 5280.)) ;; let get_dist ((d,t): dist_unit * time_unit): dist_unit = d;; let get_time ((d,t): dist_unit * time_unit): time_unit = t;; let get_val ((form,val_): 'a * float): float = val_;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let t1=get_time from_unit in let d1=get_dist from_unit in let t2=get_time to_unit in let d2=get_dist to_unit in (to_unit,(get_val(convert_dist (d1,val_) d2))/.(get_val(convert_time (t1,1.) t2))) ;;
let dist_traveled (time : time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let t=get_time speed_unit in let d=get_dist speed_unit in (d,get_val(convert_time time t)*.speed_val) ;; let get_square (t: tree) : float = match t with | Leaf -> 0. | Branch (f, _) -> f*.f ;; let rec get_list_square (tl: tree list) : float = match tl with | [] -> 0. | h::t -> (get_square h) +. (get_list_square t) ;; let rec get_children (t: tree ) (tl: tree list) : tree list= match t with | Leaf -> List.append tl [Leaf] | Branch(f,tl_) -> List.append tl tl_ ;; let check_once (t: tree): bool= match t with | Leaf -> true | Branch(f,tl) -> (f*.f) >= (get_list_square tl) let rec check (tl: tree list): bool= match tl with | [] -> true | h::t -> (check_once h) && (check t) ;; let rec passes_da_vinci (t: tree) = (check_once t) && (check (get_children t [])) ;;
let mode (l: 'a list) : 'a = match l with | [] -> failwith "Undefined input." | _ -> let rec aux (l : 'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | x :: xs -> if x = cur_el then if x = max_el then aux xs (cur_el, 1 + cur_num) (cur_el, 1 + cur_num) else if cur_num + 1 > max_num then aux xs (x, cur_num + 1) (x, cur_num + 1) else aux xs (x, cur_num + 1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in aux (List.sort compare l) ((List.nth (List.sort compare l) 0), 0) ((List.nth (List.sort compare l) 0), 0) ;; let create_list_of_tuples (l: 'a list) : (('a * 'b) list) = let rec tuple_creator_helper (l: ('a list)) (result : (('a * 'b) list)) : (('a * 'b) list) = match l with | [] -> result | h :: [] -> result | x1 :: x2 :: rest -> tuple_creator_helper (x2 :: rest) (List.append result [(x1, x2)]) in tuple_creator_helper l [] ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Undefined input." else mode (create_list_of_tuples 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 (Hour, val_ /. 3600.0) else (from_unit, val_) | Hour -> if to_unit = Second then (Second, val_ *. 3600.0) 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 | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.0) | Foot -> (from_unit, val_)) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Mile -> (Mile, val_ /. (5280.0 *. 0.3048)) | Meter -> (from_unit, val_)) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.0) | Meter -> (Meter, val_ *. (5280.0 *. 0.3048)) | Mile -> (from_unit, val_)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (input_dist,input_time) = from_unit in let (output_dist,output_time) = to_unit in (output_dist, output_time),(snd(convert_dist (input_dist, val_) output_dist) /. snd(convert_time (input_time, 1.0) output_time)) ;;
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 square_count (tr) : float = let rec count_helper (tre) (result : float) : float = match tre with | [] -> result | Leaf :: rest -> count_helper rest result | Branch (valuee, inner_list) :: rest -> (if valuee <= 0.0 then failwith "Undefined input." else count_helper rest (result +. (valuee ** 2.))) in count_helper tr 0.0 ;; let rec iteration_list (tree_list) (result : bool) : bool = match tree_list with | [] -> result | x :: rest -> (match x with | Branch (value, list) -> iteration_list rest ((value ** 2. >= (square_count list)) && result) | Leaf -> iteration_list rest (result)) ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (value, ls) -> ( match ls with | [] -> (iteration_list ls (value >= (square_count ls))) | x :: xs -> (passes_da_vinci x) && (iteration_list xs ((value ** 2.0) >= (square_count ls))));;
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 h == cur_el then aux t (cur_el, (cur_num + 1)) (max_el, max_num) else if cur_num > max_num || (cur_num == max_num && cur_el < max_el )then aux t (h, 0) (cur_el, cur_num) else aux t (h, 0) (max_el, max_num) in match l with | [] -> failwith "Empty list" | _ -> aux (List.sort compare l) ((List.hd l), 0) ((List.hd l), 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if l == [] then failwith "Empty list" else let l1 = List.tl l and l2 = List.rev (List.tl (List.rev l)) in let superList = List.combine l2 l1 in mode superList ;;
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 if to_unit == Hour then (to_unit, val_ /. 3600.) else failwith "Wrong data type to convert to" | Hour -> if to_unit == Second then (to_unit, val_ *. 3600.) else if to_unit == Hour then (to_unit, val_) else failwith "Wrong data type to convert to" ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit == Meter then (to_unit, val_ *. 0.3048) else if to_unit == Mile then (to_unit, val_ /. 5280.) else if to_unit == Foot then (to_unit, val_) else failwith "Wrong data type to convert to" | Mile -> if to_unit == Meter then (to_unit, val_ *. 5280. *. 0.3048) else if to_unit == Mile then (to_unit, val_) else if to_unit == Foot then (to_unit, val_ *. 5280.) else failwith "Wrong data type to convert to" | Meter -> if to_unit == Meter then (to_unit, val_) else if to_unit == Mile then (to_unit, (val_ /. 0.3048) /. 5280. ) else if to_unit == Foot then (to_unit, val_ /. 0.3048) else failwith "Wrong data type to convert to" ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let time = convert_time (snd(from_unit), 1.) (snd(to_unit)) and distance = convert_dist (fst(from_unit), val_) (fst(to_unit)) in (((fst(distance),(fst(time)))), (snd(distance)) /. (snd(time))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let timmar = convert_time time (snd(speed_unit)) in (fst(speed_unit), snd(timmar) *. speed_val ) ;; 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) = notimplemented() in match List.sort compare l with | [] -> notimplemented() | x :: xs -> aux l (x,0) (x,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Hour, _) -> (match to_unit with | Hour -> (to_unit, snd(from_unit, val_)) | Second -> (to_unit, snd(from_unit, val_) *. 3600.)) | (Second, _) -> (match to_unit with | Second -> (to_unit, snd(from_unit, val_)) | Hour -> (to_unit, snd(from_unit, val_) /. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_) with | (Foot, _) -> (match to_unit with | Foot -> (to_unit, snd(from_unit, val_)) | Meter -> (to_unit, snd(from_unit, val_) *. 0.3048) | Mile -> (to_unit, snd(from_unit, val_) /. 5280.)) | (Meter, _) -> (match to_unit with | Meter -> (to_unit, snd(from_unit, val_)) | Foot -> (to_unit, snd(from_unit, val_) /. 0.3048) | Mile -> (to_unit, snd(from_unit, val_) /. 0.3048 /. 5280.)) | (Mile, _) -> (match to_unit with | Mile -> (to_unit, snd(from_unit, val_)) | Foot -> (to_unit, snd(from_unit, val_) *. 5280.) | Meter -> (to_unit, snd(from_unit, val_) *. 5280. *. 0.3048)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist_unit = fst(to_unit) and time_unit = snd(to_unit) and value = snd(from_unit, val_) in let dist_value = snd(convert_dist (fst from_unit, val_) dist_unit) and time_value = snd(convert_time (snd from_unit, 1.) time_unit) in match fst(fst(from_unit), val_) with | dist_unit -> (match fst(snd(from_unit), value) with | time_unit -> (to_unit, value) | _ -> (to_unit, value /. time_value)) | _ -> (match fst(snd(from_unit), val_) with | time_unit -> (to_unit, value /. dist_value) | _ -> (to_unit, value *. (dist_value /. time_value))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = notimplemented () ;; let rec passes_da_vinci t = notimplemented () ;;
let domainError () = failwith "Domain is not correct" let compare (a: ('a * int)) (b: ('a * int)): int = let count_a = snd a in let count_b = snd b in if (count_a - count_b) = 0 then (if (fst a) > (fst b) then 1 else -1) else count_b - count_a ; ;; let rec find_add (el:'a) (counts: ('a * int) list): ('a * int) list = match counts with | [] -> [(el, 1)] | x :: xs -> if (fst x) = el then ((fst x, (snd x + 1)) :: xs) else x :: find_add el xs ;; let count (l: 'a list): ('a * int) list = let rec help (l: 'a list) (counts: ('a * int) list): ('a * int) list = match l with | [] -> counts | x :: xs -> help xs (find_add x counts) in help l [] ;;
let mode (l: 'a list) : 'a = if List.length l < 1 then domainError () else count l |> List.sort compare |> List.hd |> fst;; let rec make_bi_pairs (l: 'a list) : ('a * 'a) list = match l with | [] -> [] | [_] -> [] | x1 :: x2 :: xs -> (x1, x2) :: make_bi_pairs (x2::xs) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then domainError () else mode (make_bi_pairs l) ;;
let convert_time ((from_unit, val_) : time_unit value) (to_unit : time_unit): time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | _ -> (to_unit, val_) ;; let rec convert_dist ((from_unit, val_) : dist_unit value) (to_unit: dist_unit) : dist_unit value = if from_unit != to_unit then match (from_unit, to_unit) with | (Foot, Meter) -> (to_unit, val_ *. 0.3048) | (Foot, Mile) -> (to_unit, val_ /. 5280.) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (f, t) -> convert_dist (convert_dist (f, val_) Foot) t else(from_unit, val_) ;; let rec convert_speed ((from_unit, val_) : speed_unit value) (to_unit: speed_unit) : speed_unit value = if val_ < 0. then domainError () else if fst from_unit != (fst to_unit) then convert_speed ((fst to_unit, snd from_unit), snd (convert_dist (fst from_unit, val_) (fst to_unit))) to_unit else (to_unit, 1. /. (snd (convert_time (snd from_unit, (1. /. val_)) (snd to_unit)))) ;;
let dist_traveled (time : time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit, speed_val *. (snd (convert_time time (snd speed_unit)))) ;; let get_thkns (tr : tree) : float = match tr with | Leaf -> 0. | Branch (w, _) -> w let check_valid (w: float) (sub: tree list): bool = (w ** 2.) >= (List.fold_left (fun a b -> a +. ((get_thkns b) ** 2.) ) 0. sub) let rec passes_da_vinci (tr : tree) : bool = match tr with | Leaf -> true | Branch (w, sub) -> (check_valid w sub) && (List.fold_left (fun a b -> a && (passes_da_vinci b)) true sub );;
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 -> if (xs != [] && x = List.hd(xs)) then aux xs (x, cur_num + 1) (max_el, max_num) else if xs != [] then if (max_num < cur_num) then aux xs (List.hd(xs), 1) (cur_el, cur_num) else aux xs (List.hd(xs), 1) (max_el, max_num) else if max_num < cur_num then cur_el else max_el in let sorted_list = List.sort compare(l) in match l with | [] -> failwith "Invalid input list" | hd::_ -> aux sorted_list (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec make_pairs b pairs = if List.length b < 2 then pairs else let tupl = (List.nth b 0, List.nth b 1) in make_pairs (List.tl b) (tupl::pairs) in if List.length l < 2 then failwith "The input list should have length at least 2" else mode (make_pairs l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> if from_unit = to_unit then (to_unit, val_) else to_unit, (val_ *. 3600.) | Hour -> if from_unit = to_unit then (to_unit, val_) else to_unit, val_ *. (1. /. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> if from_unit = to_unit then (to_unit, val_) else if from_unit = Meter then to_unit, val_ *. (1. /. 0.3048) else to_unit, val_ *. 5280. | Meter -> if from_unit = to_unit then (to_unit, val_) else if from_unit = Foot then to_unit, val_ *. 0.3048 else to_unit, val_ *. (5280. *. 0.3048) | Mile -> if from_unit = to_unit then (to_unit, val_) else if from_unit = Foot then to_unit, val_ *. (1. /. 5280.) else to_unit, val_ *. (1. /. (5280. *. 0.3048)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_time = snd from_unit in let to_time = snd to_unit in let (a,x) = convert_time (from_time, 1.) to_time in let from_dist = fst from_unit in let to_dist = fst to_unit in let (b,y) = convert_dist (from_dist, 1.) to_dist in to_unit, (val_ *. y) /. x ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time_unit, time_val = time in let to_time_unit = snd speed_unit in let a, time_taken = convert_time (time_unit, time_val) to_time_unit in fst speed_unit, speed_val *. time_taken ;; let rec 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 ** 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 invalid () = failwith "Invalid 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 | hd :: tl -> if hd = cur_el then aux tl (hd, 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 if l = [] then invalid() else let (k: 'a list) = List.sort compare(l) in aux k ((List.hd k, 0) : 'a * int) ((List.hd k, 0) : 'a * int) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length (l) < 2 then invalid() else (let l1 = List.tl (l) in let l2 = List.rev(l) in let l2 = List.tl (l2) in let l2 = List.rev(l2) in let l3 = List.combine(l2)(l1) in mode (l3) ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let temp = (from_unit, val_) in if fst(temp) = to_unit then temp else if fst(temp) = Second then (to_unit,snd(temp) /. 3600.) else (to_unit,snd(temp) *. 3600.) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let temp = (from_unit, val_) in if fst(temp) = to_unit then temp else match fst(temp) with | Foot -> (if to_unit = Meter then (to_unit,snd(temp) *. 0.3048) else (to_unit, snd(temp) /. 5280.)) | Meter -> (if to_unit = Foot then (to_unit, snd(temp) /. 0.3048) else (to_unit, snd(temp) /. (5280. *. 0.3048))) | Mile -> (if to_unit = Foot then (to_unit, snd(temp) *. 5280.) else (to_unit, snd(temp) *. 0.3048 *. 5280.)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let temp = (from_unit, val_) in let initialdist = fst(fst(temp)) in let initialtime = snd(fst(temp)) in let wanteddist = fst(to_unit) in let wantedtime = snd(to_unit) in if initialdist = wanteddist && initialtime = wantedtime then temp else if initialdist = wanteddist then (to_unit, snd(convert_time (wantedtime, snd(temp)) initialtime)) else let step1 = convert_dist (initialdist,snd(temp)) wanteddist in let step2 = convert_time (wantedtime, snd(step1)) initialtime in (to_unit, snd(step2)) ;;