text
stringlengths
0
601k
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (l: 'a list) (tar :('a * 'a) list) (pre : 'a) = match l with | [] -> tar | (h :: t) -> aux t ((pre, h) :: tar) h in let newlist: ('a * 'a) list = [] in match l with | [] -> failwith "Invalid input. The list is empty!" | [x] -> failwith "Invalid input. The list only has one element!" | h :: t -> mode (aux t newlist h) ;;
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.0)) | Hour -> (match to_unit with | Second -> (Second, (val_ *. 3600.0)) | Hour -> (Hour, val_)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.0)) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048 ) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /. 0.3048 /. 5280.0)) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.0) | Meter -> (Meter, val_ *. 0.3048 *. 5280.0) | Mile -> (Mile, val_)) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (d, t) : (float * float) = (snd (convert_dist (fst from_unit, val_) Meter), snd (convert_time (snd from_unit, 1.0) Second)) in let dd = d /. t in match to_unit with | (Foot, Second) -> ((Foot, Second), dd /. 0.3048) | (Foot, Hour) -> ((Foot, Hour), dd /. 0.3048 *. 3600.0) | (Meter, Second) -> ((Meter, Second), dd) | (Meter, Hour) -> ((Meter, Hour), dd *. 3600.0) | (Mile, Second) -> ((Meter, Second), dd /. 0.3048 /. 5280.0) | (Mile, Hour) -> ((Meter, Hour), dd /. 0.3048 /. 5280.0 *. 3600.0) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let sa = (snd (convert_speed a (fst b_unit, snd b_unit))) in (b_unit, sa +. b_val) ;;
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 -> if not ((check subtree) && ((sum_ subtree 0.0) <= (width *. width))) then false else check tl in match [t] with | [] -> failwith "Invalid input. Empty tree!" | _ -> check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el then aux xs (cur_el, cur_num+1) (max_el, max_num) else aux xs (x, 1) (if cur_num > max_num then (cur_el, cur_num) else (max_el, max_num)) in match List.sort compare l with | [] -> failwith "Incorrect input" | x::xs -> aux xs (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec gv_pair_lst l res = match l with | [] -> res | _::[] -> res | x1::x2::[] -> ((x1, x2)::res) | x1::x2::xs -> gv_pair_lst (x2::xs) ((x1, x2)::res) in match gv_pair_lst l [] with | [] -> failwith "Incorrect input" | x::xs -> mode (List.sort compare (x::xs)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (to_unit, val_) else (match from_unit with | Second -> (to_unit, val_ /. 3600.) | Hour -> (to_unit, val_ *. 3600.)) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (to_unit, val_) else (match from_unit with | Foot -> if to_unit = Meter then (to_unit, val_ *. 0.3048) else (to_unit, val_ /. 5280.) | Meter -> if to_unit = Foot then (to_unit, val_ /. 0.3048) else (to_unit, val_ /. 0.3048 /. 5280.) | Mile -> 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 = let (fd, ft) = from_unit in let (td, tt) = to_unit in let (_, cvd) = convert_dist (fd, 1.) (td) in let (_, cvt) = convert_time (ft, 1.) (tt) in (to_unit, val_ *. cvd /. cvt) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (to_unit_dist, to_unit_time) = speed_unit in let (_, cvt) = convert_time time to_unit_time in (to_unit_dist, speed_val *. cvt) ;; let rec passes_da_vinci t = let rec sum_sqrs l acc = match l with | [] -> acc | x::xs -> if x = Leaf then sum_sqrs xs acc else let Branch (w, b_lst) = x in sum_sqrs xs (acc+.(w*.w)) in if t = Leaf then true else let Branch (w, b_lst) = t in List.for_all passes_da_vinci b_lst && ((sum_sqrs b_lst 0.) <= (w *. w)) ;;
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 x :: y -> if x= cur_el then (if max_el = cur_el then aux y (cur_el,cur_num+1) (max_el,max_num+1) else if (cur_num+1)>max_num then aux y (cur_el,cur_num+1) (cur_el,cur_num+1) else aux y (cur_el,cur_num+1) (max_el,max_num)) else (if x>cur_el && cur_num>max_num then aux y (x,1) (cur_el,cur_num) else aux y (x,1) (max_el,max_num)) | [] -> max_el in let hl = List.sort compare(l) in aux hl (List.nth hl 0,0) (List.nth hl 0,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec aux l com= match l with | [] ->com | _::[] -> com | a::b::c -> aux (b::c) ((a,b)::com) in mode (aux l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,val_),to_unit with |(Hour,u), Second -> (Second,u *.3600.) |(Second,u), Hour -> (Hour, u /.3600.) |_ -> (from_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,val_),to_unit with |(Foot,u),Mile -> (Mile, u /. 5280.) |(Mile,u),Foot -> (Foot, u *. 5280.) |(Foot,u),Meter -> (Meter,u *. 0.3048) |(Meter,u),Foot->(Foot, u /. 0.3048) |(Meter,u),Mile -> (Mile, (u /. 0.3048) /. 5280.) |(Mile,u),Meter -> (Meter, u*. 5280. *. 0.3048) |_->(from_unit,val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let i = snd (convert_dist ((fst from_unit),val_) (fst to_unit)) in (to_unit, 1./.snd(convert_time (snd from_unit,1./.i) (snd to_unit))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = ((fst speed_unit), (snd (convert_speed (speed_unit,speed_val) (fst speed_unit,fst time)))*.(snd time)) ;; let sqr x = x *. x let rec subtree_ite_sum sub sum = match sub with | Branch(a,b)::c -> subtree_ite_sum c (sum+.(sqr a)) | Leaf ::c -> subtree_ite_sum c sum | [] -> sum let rec passes_da_vinci t = let check x = x in match t with |Leaf -> true |Branch(a,b) -> if (sqr a) >= (subtree_ite_sum b 0.) then List.for_all check (List.map passes_da_vinci b) else false ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if (compare cur_num max_num) = 1 then (cur_el, cur_num) else (max_el, max_num) | x :: l -> if (compare x cur_el) = 0 then aux l (cur_el, cur_num + 1) (max_el, max_num) else if (compare x cur_el) = 1 && (compare cur_num max_num) = 1 then aux l (x, 1) (cur_el, cur_num) else aux l (x, 1) (max_el, max_num) in let sorted_l = List.sort compare l in let (max_el, max_num) = match sorted_l with | [] -> failwith "Undefined input" | x :: l -> aux l (x, 1) (x, 1) in max_el ;;
let pair_mode (l: 'a list): 'a * 'a = let rec pair_up l acc = match l with | [] -> acc | x :: [] -> acc | x :: y :: l -> pair_up (y :: l) ((x, y) :: acc) in let bigrams = match l with | [] -> failwith "Wrong input" | x :: [] -> failwith "Wrong input" | _ -> pair_up l [] in mode bigrams ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | Second, Hour -> (Hour, val_ /. 3600.) | Second, Second -> (Second, val_) | Hour, 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, 0.3048 *. val_) | Foot, Mile -> (Mile, val_ /. 5280.) | Meter, Foot -> (Foot, val_ /. 0.3048) | Meter, Meter -> (Meter, val_) | Meter, Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Mile, Foot -> (Foot, 5280. *. val_) | Mile, Meter -> (Meter, 5280. *. 0.3048 *. val_) | Mile, Mile -> (Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_dist, from_time) = from_unit in let (to_dist, to_time) = to_unit in let _, conv_time = convert_time (to_time, val_) from_time in let _, conv_dist = convert_dist (from_dist, conv_time) to_dist in (to_dist, to_time), conv_dist ;;
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 (_, conv_a_val) = convert_speed (a_unit, a_val) b_unit in b_unit, (conv_a_val +. b_val) ;;
let passes_da_vinci (t: tree): bool = let rec square_branches (l: tree list) (acc: float): float = match l with | [] -> acc | Leaf :: tl -> square_branches tl acc | Branch (width, subtree) :: tl -> square_branches tl (acc +. (width *. width)) in let rec check (l: tree list): bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> let is_valid = (square_branches subtree 0.) <= (width *. width) in if not is_valid || not (check subtree) 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) : 'a = match l with | [] -> if cur_num > max_num then cur_el else max_el | head::tail -> if head = cur_el then aux tail (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tail (head, 1) (cur_el, cur_num) else aux tail (head, 1) (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 = let modif_list = List.rev (List.tl (List.rev l)) in let pairs = List.combine modif_list (List.tl l) in mode pairs ;; let foot_in_meter = 0.3048 ;; let mile_in_meter = 1609.344 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let common_time = match from_unit with Hour -> val_ *. 3600. | Second -> val_ in ( to_unit, match to_unit with Hour -> common_time /. 3600. | Second -> common_time ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let common_dist = match from_unit with Foot -> val_ *. foot_in_meter | Mile -> val_ *. mile_in_meter | Meter-> val_ in ( to_unit, match to_unit with Foot -> common_dist *. (1. /. foot_in_meter) | Mile -> common_dist *. (1. /. mile_in_meter) | Meter -> common_dist ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist_f, time_f) = from_unit in let (dist_t, time_t) = to_unit in let (_, current_val) = (convert_time (time_t, val_) time_f) in let (_, current_val) = convert_dist (dist_f, current_val) dist_t in (to_unit, current_val) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_u, time_u) = speed_unit in let (_, compatible_time) = convert_time time time_u in (dist_u, compatible_time *. speed_val) ;; let rec branch_sum_of_squares (l: tree list) acc : float = match l with | [] -> acc | hd :: tl -> let width = match hd with | Leaf -> 0. | Branch (_val, _) -> _val in branch_sum_of_squares tl (acc +. (width**2.)) let rec passes_da_vinci t: bool = let rec aux_check_prop (l: tree list) : bool = match l with | [] -> true | hd :: tl -> (passes_da_vinci hd) && (aux_check_prop tl) in match t with | Leaf -> true | Branch (width, children) -> if (width**2.) >= (branch_sum_of_squares children 0.) then aux_check_prop children else false ;;
let mode (l: 'a list) : 'a = let is e = (!=) e in let rec aux l (cur_el, cur_num) (max_el, max_num) = if l == [] then if cur_num <= max_num then max_el else cur_el else if cur_num <= max_num then let x = match l with | a::_ -> a in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (max_el, max_num) else let x = match l with | a::_ -> a in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (cur_el, cur_num) in let l = List.sort compare l in let x = match l with | a::_ -> a in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (x,n) ;; let mode' (l: 'a list) : 'a = let is (a1,a2) (b1,b2) = (a1 != b1) || (a2 != b2) in let rec aux l (cur_el, cur_num) (max_el, max_num) = if l == [] then if cur_num > max_num then cur_el else max_el else if cur_num <= max_num then let x = match l with | (a,b) ::_ -> (a,b) in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (max_el, max_num) else let x = match l with | (a,b) ::_ -> (a,b) in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (cur_el, cur_num) in let l = List.sort compare l in let x = match l with | (a,b) ::_ -> (a,b) in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (x,n) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec mk_pair_ls (l: 'a list) = if List.length l == 2 then match l with | a::(b::[]) -> [(a,b)] else let pair = match l with | a :: b :: _ -> [(a,b)] in let poped_ls = match l with | a :: xs -> xs in pair @ mk_pair_ls poped_ls in let l = mk_pair_ls l in mode' l ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_), to_unit with | (Hour, x), Second -> (Second, x *. 3600.) | (Second,x), Hour -> (Hour, x /. 3600.) | (a,x), b -> (a, x) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_), to_unit with | (Mile, x), Foot -> (Foot, x *. 5280.) | (Foot,x), Mile -> (Mile, x /. 5280.) | (Foot,x), Meter -> (Meter, x *. 0.3048) | (Meter,x), Foot -> (Foot, x /. 0.3048) | (Mile, x), Meter -> (Meter, x *. 5280. *. 0.3048) | (Meter, x), Mile -> (Mile, x /. 5280. /. 0.3048) | (a,x), b -> (a,x) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let distance:'a value = match (from_unit,val_), to_unit with | ((a, b),e), (c, d) -> convert_dist (a,e) c in let speed:'b value = match (from_unit,val_), to_unit with | ((a, b),e), (c, d) -> convert_time (b,1.) d in let (distance, speed) = match distance, speed with | ((a,b),(c,d)) -> (b,d) in (to_unit, distance /. speed) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let added_speed = convert_speed a b_unit in let added_speed = match added_speed with | (a,b) -> b in (b_unit, added_speed +. b_val) ;;
let passes_da_vinci t = traverse t ;;
let mode (l: 'a list) : 'a = let is e = (!=) e in let rec aux l (cur_el, cur_num) (max_el, max_num) = if l == [] then if cur_num <= max_num then max_el else cur_el else if cur_num <= max_num then let x = List.hd l in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (max_el, max_num) else let x = List.hd l in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (cur_el, cur_num) in let l = List.sort compare l in let x = List.hd l in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (x,n) ;; let mode' (l: 'a list) : 'a = let is (a1,a2) (b1,b2) = (a1 != b1) || (a2 != b2) in let rec aux l (cur_el, cur_num) (max_el, max_num) = if l == [] then if cur_num > max_num then cur_el else max_el else if cur_num <= max_num then let x = List.hd l in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (max_el, max_num) else let x = List.hd l in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (cur_el, cur_num) in let l = List.sort compare l in let x = List.hd l in let n = List.length l - List.length (List.filter (is x) l) in aux (List.filter (is x) l) (x , n) (x,n) ;;
let pair_mode l = let rec mk_pair_ls (l: 'a list) = if List.length l == 2 then [(List.hd l,List.hd (List.tl l))] else let pair = [(List.hd l,List.hd (List.tl l))] in let poped_ls = List.tl l in pair @ (mk_pair_ls poped_ls) in let l = mk_pair_ls l in mode' l ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_), to_unit with | (Hour, x), Second -> (Second, x *. 3600.) | (Second,x), Hour -> (Hour, x /. 3600.) | (a,x), _ -> (a, x) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_), to_unit with | (Mile, x), Foot -> (Foot, x *. 5280.) | (Foot,x), Mile -> (Mile, x /. 5280.) | (Foot,x), Meter -> (Meter, x *. 0.3048) | (Meter,x), Foot -> (Foot, x /. 0.3048) | (Mile, x), Meter -> (Meter, x *. 5280. *. 0.3048) | (Meter, x), Mile -> (Mile, x /. 5280. /. 0.3048) | (a,x), _ -> (a,x) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (((a,_),e),(c,_)) = ((from_unit, val_), to_unit) in let distance:'a value = convert_dist (a, e) c in let (((_,b),_),(_,d)) = ((from_unit, val_), to_unit) in let speed:'b value = convert_time (b,1.) d in let ((_,b),(_,d)) = (distance, speed) in (to_unit, b /. d) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let added_speed = convert_speed a b_unit in let (_,b) = added_speed in (b_unit, b +. b_val) ;;
let passes_da_vinci t = traverse t ;;
let endhelper (l: 'a list) = match l with |[] -> [] |first::xs -> xs let starthelper (l: 'a list) = match l with |[] -> [] |first::xs -> first;;
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::tail -> if hd = cur_el then aux tail (hd, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux tail (hd,1) (cur_el, cur_num) else aux tail (hd, 1) (max_el, max_num) in match l with |[] -> failwith "empty list error" |hd::tail -> aux (List.sort compare l) (hd, 0) (hd, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let l1 = l in let l2 = l in match l with |[] -> failwith "empty bigram list erorr" |hd::tail -> let fst1::rest1 = l1 in let fst2::rest2 = (List.rev l2) in mode (List.combine (List.rev rest2) rest1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match fst(from_unit, val_) with |Second -> if to_unit = Second then (Second, val_) else if to_unit = Hour then (Hour, val_ /. 3600.) else failwith "Wrong unit conversion request" |Hour -> if to_unit = Hour then (Hour, val_) else if to_unit = Second then (Second, val_ *. 3600.) else failwith "Wrong unit conversion request" ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match fst(from_unit, val_) with |Foot -> if to_unit = Foot then (Foot, val_) else if to_unit = Meter then (Meter, val_ *. 0.3048) else if to_unit = Mile then (Mile, val_ /. 5280.) else failwith "Wrong unit conversion request" |Meter -> if to_unit = Foot then (Foot, val_ /. 0.3048) else if to_unit = Meter then (Meter, val_) else if to_unit = Mile then (Mile, val_ /. 1609.344) else failwith "Wrong unit conversion request" |Mile -> if to_unit = Foot then (Foot, val_ *. 5280.) else if to_unit = Meter then (Meter, val_ *. 1609.344) else if to_unit = Mile then (Mile, val_) else failwith "Wrong unit conversion request" ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match fst(from_unit) with |Foot -> (match fst(to_unit) with |Foot -> if snd(from_unit) = snd(to_unit) then ((Foot, snd(from_unit)), val_) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Foot, Hour), val_ /. 3600.) else ((Foot, Second), val_ *. 3600.) |Meter -> if snd(from_unit) = snd(to_unit) then ((Meter, snd(from_unit)), snd(convert_dist (Foot, val_) Meter)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Meter, Hour), snd(convert_dist (Foot, val_) Meter) /. 3600.) else ((Meter, Second), snd(convert_dist (Foot, val_) Meter) *. 3600.) |Mile -> if snd(from_unit) = snd(to_unit) then ((Mile, snd(from_unit)), snd(convert_dist (Foot, val_) Mile)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Mile, Hour), snd(convert_dist (Foot, val_) Mile) /. 3600.) else ((Mile, Second), snd(convert_dist (Foot, val_) Mile) *. 3600.)) |Meter -> (match fst(to_unit) with |Foot -> if snd(from_unit) = snd(to_unit) then ((Foot, snd(from_unit)), snd(convert_dist (Meter, val_) Foot)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Foot, Hour), snd(convert_dist (Meter, val_) Foot) /. 3600.) else ((Foot, Second), snd(convert_dist (Meter, val_) Foot) *. 3600.) |Meter -> if snd(from_unit) = snd(to_unit) then ((Meter, snd(from_unit)), snd(convert_dist (Meter, val_) Meter)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Meter, Hour), snd(convert_dist (Meter, val_) Meter) /. 3600.) else ((Meter, Second), snd(convert_dist (Meter, val_) Meter) *. 3600.) |Mile -> if snd(from_unit) = snd(to_unit) then ((Mile, snd(from_unit)), snd(convert_dist (Meter, val_) Mile)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Mile, Hour), snd(convert_dist (Meter, val_) Mile) /. 3600.) else ((Mile, Second), snd(convert_dist (Meter, val_) Mile) *. 3600.)) |Mile -> (match fst(to_unit) with |Foot -> if snd(from_unit) = snd(to_unit) then ((Foot, snd(from_unit)), snd(convert_dist (Mile, val_) Foot)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Foot, Hour), snd(convert_dist (Mile, val_) Foot) /. 3600.) else ((Foot, Second), snd(convert_dist (Mile, val_) Foot) *. 3600.) |Meter -> if snd(from_unit) = snd(to_unit) then ((Meter, snd(from_unit)), snd(convert_dist (Mile, val_) Meter)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Meter, Hour), snd(convert_dist (Mile, val_) Meter) /. 3600.) else ((Meter, Second), snd(convert_dist (Mile, val_) Meter) *. 3600.) |Mile -> if snd(from_unit) = snd(to_unit) then ((Mile, snd(from_unit)), snd(convert_dist (Mile, val_) Mile)) else if snd(from_unit) = Hour && snd(from_unit) != snd(to_unit) then ((Mile, Hour), snd(convert_dist (Mile, val_) Mile) /. 3600.) else ((Mile, Second), snd(convert_dist (Mile, val_) Mile) *. 3600.)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. ( 1. /. (snd(convert_time (snd(fst(a)),( 1. /. (snd(convert_dist (fst(fst(a)), snd(a)) (fst(b_unit))))))(snd(b_unit)))))) ;;
let passes_da_vinci t = let rec check l = match l with |[] -> true |Leaf::tl -> true |Branch (width, subtree)::tl -> if not((sum_ subtree 0.) <= (width**(2.)) && (check subtree)) then false else check tl in match [t] with |[] -> failwith "empty tree" |tree -> 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 ( cur_el, cur_num + 1) ( max_el, max_num) else if cur_num > max_num then aux xs (x,1) ( cur_el, cur_num) else aux xs (x, 1) ( max_el, max_num) in match List.sort compare l with | [] -> notimplemented () | x::xs -> aux xs (x,1) (x, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l2 = List.rev l in let l3 = List. tl l2 in let l4 = List.rev l3 in let l5 = List.combine l4 l1 in match l with | [] -> failwith "empty" | _ -> mode l5 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with |Second -> Second, val_ | Hour -> Hour,val_ /.3600.) | Hour -> (match to_unit with |Second -> Second,val_ *.3600. | Hour -> Hour,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then to_unit,val_ else match from_unit with | Meter -> (match to_unit with | Foot -> Meter,val_ /.0.3048 | Mile -> Mile,val_ /.1609.344) | Foot -> (match to_unit with | Meter -> Meter,val_ *.0.3048 | Mile -> Mile,val_ /.5280. ) | Mile -> (match to_unit with | Meter -> Meter,val_ *.1609.344 | Foot -> Foot,val_*.5280.) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let for_time = snd(from_unit)in let for_dist = fst(from_unit)in let to_time = snd(to_unit)in let to_dist = fst(to_unit)in let val2 = snd(convert_dist (for_dist, val_) to_dist) in let val3 = snd(convert_time (to_time, val2) for_time) in if from_unit = to_unit then to_unit,val_ else to_unit, val3 ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let t_unit = fst(time) in let t_value = snd(time) in let st_unit = snd(speed_unit) in let st_dist = fst(speed_unit) in let conv_time = snd( convert_time (t_unit, t_value) st_unit)in st_dist, speed_val *. conv_time ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch( _ , _) -> false ;;
let invalid_arg () = failwith "Invalid Argument";;
let mode (l: 'a list) : 'a = let g = (List.sort compare l) in if (List.length g) = 0 then invalid_arg () else if (List.length g) = 1 then (List.hd g) else let rec aux g ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match g with |[] -> if (cur_num > max_num) then (cur_el) else (max_el) |g::xs -> if (g = cur_el) then aux xs (cur_el, cur_num +1) (max_el, max_num) else if (cur_num > max_num) then aux xs (g, 1) (cur_el, cur_num) else aux xs (g,1) ( max_el, max_num) in let (_::xs) = g in aux xs ((List.hd g), 1) ((List.hd g), 1);; let tail l = match l with | [] -> [] | h::t -> t ;; let rtail l = let r = List.rev l in (List.rev (tail r));;
let pair_mode (l: 'a list) : 'a * 'a = match l with |[] -> invalid_arg () |l -> mode(List.combine (rtail l) (tail l));;
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.) | (Hour, Hour) -> (Hour, val_ *. 1.) | (Second, Second) -> (Second, val_ *. 1.) ;;
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.) | (Foot, Foot) -> (Foot, val_ *. 1.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. (5280. *. 0.3048)) | (Meter, Meter) -> (Meter, val_ *. 1.) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. (5280. *. 0.3048)) | (Mile, Mile) -> (Mile, val_ *. 1.) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (x,y) = from_unit in let (w,z) = to_unit in let (c,c') = convert_dist (x,val_) w in let (g, g') = convert_time (y,val_) z in let b = (c' /. g')*.(val_) in ((w,z), b) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_unit,a_val) = convert_speed a b_unit in let sum_val = a_val +. b_val in (b_unit, sum_val) ;;
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ( square width >= sum_ subtree 0. && check subtree) then check tl else false in check [t];;
let mode (l: 'a list) : 'a = if List.length l < 1 then failwith "Input list must contain at least 2 elements" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int):'a = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd::tl-> if (compare hd cur_el)!=0 then if cur_num > max_num then aux tl (hd,1) (cur_el,cur_num) else aux tl (hd,1) (max_el,max_num) else aux tl (cur_el,cur_num+1) (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 "Input list must contain at least 2 elements" else let rec turn_in_pair (l: 'a list)(prev:'a)(tuple_list:('a * 'a) list): (('a*'a) list)= match l with | [] -> tuple_list | hd::tl-> turn_in_pair (tl) (hd) (List.cons (prev,hd) tuple_list) in mode (List.sort compare ((turn_in_pair(List.tl l)(List.hd l)([])))) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second->( match to_unit with |Second->(Second,val_) |Hour-> (Hour,val_/. 3600.) ) |Hour->( match to_unit with |Hour->(Hour,val_) |Second-> (Second,val_ *. 3600.) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot->( match to_unit with |Foot->(Foot,val_) |Meter->(Meter,val_*. 0.3048) |Mile->(Mile,val_/.5280.) ) |Meter->( match to_unit with |Foot->(Foot,val_/. 0.3048) |Meter->(Meter,val_) |Mile->(Mile,(val_/.0.3048) /. 5280.) ) |Mile->( match to_unit with |Foot->(Foot,val_*. 5280.) |Meter->(Meter,val_ *. 5280. *. 0.3048) |Mile->(Mile,val_) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let trans_value= (snd (convert_dist((fst from_unit),val_) (fst to_unit))) /. (snd (convert_time((snd from_unit),1.) (snd to_unit))) in (to_unit,trans_value) ;;
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:tree) : bool = let rec sum_of_square (t: tree list) (count:float):float= match t with |[]->count |hd::tl-> match hd with |Leaf->sum_of_square (tl) (count) |Branch(aFloat,blist)->sum_of_square (tl) (count +. (aFloat ** 2.)) in let rec traversal_tree_list (t:tree list):bool= match t with |[]->true |hd::tl-> match hd with |Leaf->true |Branch(aFloat,childList)->( if ((aFloat)**2.) >= ((sum_of_square (childList) (0.))) then ((traversal_tree_list (childList)) && (traversal_tree_list (tl)) ) else false ) in traversal_tree_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 |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 (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),1) ;; let delfirst =function |[] -> [] |_::xs -> xs;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <2 then notimplemented () else let l1=delfirst l in let l2=List.rev (delfirst (List.rev l) )in mode (List.combine l2 l1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit=to_unit then (to_unit, val_) else if from_unit=Second then (Hour , val_ /. 3600.0) else (Second, val_ *. 3600.0) ;;
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 ) |Foot, Foot -> (Foot, val_) |Meter,Foot -> (Foot, val_/. 0.3048 ) |Meter,Mile -> (Mile, val_/. 1609.3440 ) |Meter,Meter -> (Meter, val_) |Mile, Foot -> (Foot, val_*. 5280.0 ) |Mile, Meter -> (Meter, val_*. 1609.3440 ) |Mile,Mile -> (Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dis=snd(convert_dist ( fst(from_unit), val_) (fst(to_unit))) in let time = snd( convert_time (snd(from_unit), 1.) (snd(to_unit))) in ( (fst(to_unit),snd(to_unit) ) , (dis/.time)) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time = convert_time time (snd(speed_unit)) in ( fst(speed_unit) , snd(time)*. 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) = match l with | [] -> max_el | x::xs -> if x <> cur_el then aux xs (x,1) (max_el, max_num) else 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); in let sorted_list = List.sort compare l in match sorted_list with | [] -> failwith "Empty list" | x::xs -> aux xs (x,1) (x,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let shifted_list = List.hd (List.rev l) in let l2 = shifted_list :: (List.rev (List.tl (List.rev l)))in let combined_list = List.combine l2 l in match combined_list with | [] -> failwith "Empty list" | x::xs -> mode xs ;;
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.) | (Hour, Hour) -> (Hour, val_) | (Second, Second) -> (Second, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | (Foot, Meter) -> (Meter,val_ *. 0.3048) | (Foot, Mile) ->(Mile,val_ /. 5280.) | (Foot, Foot) -> (Foot, val_) | (Mile, Mile) ->(Mile,val_) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. 1609.344) | (Meter, Meter) -> (Meter, val_) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. 1609.344) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((a,b),(c,d)) = (from_unit,to_unit) in let dist, val_ = convert_dist (a,val_) c in let time, val_ = convert_time (d,val_) b in ((dist,time), val_) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((a_dist, a_time), a_val) = convert_speed a b_unit in (b_unit,a_val+.b_val) ;;
let passes_da_vinci t = notimplemented () ;;
let string_explode (s : string) : char list = tabulate (String.get s) (String.length s) ;;
let string_implode (l : char list) : string = let s_l = List.map (Char.escaped) (l) in List.fold_left (^) "" s_l ;;
let evens (max : int) : int list = unfold (fun b -> (b, b+2)) ((<=) max) 0 ;;
let fib (max : int) : int list = unfold (fun (b0, b1) -> (b0, (b1,b0+b1))) (fun (b0,b1) -> max <= b0) (1,1) ;;
let pascal (max : int) : int list list = unfold ( fun b -> (b, (List.map2 (+) (0::b) (b@[0]) ) ) ) (fun b -> max < (List.length b)) [1] ;;
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) = List.filter (fun (Cupcake (_,_,_,cI):cupcake) -> (List.for_all (fun y -> (not (List.exists (fun x -> x==y) cI))) allergens) ) cupcakes ;;
let get_str (f : string-> int -> char) (s : string) = f s;;
let string_explode (s : string) : char list = tabulate (get_str String.get s) (String.length s);;
let string_implode (l : char list) : string = List.fold_left (^) "" (List.map (Char.escaped) l);;
let evens (max : int) : int list = unfold (fun b -> (b, b+2)) (fun b -> max <= b) 0;;
let fib (max : int) : int list = unfold (fun (prev, this) -> (this, (this, prev + this))) (fun b -> let (a,c) = b in max <= c) (0,1) let generate_next l = let l2 = List.map (fun x -> x) l in let l = List.tl l in let l2 = List.rev (List.tl (List.rev l2)) in let tl = List.combine l l2 in let comb = List.map (fun x -> let (y,z) = x in y + z) tl in let ret = List.append comb [1] in let ret = List.append [1] ret in ret ;;
let pascal (max : int) : int list list = unfold (fun l -> (l, generate_next l )) (fun l -> max < List.length l) [1] let get l1 l2 n = let e1 = List.nth l1 n in let e2 = List.nth l2 n in ((e1,e2), n+1);;
let allergy_free (allergens : ingredient list) (cupcakes : cupcake list) : cupcake list = let filter als cups acc = let ret = acc @ (List.filter (fun cup -> (let rec filter cup als = let Cupcake (p,q,c,l) = cup in match als with | hd::tl -> if (List.exists (fun i -> i = hd) l) then false else filter cup tl | [] -> true in filter cup als)) cups) in if (List.for_all (fun cup -> (let rec filter cup als = let Cupcake (p,q,c,l) = cup in match als with | hd::tl -> if (List.exists (fun i -> i = hd) l) then false else filter cup tl | [] -> true in filter cup als)) ret) then ret else [] in filter allergens cupcakes [] ;;