text
stringlengths
0
601k
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.) | (Second, Second) -> (Second, val_) | (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, 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 = let val_1 = convert_dist ((fst from_unit), val_) (fst to_unit) in let val_2 = convert_time ((snd from_unit), (1. /. (snd val_1))) (snd to_unit) in to_unit, (1. /. (snd val_2)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_converted = convert_speed a b_unit in let total_val = (snd a_converted) +. b_val in b_unit, total_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 check [t] ;;
let mode (l: 'a list) : 'a = match l with |[] -> failwith "Input is invalid" |_ -> 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 aux tl (hd, 1) (max_el, max_num) in aux (List.sort compare l) (List.hd (List.sort compare l), 0) (List.hd (List.sort compare l), 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) < 2 then failwith "Invalid input" else let l1_hd :: l1_tl = l in let l2_hd :: l2_tl = List.rev l in let l2_tl = List.rev l2_tl in let bi_gram_l = List.combine l2_tl l1_tl in mode bi_gram_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.) | (Second, Second) -> (Second, val_) | (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, 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 = let val_1 = convert_dist ((fst from_unit), val_) (fst to_unit) in let val_2 = convert_time ((snd from_unit), (1. /. (snd val_1))) (snd to_unit) in to_unit, (1. /. (snd val_2)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_converted = convert_speed a b_unit in let total_val = (snd a_converted) +. b_val in b_unit, total_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 check [t] ;;
let mode (l: 'a list) : 'a = let gfe l = match l with |x::xs -> x,xs |_ -> failwith "invalid input" in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if l = [] && max_num < cur_num then cur_el, cur_num else if l = [] then max_el, max_num else if cur_num > max_num then let (a,b) = gfe l in if cur_el = a then aux b (a,cur_num + 1) (cur_el, cur_num) else aux b (a, 1) (cur_el, cur_num) else let (a,b) = gfe l in if cur_el = a then aux b (a,cur_num + 1) (max_el, max_num) else aux b (a, 1) (max_el, max_num) in let g = List.sort compare l in let (a,b) = gfe g in let (x,y) = aux b (a,1) (a,1) in x ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l = 1 then failwith "invalid input" else if List.length l = 0 then failwith "invalid input" else let l1 = List.tl l and lrev = List.rev l in let l2rev = List.tl lrev in let l2 = List.rev l2rev in let lcomplete = List.combine l2 l1 in mode lcomplete ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Hour -> (match to_unit with |Second -> Second, (val_*.3600.) |Hour -> Hour,(val_)) |Second -> (match to_unit with |Second -> Second, (val_) |Hour -> Hour, (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 = let (a,b) = from_unit and (c,d) = to_unit in let (e,f) = convert_dist (a,val_) c in let (g,h) = convert_time (b,1.) d in to_unit, f/.h ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (x,y) = a in let (m,n) = convert_speed (x,y) b_unit in b_unit, (n+.b_val) ;;
let passes_da_vinci t = let rec sumsquare l acc = match l with |[] -> acc |Leaf :: tl -> sumsquare tl acc |Branch (w,sub) :: tl -> sumsquare tl (acc +. (w*.w)) in let rec treerec y = match y with |[] -> true |Leaf :: tl -> treerec tl |Branch (w,sub) :: tl -> if not ((treerec sub) && ((sumsquare sub 0.) <= (w*.w))) then false else treerec tl in treerec [t] ;;
let mode (l: 'a list) : 'a = if List.length l < 1 then failwith "This list is empty" else 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 | [] -> max_el | hd::tl -> let head = hd in if head = cur_el then aux tl (cur_el, (cur_num + 1)) (max_el, max_num) else aux tl (head, 1) (max_el, max_num) in let s_list = List.sort compare l in aux (List.tl s_list) ((List.hd s_list), 1) ((List.hd s_list), 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "This list is too short" else let front = List.rev ( List.tl (List.rev l)) in let back = List.tl l in let pairs = List.combine front back in mode pairs ;;
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.)) |_ -> (from_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_ *. (0.3048 *. 5280.))) |_ -> (from_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist = convert_dist ((fst from_unit), val_) (fst to_unit) in let time = convert_time ((snd to_unit), (snd dist)) (snd from_unit) in let speed = (to_unit, (snd time)) in speed ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time_conv = convert_time time (snd speed_unit) in let dist = ((fst speed_unit), (speed_val *. (snd time_conv))) in dist ;; let rec passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: sub -> sum_ sub acc |Branch (width, subtree) :: sub -> sum_ sub (acc +. (width *. width)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && ((sum_ subtree 0.) <= (width *. width))) then false else check tl in check [t] ;;
let mode (l: 'a list) : 'a = match l with | [] -> failwith "Empty list" | _ -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | head::sublist -> aux sublist (head, if head = cur_el then cur_num + 1 else 1) (if cur_num + 1 > max_num then (cur_el, cur_num + 1) else (max_el, max_num)) in aux (List.sort compare l) (List.hd l, 0) (List.hd l, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "Not enough items" else let rec pair_list sublist new_list = if List.length sublist <= 1 then new_list else let rest = function | [] -> [] | _::s -> s in pair_list (rest sublist) (List.append new_list [List.hd sublist, List.hd (rest sublist)]) in mode (pair_list l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "Cannot have negative time" else match (from_unit, val_) with | (Second, s_val) -> (match to_unit with | Second -> (Second, s_val) | Hour -> (Hour, s_val /. 3600.)) | (Hour, h_val) -> (match to_unit with | Second -> (Second, h_val *. 3600.) | Hour -> (Hour, h_val)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "Cannot have negative distance" else match (from_unit, val_) with | (Foot, f_val) -> (match to_unit with | Foot -> (Foot, f_val) | Meter -> (Meter, f_val *. 0.3048) | Mile -> (Mile, f_val /. 5280.)) | (Meter, m_val) -> (match to_unit with | Foot -> (Foot, m_val /. 0.3048) | Meter -> (Meter, m_val) | Mile -> (Mile, (m_val /. 0.3048) /. 5280.)) | (Mile, mi_val) -> (match to_unit with | Foot -> (Foot, mi_val *. 5280.) | Meter -> (Meter, (mi_val *. 0.3048) *. 5280.) | Mile -> (Mile, mi_val)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "Cannot have negative speed" else let (((d_unit,s_unit),val_),(do_unit,so_unit)) = ((from_unit, val_), to_unit) in let (_,(du,d_val),(su,s_val)) = (do_unit, (convert_dist (d_unit, val_) do_unit), (convert_time (s_unit, 1.) so_unit)) in ((du, su), (d_val /. s_val)) ;;
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 "Cannot have negative speeds" else (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 -> width *. width +. 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. <= (width *. width)) then check tl else false in check [t] ;;
let mode (l: 'a list) : 'a = match l with | [] -> failwith "Empty list" | _ -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | head::sublist -> aux sublist (head, if head = cur_el then cur_num + 1 else 1) (if head = cur_el && cur_num + 1 > max_num then (cur_el, cur_num + 1) else (max_el, max_num)) in let sorted_list = List.sort compare l in aux sorted_list (List.hd sorted_list, 0) (List.hd sorted_list, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "Not enough items" else let rec pair_list sublist new_list = if List.length sublist <= 1 then new_list else let rest = function | [] -> [] | _::s -> s in pair_list (rest sublist) (List.append new_list [List.hd sublist, List.hd (rest sublist)]) in mode (pair_list l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "Cannot have negative time" else match (from_unit, val_) with | (Second, s_val) -> (match to_unit with | Second -> (Second, s_val) | Hour -> (Hour, s_val /. 3600.)) | (Hour, h_val) -> (match to_unit with | Second -> (Second, h_val *. 3600.) | Hour -> (Hour, h_val)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "Cannot have negative distance" else match (from_unit, val_) with | (Foot, f_val) -> (match to_unit with | Foot -> (Foot, f_val) | Meter -> (Meter, f_val *. 0.3048) | Mile -> (Mile, f_val /. 5280.)) | (Meter, m_val) -> (match to_unit with | Foot -> (Foot, m_val /. 0.3048) | Meter -> (Meter, m_val) | Mile -> (Mile, (m_val /. 0.3048) /. 5280.)) | (Mile, mi_val) -> (match to_unit with | Foot -> (Foot, mi_val *. 5280.) | Meter -> (Meter, (mi_val *. 0.3048) *. 5280.) | Mile -> (Mile, mi_val)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "Cannot have negative speed" else let (((d_unit,s_unit),val_),(do_unit,so_unit)) = ((from_unit, val_), to_unit) in let (_,(du,d_val),(su,s_val)) = (do_unit, (convert_dist (d_unit, val_) do_unit), (convert_time (s_unit, 1.) so_unit)) in ((du, su), (d_val /. s_val)) ;;
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 "Cannot have negative speeds" else (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, _) :: tl -> sum_ tl (acc +. width *. width) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> ((check subtree) && (sum_ subtree 0. <= (width *. width))) && 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 with Second -> if to_unit == Hour then Hour, val_ /. 3600. else Second, val_ | Hour -> if to_unit == Second then Second, val_ *. 3600. else Hour, val_;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with Foot -> if to_unit == Mile then Mile, val_ /. 5280. else if to_unit == Meter then Meter, val_ *. 0.3048 else Foot, val_ | Mile -> if to_unit == Foot then Foot, val_ *. 5280. else if to_unit == Meter then Meter, val_ *. 1609.344 else Mile, val_ | Meter -> if to_unit == Foot then Foot, val_ /. 0.3048 else if to_unit == Mile then Mile, val_ /. 1609.344 else Meter, val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let f1 = fst from_unit in let f2 = snd from_unit in let t1 = fst to_unit in let t2 = snd to_unit in if (f1 != t1) && (f2 != t2) then let n1 = snd (convert_dist (f1, val_) t1) in (t1, t2), snd (convert_time (t2, n1) f2) else if (f1 == t1) && (f2 != t2) then (f1, t2), snd (convert_time (t2, val_) f2) else if (f1 != t1) && (f2 == t2) then (t1, f2), snd (convert_dist (f1, val_) t1) else from_unit, val_ ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let tUnit = fst time in let tVal = snd time in let sDist = fst speed_unit in let sTime = snd speed_unit in if tUnit != sTime then let newTime = snd (convert_time (time) sTime) in let totDist = newTime *. speed_val in sDist, totDist else let totDist = tVal *. speed_val in sDist, totDist ;; let rec passes_da_vinci t = notimplemented () ;;
let head l = match l with |[] -> failwith "Empty List"| h::t -> h let tail l= match l with | [] -> failwith "Empty List"| h::t -> t;;
let mode (l: 'a list) : 'a = let l = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h::t -> if (h = cur_el && cur_num+1 > max_num) then aux t (h, cur_num+1) (h,cur_num+1) else if h = cur_el then aux t (h,cur_num+1) (max_el,max_num) else aux t (h,1) (max_el,max_num) | _ -> failwith "Not a list" in aux l (head l,0) (head l, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List too small, space cowboy" else if l = [] then failwith "The list is empty, space cowboy" else let l1 = tail l in let l2 = List.rev (tail (List.rev l)) in mode(List.combine l2 l1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Hour,Second -> (Second,val_ *. 3600.) | Second,Hour -> (Hour, val_ /. 3600.) | Second, Second -> (Second, val_) | 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, Meter -> (Meter,val_ *. 0.3048) | Meter, Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Mile, Meter -> (Meter, val_ *. (5280. *. 0.3048)) | Meter, Foot -> (Foot, val_ /. 0.3048) | Foot, Mile -> (Mile, val_ /. 5280.) | Mile, Foot -> (Foot, val_ *. 5280.) | Mile, Mile -> (Mile, val_) | Foot, Foot -> (Foot, val_) | Meter, Meter -> (Meter, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit, to_unit with | (a,b), (x,y) when x = a && b = y -> ((x,y),val_) | (a,b), (x,y) when x = a -> ((x,y), snd (convert_time (y,val_) b)) | (a,b), (x,y) when y = b -> ((x,y), snd (convert_dist (a,val_) x)) | (a,b), (x,y) -> ((x,y),snd (convert_time (y,(snd (convert_dist (a,val_) x))) b)) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit, speed_val *. snd (convert_time (fst time, snd time) (snd speed_unit))) ;; let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch(w,st) :: tl -> sum_ tl (acc +. w**2.) let rec passes_da_vinci (t:tree) :bool = let rec check (l:tree list) :bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch(w, st) :: tl -> if not (check st && (sum_ st 0.) <= (w**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) = match (List.sort compare l) with | [] -> max_el | x :: xs -> if x = max_el then aux xs ( x, cur_num+1) (x, max_num+1) else ( if x <> cur_el then aux xs (x, 1) (max_el, max_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) ) ) in match List.sort compare l with |[] -> notimplemented () | x :: xs -> aux xs ( x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode ( let rec new_list l (cum: ('a * 'a) list) = match l with | x :: y :: xs -> new_list (y :: xs) (cum @ ([x,y])) | _ -> cum in match l with | x :: y :: xs -> new_list (y :: xs) ([x,y]) | _ -> notimplemented () ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if fst(from_unit, val_) = Second then (if to_unit = Second then (Second,val_) else (Hour, val_ /. 3600.) ) else (if to_unit = Second then (Second, val_ *. 3600.) else (Hour, val_) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match fst(from_unit, val_) with |Foot -> ( match to_unit with |Foot ->(Foot,val_) |Meter ->(Meter, val_ *. 0.3048) |Mile -> (Mile, val_ /. 5280.)) |Meter -> ( match to_unit with |Meter -> (Meter, val_) |Foot ->(Foot, val_ /.0.3048) |Mile ->(Mile, val_ /. 1609.344) ) |Mile -> ( match to_unit with |Mile -> (Mile, val_) |Foot -> (Foot, val_ *. 5280.) |Meter-> (Meter, val_ *.1609.344) ) ;;
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 converted_dist = convert_dist (fst(from_unit), val_) (fst(to_unit)) in let converted_time = convert_time (snd(from_unit), 1./.snd(converted_dist)) (snd(to_unit)) in (to_unit, 1./.snd(converted_time)) ) ;;
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_a = convert_speed (a) b_unit in ((b_unit), snd(converted_a ) +. b_val) ;;
let passes_da_vinci t = match t with |Leaf-> true |_-> let rec check l node acc = match l with | [] -> true | Leaf :: tl -> check tl node acc | Branch (width, subtree) :: tl -> if acc+.(width**2.) <= node ** 2. && (check subtree width 0.) then check tl node (acc+.(width**2.)) else false in let Branch (width,subtree) = t in check subtree width 0. ;;
let mode (l: 'a list) : 'a = if l = [] then failwith "Undefined input." else 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 | head :: rest -> if head = cur_el then aux (rest) (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux (rest) (head, 1) (cur_el, cur_num) else aux (rest) (head, 1) (max_el, max_num) in let sortedList = List.sort compare l in aux (sortedList) (List.hd sortedList,0) (List.hd sortedList,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Undefined input." else let rec aux (curr_index: int) (pair_list: ('a*'a) list) = let next_index = curr_index + 1 in if next_index >= List.length l then mode pair_list else aux next_index (List.append pair_list [(List.nth l curr_index, List.nth l next_index)]) in aux 0 [] ;;
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.) | _ -> (from_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_ /. 1609.344) | Mile, Foot -> (Foot, val_ *. 5280.) | Mile, Meter -> (Meter, val_ *. 1609.344) | _ -> (from_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let converted_distance = convert_dist ((fst from_unit), val_) (fst to_unit) in let converted_time = convert_time ((snd from_unit), 1.) (snd to_unit) in (to_unit, (snd converted_distance) /. (snd converted_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 ((fst speed_unit),speed_val *. converted_time) ;; let sum_of_squares (child_tree : tree list): float = let rec visit_child (subtree: tree list) (sum : float) = match subtree with | [] -> sum | Leaf :: rest -> visit_child rest sum | Branch(width, _) :: rest -> visit_child rest (sum +. width *. width) in visit_child child_tree 0. ;; let rec get_next_branch (child_tree : tree list) = match child_tree with | [] -> Branch(0., []) | Leaf :: rest -> get_next_branch rest | Branch(width, child) :: _ -> Branch (width, child) ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, []) -> 0. <= (width *. width) | Branch (width, children) -> if sum_of_squares children > (width *. width) then false else passes_da_vinci (get_next_branch children) ;;
let mode (l: 'a list) : 'a = match l with | [] -> failwith "List is empty!" | _ :: list -> 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 let up_num = cur_num+1 in if up_num > max_num then let max_el = cur_el in let max_num = up_num in aux tl (hd, up_num) (max_el,max_num) else aux tl (hd, up_num) (max_el,max_num) else aux tl (hd, 1) (max_el,max_num) in let sorted_list = List.sort compare (l) in let head = List.nth sorted_list 0 in let no_head = List.tl sorted_list in aux no_head (head,1) (head,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List does not contain enough elements!" else match l with | [] -> failwith "List is empty!" | _ :: list -> let dummy1 = l in let l1 = List.tl dummy1 in let dummy2 = l in let l2_rev = List.rev dummy2 in let l2_rev = List.tl l2_rev in let l2 = List.rev l2_rev in let final_list = List.combine l2 l1 in mode final_list ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let unit = fst(from_unit, val_) in let value = snd(from_unit,val_) in match to_unit with | Second -> if unit = Hour then let convert = value *.3600.0 in (Second, convert) else (unit, value) | Hour -> if unit = Second then let convert = value/.3600.0 in (Hour, convert) else (unit, value) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let unit = fst(from_unit, val_) in let value = snd(from_unit,val_) in if from_unit = to_unit then (unit, value) else match from_unit with | Foot -> if to_unit = Meter then let convert = value *. 0.3048 in (Meter, convert) else let convert = value /. 5280.0 in (Mile, convert) | Mile -> if to_unit = Foot then let convert = value *. 5280.0 in (Foot, convert) else let convert = value *. 1609.344 in (Meter, convert) | Meter -> if to_unit = Foot then let convert = value /. 0.3048 in (Foot, convert) else let convert = value /. 1609.344 in (Mile, convert) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let unit = fst(from_unit, val_) in let in_dist = fst(unit) in let in_time = snd (unit) in let out_dist = fst(to_unit) in let out_time = snd (to_unit) in let value = snd(from_unit,val_) in let convert1 = convert_time (in_time, 1.) out_time in let value1 = snd(convert1) in let convert2 = convert_dist(in_dist, 1.) out_dist in let value2 = snd(convert2) in let final_value = value*.value2/.value1 in (to_unit, final_value) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let out_unit = b_unit in let out_val = b_val in let in_unit = fst(a) in let in_val = snd(a) in let a_speed = convert_speed(in_unit, in_val) b_unit in let a_val = snd(a_speed) in let final_val = a_val+.out_val in (out_unit, final_val) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let speed_dist = fst(speed_unit) in let time_unit = fst(time) in let convert = convert_speed(speed_unit, speed_val) (speed_dist, time_unit) in;;
let passes_da_vinci t = let rec sum_ l acc = match l with |[] -> acc |Leaf :: tl -> sum_ tl acc |Branch(width, subtree) :: tl -> sum_ tl (acc +. width*.width) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch(width, subtree) :: tl-> if (sum_ subtree 0. > (width*.width) || not (check subtree)) then false else check tl in match t with | Leaf -> true | Branch(width, subtree) -> not (((sum_ subtree 0.) > (width *. width)) || (not (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 | x :: xs -> if x = cur_el then 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 let sorted = List.sort compare l in match sorted with | [] -> failwith "list is empty." | x :: xs -> aux xs (x,1) (x,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec bigram newList = match newList with |x1 :: x2 :: xs -> (x1,x2)::(bigram (x2::xs)) | _ -> [] in mode (bigram 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.0) | (Hour,Second) -> (Second, val_*.3600.0) | _ -> (from_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.0) | (Meter,Foot) -> (Foot, val_ /. 0.3048) | (Meter,Mile) -> (Mile, val_/.5280.0/.0.3048) | (Mile,Meter) -> (Meter, val_ *.5280.0*.0.3048) | (Mile,Foot) -> (Foot, val_*.5280.0) | _ -> (from_unit,val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dis_from,time_from) = from_unit in let (dis_to,time_to) = to_unit in let updated_dist_value = snd (convert_dist (dis_from, val_) dis_to) in let updated_time_value = snd (convert_time (time_to, updated_dist_value) time_from) in (to_unit, updated_time_value) ;;
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 = match t with | Leaf -> true | Branch (width,children) -> let rec sum_of_childs children = (match children with | [] -> 0. | x :: xs -> (if passes_da_vinci x then ( sum_of_childs xs +. (match x with | Leaf -> 0. | Branch (width2,children2) -> width2*.width2 )) else infinity)) in sum_of_childs children <= width*.width ;;
let error () = failwith "List is empty.";;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> (if x = cur_el then aux xs (cur_el, cur_num + 1) (max_el, max_num) else (if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num))) in match l with | [] -> notimplemented () | x :: xs -> aux (List.sort compare l) (x, 0) (x, 0) ;; let rec insert_at_end l i = match l with [] -> [i] | h :: t -> h :: (insert_at_end t i);;
let pair_mode (l: 'a list) : 'a * 'a = let rec aux old_l new_l prev = match old_l with | [] -> new_l | x :: xs -> aux xs (insert_at_end new_l (prev, x)) x in match l with | [] -> notimplemented () | x :: xs -> mode (aux xs [] x) ;;
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 if to_unit = Hour then (Hour, val_ /. 3600.) else notimplemented()) | Hour -> (if to_unit = Second then (Second, val_ *. 3600.) else if to_unit = Hour then (Hour, val_) else notimplemented()) | _ -> notimplemented();;
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 if to_unit = Mile then (to_unit, val_ /. 5280.) else notimplemented()) | Meter -> (if to_unit = Foot then (to_unit, val_ /. 0.3048) else if to_unit = Meter then (to_unit, val_) else if to_unit = Mile then (to_unit, val_ /. 0.3048 /. 5280.) else notimplemented()) | Mile -> (if to_unit = Foot then (to_unit, val_ *. 5280.) else if to_unit = Meter then (to_unit, val_ *. 5280. *. 0.3048) else if to_unit = Mile then (to_unit, val_) else notimplemented()) | _ -> notimplemented() ;;
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 ((fst distance, fst time), (snd distance) /. (snd time) ) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time_unit = fst time in let time_val = snd time in let traveled_time = convert_time (time_unit, time_val) (snd speed_unit) in (fst speed_unit, speed_val *. snd traveled_time) ;; let rec sum_of_squares list sum = match list with | [] -> sum | Leaf :: tl -> sum_of_squares tl sum | Branch (width, subtree) :: tl -> sum_of_squares tl (sum +. (width *. width)) let rec checker list = match list with | [] -> true | Leaf :: tl -> checker tl | Branch (width, subtree) :: tl -> ( let branch_width_square = width *. width in let child_sum_of_square = sum_of_squares subtree 0. in (if child_sum_of_square > branch_width_square or (not (checker subtree)) then false else checker tl) ) let rec passes_da_vinci t = checker [t] ;;
let mode (l: 'a list) : 'a = if l == [] then failwith "Invalid input." else let l = List.sort compare (l) in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else if (cur_num = max_num) && (compare cur_el max_el < 0) then cur_el else max_el | hd :: restOfList -> if compare hd cur_el = 0 then if cur_num + 1 > max_num then aux restOfList (hd, cur_num + 1)(hd, cur_num + 1) else if cur_num + 1 = max_num then if compare cur_el max_el <= 0 then aux restOfList (hd, cur_num + 1)(hd, cur_num + 1) else aux restOfList (hd, cur_num + 1)(max_el, max_num) else aux restOfList(hd, cur_num + 1) (max_el, max_num) else aux restOfList(hd, 1)(max_el, max_num) in aux l (List.hd(l),0) (List.hd(l),0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "The list is too small and does not contain any pairs!" else let l1 = List.tl (l) in let l2 = List.rev (List.tl (List.rev(l))) in let allTuples = List.combine l2 l1 in mode (allTuples) ;;
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.) | Second, Second -> (Second, val_) | 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 = if from_unit = to_unit then ((fst(from_unit), snd(to_unit)) , val_) else let inputDistUnit = fst(from_unit) in let inputTimeUnit = snd(from_unit) in let outputDistUnit = fst(to_unit) in let outputTimeUnit = snd(to_unit) in let outputDistFactor = snd(convert_dist(inputDistUnit, 1.) outputDistUnit) in let outputTimeFactor = snd(convert_time(inputTimeUnit, 1.) outputTimeUnit) in let outputVal = (val_ *. outputDistFactor /. outputTimeFactor) in ((outputDistUnit, outputTimeUnit), outputVal) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let aUnit = fst(a) in let aNumber = snd(a) in let aInBUnits = convert_speed(aUnit, aNumber) (b_unit) in let aPlusBNumber = snd(aInBUnits) +. b_val in (b_unit, aPlusBNumber) ;;
let passes_da_vinci t = let acc = 0. in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && ((squaredSumSubtrees acc subtree) <= (width *. width))) then check tl else false in check [t] ;;
let mode (l: 'a list) : 'a = if l == [] then failwith "Invalid input." else let l = List.sort compare (l) in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else if (cur_num = max_num) && (compare cur_el max_el < 0) then cur_el else max_el | hd :: restOfList -> if compare hd cur_el = 0 then if cur_num + 1 > max_num then aux restOfList (hd, cur_num + 1)(hd, cur_num + 1) else if cur_num + 1 = max_num then if compare cur_el max_el <= 0 then aux restOfList (hd, cur_num + 1)(hd, cur_num + 1) else aux restOfList (hd, cur_num + 1)(max_el, max_num) else aux restOfList(hd, cur_num + 1) (max_el, max_num) else aux restOfList(hd, 1)(max_el, max_num) in aux l (List.hd(l),0) (List.hd(l),0) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "The list is too small and does not contain any pairs!" else let l1 = List.tl (l) in let l2 = List.rev (List.tl (List.rev(l))) in let allTuples = List.combine l2 l1 in mode (allTuples) ;;
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.) | Second, Second -> (Second, val_) | 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 = if from_unit = to_unit then ((fst(from_unit), snd(to_unit)) , val_) else let inputDistUnit = fst(from_unit) in let inputTimeUnit = snd(from_unit) in let outputDistUnit = fst(to_unit) in let outputTimeUnit = snd(to_unit) in let outputDistFactor = snd(convert_dist(inputDistUnit, 1.) outputDistUnit) in let outputTimeFactor = snd(convert_time(inputTimeUnit, 1.) outputTimeUnit) in let outputVal = (val_ *. outputDistFactor /. outputTimeFactor) in ((outputDistUnit, outputTimeUnit), outputVal) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let speedUnitDist = fst(speed_unit) in let speedUnitTime = snd(speed_unit) in let givenTimeUnit = fst(time) in let givenTimeNumber = snd(time) in let givenTimeInSpeedTimeUnits = convert_time(givenTimeUnit, givenTimeNumber) speedUnitTime in (speedUnitDist, (speed_val *. snd(givenTimeInSpeedTimeUnits))) ;; let rec passes_da_vinci t = notimplemented () ;;
let mode (l: 'a list) : 'a = if l == [] then failwith "Invalid input." else let l = List.sort compare (l) in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else if (cur_num = max_num) && (compare cur_el max_el < 0) then cur_el else max_el | hd :: restOfList -> if compare hd cur_el = 0 then if cur_num + 1 > max_num then aux restOfList (hd, cur_num + 1)(hd, cur_num + 1) else if cur_num + 1 = max_num then if compare cur_el max_el <= 0 then aux restOfList (hd, cur_num + 1)(hd, cur_num + 1) else aux restOfList (hd, cur_num + 1)(max_el, max_num) else aux restOfList(hd, cur_num + 1) (max_el, max_num) else aux restOfList(hd, 1)(max_el, max_num) in aux l (List.hd(l),0) (List.hd(l),0) ;;