text
stringlengths
0
601k
let passes_da_vinci (t : tree) : bool = let rec auxu (x: float) (y: 'a list) = match y with | [] -> true | Leaf :: ys -> auxu (x) (ys) | Branch (widthx, subtree) :: ys -> let width = x *. x in let children = (getneighbours (y) 0.0) in if (compare width children) < 0 then false else let first = auxu (widthx) (subtree) in first && (auxu x ys) in match t with | Leaf -> true | Branch( _ , _ ) -> auxu (getfirstele t) (getsecondele 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 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 List.sort compare l with | [] -> failwith "Invalid input" | hd :: tl -> aux (tl) (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid input" | _ -> match List.tl (List.rev l) with | [] -> failwith "Invalid input" | l2 -> mode (List.combine (List.rev l2) (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 -> (to_unit, val_) | Hour -> (to_unit,val_/.3600.) ) | Hour -> (match to_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 from_unit with | Foot -> ( match to_unit with | Foot -> (to_unit, val_) | Meter -> (to_unit, 0.3048*.val_) | 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 (distFrom_unit, timeFrom_unit) = from_unit in let (dist, dist_val) = convert_dist (distFrom_unit, val_) (fst(to_unit)) in let (time, time_val) = convert_time (timeFrom_unit, 1.) (snd(to_unit)) in ((dist,time), dist_val/.time_val);;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((distU1, timeU1), val1) = a in let (distU2, timeU2) = b_unit in let ((newdist, newtime), val3) = convert_speed ((distU1,timeU1), val1) (distU2, timeU2) in ((distU2, timeU2), (val3+.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 -> check tl | Branch (width, subtree) :: tl -> if ( not (check subtree) || sum_ subtree 0. > width**2.) then false else check tl in match [t] with | [] -> failwith "Invalid tree" | [t] -> check [t] ;;
let domain() = failwith "This is invalid input" ;; let getHead (l : 'a list) : 'a = match l with | [] -> domain() | hd :: tl -> hd ;; let getRemains (l : 'a list) : 'a list = match l with | [] -> [] | hd :: tl -> tl ;;
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 + 1 > max_num then aux tl (cur_el, cur_num + 1) (cur_el, cur_num + 1) else if hd = cur_el && cur_num + 1 <= max_num then aux tl (cur_el, cur_num + 1) (max_el, max_num) else aux tl (hd,1) (max_el, max_num) in if l == [] then domain() else let ls = List.sort compare (l) in let hd = getHead ls in let tl = getRemains ls in aux tl (hd,1) (hd,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length (l) < 2 then domain() else begin let l1 = List.rev(getRemains(List.rev(l))) in let l2 = getRemains(l) in let ls = List.combine l1 l2 in mode ls end ;;
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, Hour) -> (Hour, val_) | (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.) | (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 d_fUnit = fst(from_unit) in let d_tUnit = fst(to_unit) in let distance = convert_dist (d_fUnit, val_) d_tUnit in let t_fUnit = snd(from_unit) in let t_tUnit = snd(to_unit) in let time = convert_time (t_fUnit, 1.) t_tUnit in ((d_tUnit, t_tUnit), snd(distance) /. snd(time)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let s1 = convert_speed (fst(a), snd(a)) b_unit in (b_unit, snd(s1) +. 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+(int_of_float(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 <= int_of_float(width**2.))) == false) then false else check tl in check [t] ;;
let domain() = failwith "This is invalid input" ;; let getHead (l : 'a list) : 'a = match l with | [] -> domain() | hd :: tl -> hd ;; let getRemains (l : 'a list) : 'a list = match l with | [] -> [] | hd :: tl -> tl ;;
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 + 1 > max_num then aux tl (cur_el, cur_num + 1) (cur_el, cur_num + 1) else if hd = cur_el && cur_num + 1 <= max_num then aux tl (cur_el, cur_num + 1) (max_el, max_num) else aux tl (hd,1) (max_el, max_num) in if l == [] then domain() else let ls = List.sort compare (l) in let hd = getHead ls in let tl = getRemains ls in aux tl (hd,1) (hd,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length (l) < 2 then domain() else begin let l1 = List.rev(getRemains(List.rev(l))) in let l2 = getRemains(l) in let ls = List.combine l1 l2 in mode ls end ;;
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, Hour) -> (Hour, val_) | (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.) | (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 d_fUnit = fst(from_unit) in let d_tUnit = fst(to_unit) in let distance = convert_dist (d_fUnit, val_) d_tUnit in let t_fUnit = snd(from_unit) in let t_tUnit = snd(to_unit) in let time = convert_time (t_fUnit, 1.) t_tUnit in ((d_tUnit, t_tUnit), snd(distance) /. snd(time)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let s1 = convert_speed (fst(a), snd(a)) b_unit in (b_unit, snd(s1) +. b_val) ;;
let passes_da_vinci t = let rec sum l acc = match l with | [] -> 0 | Leaf :: tl -> 0 | Branch (width, subtree) :: tl -> int_of_float(width**2.) + sum subtree acc + sum tl acc in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (((check subtree) && (sum subtree 0 <= int_of_float(width**2.))) == false) then false else check tl in check [t] ;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in if List.length l = 1 then List.hd l ; 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 (List.tl l) (cur_el, cur_num) (max_el, max_num+1) else if cur_num > max_num then aux (List.tl l) ((List.hd (List.tl l)), 1) (cur_el, cur_num) in aux l ((List.hd l), 1) ((),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 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 from_unit = to_unit then (to_unit, val_) else if to_unit = Mile then (to_unit, (val_ /. 5280.)) else (to_unit, (val_ *. 0.3048)) | Meter -> if from_unit = to_unit then (to_unit, val_) else if to_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 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 (from_unit, val_) else let dis (d1, t1) (d2, t2) val1 = convert_dist (d1, val1) d2 in let tim (d1, t1) (d2, t2) val1 = convert_time (t1, val1) t2 in let getval (unit, val2) = val2 in let post_dis u1 u2 val3 = getval( dis u1 u2 val3) in let post_tim u1 u2 val3 = getval (tim u1 u2 val3) in (to_unit, (val_ *. post_dis from_unit to_unit val_) /. (post_tim from_unit to_unit val_ )) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let getunit (x, y) = x in let getval (x, y) = y in let gettime time (d1, t1) = getval(convert_time time t1) in (getunit (speed_unit), ((gettime time speed_unit) *. speed_val)) ;; let rec passes_da_vinci t = true ;;
let mode (l: 'a list) : 'a = match l with |a::_-> let rec helper l b n acc1 acc2= match l,b with |[],_-> n |h::t,[] ->if acc1<acc2 then helper t t h acc2 0 else helper t t n acc1 0 |h::t,h1::t1-> if h1=h then helper l t1 n acc1 (acc2+1) else helper l t1 n acc1 acc2 in helper (List.sort compare l) (List.sort compare l) a 0 0 ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l acc = match l with |[]->acc |h::[]->acc |h::m::[]->helper [] ((h,m)::acc) |h::m::t->helper (m::t) ((h,m)::acc) in mode (helper l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit,to_unit with | Second,Second->(to_unit, val_) | Second,Hour->(to_unit, val_ /.3600.) | Hour,Hour->(to_unit, val_ ) | 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 -> (to_unit,val_) |Foot ,Meter ->(to_unit,(val_ *. 0.3048)) |Foot ,Mile ->(to_unit,(val_ /. 5280.)) |Mile ,Foot ->(to_unit,(val_ *. 5280.)) |Mile ,Meter ->(to_unit,(val_ *. (0.3048 *. 5280.))) |Meter ,Mile ->(to_unit,(val_ /.(0.3048 *. 5280.))) |Meter ,Meter ->(to_unit,val_) |Meter ,Foot ->(to_unit,val_/.0.3048) |Mile ,Mile ->(to_unit,val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit,to_unit with |(a,b),(c,d) -> match convert_dist (a,val_) c with |_,v -> match convert_time (d,v) b with |_,v1 -> (to_unit,v1) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match speed_unit with |a,b->match convert_time time b with |_,t-> (a ,speed_val *. t) ;; 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 | [] -> max_el | hd :: tl -> if cur_el = hd && cur_num > max_num -1 then aux tl (cur_el,max_num+1) (cur_el,max_num+1) else if cur_el = hd && cur_num <= max_num -1 then 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 "Invalid Input" | _ -> 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 failwith "Invalid Input" else let l1 = l in let l2 = l in mode (List.combine (List.rev (List.tl (List.rev l1))) (List.tl l2)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Hour -> if to_unit = Hour then (Hour,val_) else (to_unit, val_ *. 3600.) |Second -> if to_unit = Hour then (Hour, val_ /. 3600.) else (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Foot -> if to_unit = Foot then (Foot,val_) else if to_unit = Meter then (Meter,val_ *. 0.3048) else (to_unit, val_ /. 5280.) |Meter -> if to_unit = Meter then (Meter,val_) else if to_unit = Foot then (Foot,val_ /. 0.3048) else (to_unit, (val_ /. 0.3048) /. 5280.) |Mile -> if to_unit = Mile then (Mile,val_) else if to_unit = Foot then (Foot,val_ *. 5280.) else (to_unit, val_ *. 5280. *. 0.3048) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "invalid input" else if fst from_unit = fst to_unit && snd from_unit = snd to_unit then (to_unit, val_) else let dist_convert = (convert_dist (fst from_unit, val_) (fst to_unit)) in let time_convert = (convert_time (snd from_unit, 1.0) (snd to_unit)) in (to_unit, (snd dist_convert) /. (snd time_convert)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if snd a < 0. || b_val < 0. then failwith "invalid input" else if fst a = b_unit then (b_unit, snd a +. b_val) else let converted = convert_speed a (b_unit) in (b_unit, snd converted +. b_val) ;;
let passes_da_vinci t = let rec check (l: tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not (check subtree) || tree_sum_child 0. subtree > width *. width then false else check tl in match t with | Leaf -> true | Branch (width, subtree) -> if (tree_sum_child 0. subtree) > (width *. width) then false else check 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 | [] -> max_el | hd :: tl -> if cur_el = hd && cur_num > max_num -1 then aux tl (cur_el,max_num+1) (cur_el,max_num+1) else if cur_el = hd && cur_num <= max_num -1 then 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 "Invalid Input" | _ -> 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 failwith "Invalid Input" else let l1 = l in let l2 = l in mode (List.combine (List.rev (List.tl (List.rev l1))) (List.tl l2)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Hour -> if to_unit = Hour then (Hour,val_) else (to_unit, val_ *. 3600.) |Second -> if to_unit = Hour then (Hour, val_ /. 3600.) else (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Foot -> if to_unit = Foot then (Foot,val_) else if to_unit = Meter then (Meter,val_ *. 0.3048) else (to_unit, val_ /. 5280.) |Meter -> if to_unit = Meter then (Meter,val_) else if to_unit = Foot then (Foot,val_ /. 0.3048) else (to_unit, (val_ /. 0.3048) /. 5280.) |Mile -> if to_unit = Mile then (Mile,val_) else if to_unit = Foot then (Foot,val_ *. 5280.) else (to_unit, val_ *. 5280. *. 0.3048) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "invalid input" else if fst from_unit = fst to_unit && snd from_unit = snd to_unit then (to_unit, val_) else let dist_convert = (convert_dist (fst from_unit, val_) (fst to_unit)) in let time_convert = (convert_time (snd from_unit, 1.0) (snd to_unit)) in (to_unit, (snd dist_convert) /. (snd time_convert)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if snd a < 0. || b_val < 0. then failwith "invalid input" else if fst a = b_unit then (b_unit, snd a +. b_val) else let converted = convert_speed a (b_unit) in (b_unit, snd converted +. b_val) ;;
let passes_da_vinci t = let rec check (l: tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not (check subtree) || tree_sum_child 0. subtree > 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 cur_el = hd && cur_num > max_num -1 then aux tl (cur_el,max_num+1) (cur_el,max_num+1) else if cur_el = hd && cur_num <= max_num -1 then 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 "Invalid Input" | _ -> 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 failwith "Invalid Input" else let l1 = l in let l2 = l in mode (List.combine (List.rev (List.tl (List.rev l1))) (List.tl l2)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Hour -> if to_unit = Hour then (Hour,val_) else (to_unit, val_ *. 3600.) |Second -> if to_unit = Hour then (Hour, val_ /. 3600.) else (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Foot -> if to_unit = Foot then (Foot,val_) else if to_unit = Meter then (Meter,val_ *. 0.3048) else (to_unit, val_ /. 5280.) |Meter -> if to_unit = Meter then (Meter,val_) else if to_unit = Foot then (Foot,val_ /. 0.3048) else (to_unit, (val_ /. 0.3048) /. 5280.) |Mile -> if to_unit = Mile then (Mile,val_) else if to_unit = Foot then (Foot,val_ *. 5280.) else (to_unit, val_ *. 5280. *. 0.3048) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "invalid input" else if fst from_unit = fst to_unit && snd from_unit = snd to_unit then (to_unit, val_) else let dist_convert = (convert_dist (fst from_unit, val_) (fst to_unit)) in let time_convert = (convert_time (snd from_unit, 1.0) (snd to_unit)) in (to_unit, (snd dist_convert) /. (snd time_convert)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if snd a < 0. || b_val < 0. then failwith "invalid input" else if fst a = b_unit then (b_unit, snd a +. b_val) else let converted = convert_speed a (b_unit) in (b_unit, snd converted +. b_val) ;;
let passes_da_vinci t = let rec check (l: tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && (tree_sum_child 0. subtree) <= (width *. width)) == 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 | [] -> max_el | hd :: tl -> if cur_el = hd && cur_num > max_num -1 then aux tl (cur_el,max_num+1) (cur_el,max_num+1) else if cur_el = hd && cur_num <= max_num -1 then 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 "Invalid Input" | _ -> 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 failwith "Invalid Input" else let l1 = l in let l2 = l in mode (List.combine (List.rev (List.tl (List.rev l1))) (List.tl l2)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Hour -> if to_unit = Hour then (Hour,val_) else (to_unit, val_ *. 3600.) |Second -> if to_unit = Hour then (Hour, 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 (Foot,val_) else if to_unit = Meter then (Meter,val_ *. 0.3048) else (to_unit, val_ /. 5280.) |Meter -> if to_unit = Meter then (Meter,val_) else if to_unit = Foot then (Foot,val_ /. 0.3048) else (to_unit, (val_ /. 0.3048) /. 5280.) |Mile -> if to_unit = Mile then (Mile,val_) else if to_unit = Foot then (Foot,val_ *. 5280.) else (to_unit, val_ *. 5280. *. 0.3048) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if fst from_unit = fst to_unit && snd from_unit = snd to_unit then (to_unit, val_) else let dist_convert = (convert_dist (fst from_unit, val_) (fst to_unit)) in let time_convert = (convert_time (snd from_unit, 1.0) (snd to_unit)) in (to_unit, (snd dist_convert) /. (snd time_convert)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if fst a = b_unit then (b_unit, snd a +. b_val) else let converted = convert_speed a (b_unit) in (b_unit, snd converted +. b_val) ;;
let passes_da_vinci t = let rec check (l: tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && (tree_sum_child 0. subtree) <= (width *. width)) == 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 | [] -> max_el | hd :: tl -> if cur_el = hd && cur_num > max_num -1 then aux tl (cur_el,max_num+1) (cur_el,max_num+1) else if cur_el = hd && cur_num <= max_num -1 then 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 "Invalid Input" | _ -> 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 failwith "Invalid Input" else let l1 = l in let l2 = l in mode (List.combine (List.rev (List.tl (List.rev l1))) (List.tl l2)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Hour -> if to_unit = Hour then (Hour,val_) else (to_unit, val_ *. 3600.) |Second -> if to_unit = Hour then (Hour, val_ /. 3600.) else (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "invalid input" else match from_unit with |Foot -> if to_unit = Foot then (Foot,val_) else if to_unit = Meter then (Meter,val_ *. 0.3048) else (to_unit, val_ /. 5280.) |Meter -> if to_unit = Meter then (Meter,val_) else if to_unit = Foot then (Foot,val_ /. 0.3048) else (to_unit, (val_ /. 0.3048) /. 5280.) |Mile -> if to_unit = Mile then (Mile,val_) else if to_unit = Foot then (Foot,val_ *. 5280.) else (to_unit, val_ *. 5280. *. 0.3048) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "invalid input" else if fst from_unit = fst to_unit && snd from_unit = snd to_unit then (to_unit, val_) else let dist_convert = (convert_dist (fst from_unit, val_) (fst to_unit)) in let time_convert = (convert_time (snd from_unit, 1.0) (snd to_unit)) in (to_unit, (snd dist_convert) /. (snd time_convert)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if snd a < 0. || b_val < 0. then failwith "invalid input" else if fst a = b_unit then (b_unit, snd a +. b_val) else let converted = convert_speed a (b_unit) in (b_unit, snd converted +. b_val) ;;
let passes_da_vinci t = let rec check (l: tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not (check subtree) || tree_sum_child 0. subtree > 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 |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 let l = List.sort compare l in match l with | [] -> failwith "Invalid input." | hd::tl -> aux tl (hd,1)(hd,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let l1=l and l2=l in let hd1::tl1 = l1 and hd2::tl2 = List.rev l2 in let l3 = List.combine (List.rev tl2) tl1 in mode l3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if (from_unit = Second) && (to_unit = Hour) then (to_unit, val_ /.3600.) else if (from_unit = Hour) && (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,to_unit with | Mile, Meter -> (to_unit, val_ *.1609.344) | Mile, Foot -> (to_unit, val_ *.5280.) | Meter, Mile -> (to_unit, val_ /.1609.344) | Meter, Foot -> (to_unit, val_ /.0.3048) | Foot, Meter -> (to_unit, val_ *.0.3048) | Foot, Mile -> (to_unit, val_ /.5280.) |_ -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let x = fst(from_unit) and y = snd(from_unit) in (to_unit, snd(convert_dist(x, 1.) (fst to_unit)) /.snd(convert_time(y, 1.) (snd to_unit))*.val_ ) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = b_unit, snd(convert_speed (fst a, snd a) b_unit) +. b_val ;;
let passes_da_vinci t = let rec sum_ l = match l with | [] -> 0. | Leaf :: tl-> 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 not ((check subtree) && (sum_ subtree<= width**2.)) 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) = notimplemented () in notimplemented () ;;
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,to_unit with |Hour,Hour->(Hour,val_) |Second,Second-> (Second,val_) |Hour,Second->(Hour, val_*.3600.) |Second, Hour->(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,Mile->(Mile, val_/.5280.) |Mile,Foot->(Foot,val_*.5280.) |Foot,Meter->(Meter,val_*.0.3048) |Meter,Foot->(Foot,val_/.0.3048) |Mile,Meter->(Meter,val_*.5280.*.0.3048) |Meter,Mile->(Mile,val_/.5280./.0.3048) ;;
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 = match l with |[] -> failwith "List is empty" |_ -> let rec aux l (pos: int) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if pos = (List.length l - 1) then max_el else let next_el = List.nth l (pos + 1) in let next_num = (if (List.nth l (pos + 1)) = cur_el then (cur_num + 1) else 1) in let next_max_el = (if next_num > max_num then next_el else max_el) in let next_max_num = (if next_num > max_num then next_num else max_num) in aux l (pos + 1) (next_el,next_num) (next_max_el,next_max_num) in let sorted = List.sort compare l in aux sorted 0 (List.nth sorted 0,1) (List.nth sorted 0,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with |[] -> failwith "List is empty" |_::[] -> failwith "List is too small" |_::rest -> let first_element_list = List.rev (List.tl (List.rev l)) in let second_element_list = rest in let pairs_list = List.combine first_element_list second_element_list in mode pairs_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 let factor = if from_unit = Second then 1. /. 3600. else 3600. in (to_unit,factor *. val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (from_unit, val_) else let factor = if from_unit = Foot then (if to_unit = Meter then 0.3048 else 1. /. 5280.) else if from_unit = Meter then (if to_unit = Foot then 1. /. 0.3048 else 1. /. 0.3048 /. 5280.) else (if to_unit = Foot then 5280. else 5280. *. 0.3048) in (to_unit,factor *. 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 let (_,dist_value) = convert_dist (from_dist,val_) to_dist in let (_,time_value) = convert_time (from_time,1.) to_time in ((to_dist,to_time), dist_value /. time_value) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit,speed_time_unit) = speed_unit in let (time_unit,time_val) = time in let (_,converted_time_val) = convert_time (time_unit,time_val) speed_time_unit in (dist_unit,speed_val *. converted_time_val) ;; let rec passes_da_vinci t = let rec check_list_of_trees list = match list with |[] -> true |first::rest -> let check_first = passes_da_vinci first in if not check_first then false else if List.length list = 1 then true else check_list_of_trees rest in let rec sum_of_squares list sum = match list with | [] -> 0. | first::rest -> let first_width = match first with | Leaf -> 0. | Branch (float,_) -> float in if (List.length list) = 1 then (first_width *. first_width) +. sum else sum_of_squares rest ((first_width *. first_width) +. sum) in let parent_square = match t with |Leaf -> 0. |Branch (float,_) -> float *. float in let children_list = match t with |Leaf -> [] |Branch (_,list) -> list in let sum_of_children_squares = sum_of_squares children_list 0. in let current_branch_check = (parent_square >= sum_of_children_squares) in let children_check = check_list_of_trees children_list in current_branch_check && children_check ;;
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) = let max_el = (if cur_num > max_num then cur_el else max_el) in let max_num = (if cur_num > max_num then cur_num else max_num) in match l with | _::[] -> max_el | hd :: hd2 :: tl -> if (hd2 = cur_el) then let cur_num = cur_num + 1 in aux ([hd2]@tl) (hd2, cur_num) (max_el, max_num) else let cur_el = hd2 in let cur_num = 1 in aux ([hd2]@tl) (cur_el, cur_num) (max_el, max_num) in match l with | [] -> failwith "error" | hd :: tl -> aux l (hd, 1) (hd, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | hd:: tl -> let l1 = tl in match List.rev l with | hd :: tl -> let l2 = List.rev tl 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 -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) | _ -> failwith "error" ) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) | _ -> failwith "error" ) ;;
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 "error"; ) | Meter -> (match to_unit with | Meter -> (Meter, val_); | Foot -> (Foot, (val_ /. 0.3048)); | Mile -> (Mile, ((val_/. 0.3048) /. 5280.)); | _ -> failwith "error"; ) | Mile -> (match to_unit with | Mile -> (Mile, val_); | Foot -> (Foot, (val_ *. 5280.)); | Meter -> (Meter, ((val_ *. 5280. *. 0.3048))); | _ -> failwith "error"; ) |_ -> failwith "error"; ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist_val = ((snd (convert_dist (fst from_unit, val_) (fst to_unit)))) in let time_val = ((snd (convert_time (snd to_unit, dist_val) (snd from_unit)))) in (to_unit, time_val) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let first_speed = snd (convert_speed (a) (b_unit)) in (b_unit, ((first_speed) +. b_val)) ;;
let passes_da_vinci t = let rec sum_ t acc = match t with | Leaf -> 0. | Branch (width, subtree) -> acc +. width in let rec check l = match l with | [] -> true; | Leaf :: tl -> true; | Branch (width, subtree) :: tl -> if ((passes_da_vinci subtree) && (sum_ subtree <= width)) == false then false else (passes_da_vinci tl) in check [t] ;;
let mode (l: 'a list) : 'a = let newList = List.sort compare l in let head = match newList with | []-> failwith "Invalid input" | h::_ -> h 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::xs -> (match x with | _ when cur_num = -1 -> aux xs (x,1)(x,1) | _ when x<>cur_el && cur_num>max_num-> aux xs (x,1)(cur_el,cur_num) | _ when x<>cur_el && cur_num<=max_num->aux xs (x,1)(max_el,max_num) | cur_el -> aux xs (x,cur_num+1)(max_el,max_num) ) in aux (newList) (head,-1) (head,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec create_tuples l acc = let get_tail l = match l with | _::t -> t | [] -> failwith "Was not supposed to happend" in match l with | a::b::_ -> create_tuples (get_tail l) ((a,b)::acc) | _ -> acc in mode (create_tuples l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (from_unit,val_) else if from_unit = Hour then (Second, val_*.3600.) else (Hour, val_/.3600.) ;;