text
stringlengths
0
601k
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 (width *. width+.acc) 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 = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if cur_el = x then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el,max_num) in match List.sort compare l with | [] -> failwith "Error" | x::rem -> aux rem (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pairing (l: 'a list): ('a * 'a) list = match l with | [] -> [] | [_] -> [ ] | [p1;p2] -> [(p1,p2)] | p1 :: p2 :: tail -> (p1,p2) :: (pairing (p2 :: tail) ) in let l = l in mode (pairing l); ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Second && to_unit = Hour then (from_unit,val_/.3600.) else if from_unit = Hour && to_unit = Second then (from_unit, val_ *. 3600.) else from_unit, val_ ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = Foot && to_unit = Meter then (from_unit,val_ *. 0.3048) else if from_unit = Foot && to_unit = Mile then (from_unit, val_ /. 5280.) else if from_unit = Meter && to_unit = Mile then (from_unit, val_ /. 1609.344) else if from_unit = Meter && to_unit = Foot then (from_unit, val_ /. 0.3048) else if from_unit = Mile && to_unit = Foot then (from_unit, val_ *. 5280.) else if from_unit = Mile && to_unit = Meter then (from_unit, val_ *. 1609.344) else from_unit, val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (_,time_from_unit) = from_unit in let (dist_from_unit,_) = from_unit in let (_,time_to_unit) = to_unit in let (dist_to_unit,_) = to_unit in let (time_unit, time) = convert_time (time_from_unit, 1.) time_to_unit in let (dist_unit, dist) = convert_dist ((dist_from_unit), val_) (dist_to_unit) in ((dist_unit, time_unit), dist/.time) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((_,a_time),_) = a in let ((a_dist,_),_) = a in let ((_,_),a_speed) = a in let ((_,_), speed_2) = convert_speed ((a_dist,a_time), a_speed) (b_unit) in ((a_dist,a_time), b_val +. speed_2) ;;
let passes_da_vinci t = let Leaf | Branch (_,_ ) = t in recursing_list t true ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if cur_el = x then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el,max_num) in match List.sort compare l with | [] -> failwith "Error" | x::rem -> aux rem (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec pairing (l: 'a list): ('a * 'a) list = match l with | [] -> [] | [_] -> [ ] | [p1;p2] -> [(p1,p2)] | p1 :: p2 :: tail -> (p1,p2) :: (pairing (p2 :: tail) ) in let l = l in mode (pairing l); ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Second && to_unit = Hour then (from_unit,val_/.3600.) else if from_unit = Hour && to_unit = Second then (from_unit, val_ *. 3600.) else from_unit, val_ ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = Foot && to_unit = Meter then (from_unit,val_ *. 0.3048) else if from_unit = Foot && to_unit = Mile then (from_unit, val_ /. 5280.) else if from_unit = Meter && to_unit = Mile then (from_unit, val_ /. 1609.344) else if from_unit = Meter && to_unit = Foot then (from_unit, val_ /. 0.3048) else if from_unit = Mile && to_unit = Foot then (from_unit, val_ *. 5280.) else if from_unit = Mile && to_unit = Meter then (from_unit, val_ *. 1609.344) else from_unit, val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (_,time_from_unit) = from_unit in let (dist_from_unit,_) = from_unit in let (_,time_to_unit) = to_unit in let (dist_to_unit,_) = to_unit in let (time_unit, time) = convert_time (time_from_unit, 1.) time_to_unit in let (dist_unit, dist) = convert_dist ((dist_from_unit), val_) (dist_to_unit) in ((dist_unit, time_unit), dist/.time) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((_,a_time),_) = a in let ((a_dist,_),_) = a in let ((_,_),a_speed) = a in let ((_,_), speed_2) = convert_speed ((a_dist,a_time), a_speed) (b_unit) in ((a_dist,a_time), b_val +. speed_2) ;;
let passes_da_vinci t = let Leaf | Branch (_,_ ) = t in recursing_list t true ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> 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 match List.sort compare l with | [] -> failwith "Error" | hd :: tl -> aux tl (hd, 1) (hd, 1) let removehd lst = match lst with | [] -> failwith "Error" | hd :: tl -> tl ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Error"; let l1 = removehd l in let l2 = List.rev l in let l2 = removehd l2 in let l2 = List.rev l2 in let l3 = List.combine l2 l1 in mode l3 ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (to_unit, val_) else if from_unit = Second then (to_unit, val_ /. 3600.) else (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 con_dist = snd(convert_dist(fst(from_unit),val_)(fst(to_unit))) in let con_time = snd(convert_time(snd(to_unit),con_dist)(snd(from_unit))) in (to_unit,con_time) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let su = fst(speed_unit) in let con_time = snd(convert_time(time)(snd(speed_unit))) in let ntime = con_time *. speed_val in (su,ntime) ;; let rec add sub a = match sub with |[] -> a |Branch(d, sub) :: tl -> add tl (d**2. +. a) |Leaf :: tl -> add tl a ;; let rec check sub = match sub with |[] -> true |Leaf :: tl -> check tl |Branch(d, sub) :: tl -> if not(check(sub)) then false else if add sub 0. > d**2. then false else check tl let rec passes_da_vinci t = check [t] ;;
let mode (l: 'a list) : 'a = if (List.length(l) = 0) then failwith "input list cannot be empty" 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 (max_el, max_num) else (cur_el, cur_num) | h :: t -> if compare h cur_el == 0 then aux t (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in let first = List.hd(l) in let last = List.tl(l) in let (a,b) = aux last (first,1) (first,1) in a ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec extract_bigrams list bi_list = match list with | [] -> bi_list | h :: t -> match t with | [] -> bi_list | hd :: tl -> let new_bi_list = [h; hd] :: bi_list in extract_bigrams t new_bi_list in if List.length(l) = 0 || List.length(l) = 1 then failwith "input list cannot be empty" else let pairs = extract_bigrams l [] in let a :: b :: c= mode pairs in (a, b) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> (to_unit, val_ /. 3600.) | Hour, Second -> (to_unit, val_ *. 3600.) | Second, Second -> (to_unit, val_) | Hour, Hour -> (to_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (to_unit, val_ *. 0.3048) | Foot, Mile -> (to_unit, val_ /. 5280.) | Foot, Foot -> (to_unit, val_ ) | Meter, Foot -> (to_unit, val_ /. 0.3048) | Meter, Meter -> (to_unit, val_) | Meter, Mile -> (to_unit, val_ /. 5280. /. 0.3048) | Mile, Foot -> (to_unit, val_ *. 5280.) | Mile, Meter -> (to_unit, val_ *. 5280. *. 0.3048) | Mile, Mile -> (to_unit, val_ ) ;;
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 (_, valuee) = convert_time (d, val_) b in let (_, final) = convert_dist (a, valuee) c in (to_unit, final) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (unit, value) = convert_speed a b_unit in (b_unit, value +. b_val) ;;
let passes_da_vinci t = let rec sumOfSquares list = match list with | [] -> 0. | h :: t -> match h with | Leaf -> sumOfSquares t | Branch (width, children) -> width *. width +. sumOfSquares t in let rec checkList l = match l with | [] -> true | h :: t -> match h with | Leaf -> checkList t | Branch (w, c) -> if w *. w >= sumOfSquares c then checkList c else false in let t_l = t :: [] in checkList t_l ;;
let mode (l: 'a list) : 'a = if (List.length(l) = 0) then failwith "input list cannot be empty" 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 (max_el, max_num) else (cur_el, cur_num) | h :: t -> if compare h cur_el == 0 then aux t (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in let first = List.hd(l) in let last = List.tl(l) in let (a,b) = aux last (first,1) (first,1) in a ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec extract_bigrams list bi_list = match list with | [] -> bi_list | h :: t -> match t with | [] -> bi_list | hd :: tl -> let new_bi_list = [h; hd] :: bi_list in extract_bigrams t new_bi_list in if List.length(l) = 0 || List.length(l) = 1 then failwith "input list cannot be empty" else let pairs = extract_bigrams l [] in let a :: b :: c= mode pairs in (a, b) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> (to_unit, val_ /. 3600.) | Hour, Second -> (to_unit, val_ *. 3600.) | Second, Second -> (to_unit, val_) | Hour, Hour -> (to_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (to_unit, val_ *. 0.3048) | Foot, Mile -> (to_unit, val_ /. 5280.) | Foot, Foot -> (to_unit, val_ ) | Meter, Foot -> (to_unit, val_ /. 0.3048) | Meter, Meter -> (to_unit, val_) | Meter, Mile -> (to_unit, val_ /. 5280. /. 0.3048) | Mile, Foot -> (to_unit, val_ *. 5280.) | Mile, Meter -> (to_unit, val_ *. 5280. *. 0.3048) | Mile, Mile -> (to_unit, val_ ) ;;
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 (_, valuee) = convert_time (d, val_) b in let (_, final) = convert_dist (a, valuee) c in (to_unit, final) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (unit, value) = convert_speed a b_unit in (b_unit, value +. b_val) ;;
let passes_da_vinci t = let rec sumOfSquares list = match list with | [] -> 0. | h :: t -> match h with | Leaf -> sumOfSquares t | Branch (width, children) -> width *. width +. sumOfSquares t in let rec checkList l = match l with | [] -> true | h :: t -> match h with | Leaf -> checkList t | Branch (w, c) -> if w *. w >= sumOfSquares c then checkList c && checkList t else false in let t_l = t :: [] in checkList t_l ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | x :: xs -> if x = cur_el then (let cur_num = cur_num + 1 in (if cur_num > max_num then let max_num = cur_num in let max_el = cur_el in aux xs (cur_el, cur_num) (max_el, max_num) else ( aux xs (cur_el, cur_num) (max_el, max_num) ) ) ) else ( aux xs (x, 1) (max_el, max_num) ) in let l = List.sort compare l in (match l with | [] -> failwith "Empty list." | x :: xs -> aux xs (x, 1) (x, 1) ) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Not enough values for a pair mode" else ( let rev_l = List.rev l in let _ :: l1 = l in let _ :: l2 = rev_l in let l2 = List.rev l2 in let all_pairs = List.combine l2 l1 in mode all_pairs ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (from_unit, val_) else ( match from_unit with | Hour -> (Second, val_ *. 3600.) | Second -> (Hour, val_ /. 3600.) | _ -> failwith "Not a valid inputted time unit" ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (from_unit, val_) else ( match from_unit with | Foot -> (match to_unit with | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) | _ -> failwith "Not a valid distance unit to convert to") | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Mile -> (Mile, (val_ /. 0.3048) /. 5280.) | _ -> failwith "Not a valid distance unit to convert to") | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, (val_ *. 5280.) *. 0.3048) | _ -> failwith "Not a valid distance unit to convert to") | _ -> failwith "Not a valid inputted distance unit" ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit, to_unit with | (d1,t1), (d2,t2) -> ( let (_,dist_val) = convert_dist (d1,val_) d2 in let (_,time_val) = convert_time (t1, 1.) t2 in (to_unit, dist_val/.time_val) ) | _ -> failwith "Not valid speed unit arguments" ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = match a, b_unit with | ((d1, t1), val1), (d2, t2) -> ( let (_,dist_val) = convert_dist (d1,val1) d2 in let (_,time_val) = convert_time (t1, 1.) t2 in (b_unit, b_val +. (dist_val/.time_val)) ) | _ -> failwith "Not valid speed unit arguments" ;;
let passes_da_vinci t = da_vinci_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 | x :: xs -> if x = cur_el then (let cur_num = cur_num + 1 in (if cur_num > max_num then let max_num = cur_num in let max_el = cur_el in aux xs (cur_el, cur_num) (max_el, max_num) else ( aux xs (cur_el, cur_num) (max_el, max_num) ) ) ) else ( aux xs (x, 1) (max_el, max_num) ) in let l = List.sort compare l in (match l with | [] -> failwith "Empty list." | x :: xs -> aux xs (x, 1) (x, 1) ) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Not enough values for a pair mode" else ( let rev_l = List.rev l in let _ :: l1 = l in let _ :: l2 = rev_l in let l2 = List.rev l2 in let all_pairs = List.combine l2 l1 in mode all_pairs ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (from_unit, val_) else ( match from_unit with | Hour -> (Second, val_ *. 3600.) | _ -> (Hour, val_ /. 3600.) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (from_unit, val_) else ( match from_unit with | Foot -> ( match to_unit with | Meter -> (Meter, val_ *. 0.3048) | _ -> (Mile, val_ /. 5280.) ) | Meter -> ( match to_unit with | Foot -> (Foot, val_ /. 0.3048) | _ -> (Mile, (val_ /. 0.3048) /. 5280.) ) | Mile -> ( match to_unit with | Foot -> (Foot, val_ *. 5280.) | _ -> (Meter, (val_ *. 5280.) *. 0.3048) ) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((d1,t1), (d2,t2)) = (from_unit, to_unit) in let (_,dist_val) = convert_dist (d1,val_) d2 in let (_,time_val) = convert_time (t1, 1.) t2 in (to_unit, dist_val/.time_val) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (((d1, t1), val1), (d2, t2)) = (a, b_unit) in let (_,dist_val) = convert_dist (d1,val1) d2 in let (_,time_val) = convert_time (t1, 1.) t2 in (b_unit, b_val +. (dist_val/.time_val)) ;;
let passes_da_vinci t = da_vinci_check [t] ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h::t -> if h = max_el then aux t (cur_el, cur_num + 1) (max_el, max_num + 1) else if h = cur_el then if cur_num = max_num then aux t (cur_el, cur_num + 1) (cur_el, cur_num + 1) else aux t (cur_el, cur_num + 1) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in let a = List.sort compare l in match a with | [] -> raise Not_found | h::_ -> aux a (h, 0) (h, 0);; let rec helper l_i l_o = match l_i with | [] -> [] | [_] -> l_o | h::t -> let h1::_ = t in helper t ((h, h1)::l_o) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (helper l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let a = (from_unit, to_unit) in match a with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | (c, d) -> if c = d then (c, val_) else raise Not_found ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let a = (from_unit, to_unit) in match a with | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (Meter, (val_ *. 5280.) *. 0.3048) | (c, d) -> if c = d then (c, val_) else raise Not_found ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let a = (from_unit, to_unit) in let ((x1, y1), (x2, y2)) = a in let dist = convert_dist (x1, val_) x2 in let time = convert_time (y1, 1.) y2 in let ((_, y3), (_, y4)) = (dist, time) in (to_unit, y3 /. y4) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit, time_unit) = speed_unit in let time_conv = convert_time time time_unit in let (_, value) = time_conv in (dist_unit, value *. speed_val) ;; let rec da_vinci (l: tree list) (f: float) (acc : float) = match l with | [] -> acc <= f | Leaf::t -> if acc > f then false else da_vinci t f acc | Branch (f1, l1)::t -> if acc > f then false else da_vinci t f (acc +. (f1 *. f1)) && da_vinci l1 (f1 *. f1) 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (f, l) -> da_vinci l (f *. f) 0.;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h::t -> if h = max_el then aux t (cur_el, cur_num + 1) (max_el, max_num + 1) else if h = cur_el then if cur_num = max_num then aux t (cur_el, cur_num + 1) (cur_el, cur_num + 1) else aux t (cur_el, cur_num + 1) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in let a = List.sort compare l in match a with | [] -> raise Not_found | h::_ -> aux a (h, 0) (h, 0);; let rec helper l_i l_o = match l_i with | [] -> [] | [_] -> l_o | h::t -> let h1::_ = t in helper t ((h, h1)::l_o) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (helper l []) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let a = (from_unit, to_unit) in match a 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 = let a = (from_unit, to_unit) in match a with | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (Meter, (val_ *. 5280.) *. 0.3048) | _ -> (from_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let a = (from_unit, to_unit) in let ((x1, y1), (x2, y2)) = a in let dist = convert_dist (x1, val_) x2 in let time = convert_time (y1, 1.) y2 in let ((_, y3), (_, y4)) = (dist, time) in (to_unit, y3 /. y4) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit, time_unit) = speed_unit in let time_conv = convert_time time time_unit in let (_, value) = time_conv in (dist_unit, value *. speed_val) ;; let rec da_vinci (l: tree list) (f: float) (acc : float) = match l with | [] -> acc <= f | Leaf::t -> if acc > f then false else da_vinci t f acc | Branch (f1, l1)::t -> if acc > f then false else da_vinci t f (acc +. (f1 *. f1)) && da_vinci l1 (f1 *. f1) 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (f, l) -> da_vinci l (f *. f) 0.;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> if cur_num > max_num then cur_el else max_el |h :: t -> if cur_el = h then if cur_num+1 > max_num then aux t (h, cur_num+1) (cur_el, cur_num+1) else aux t (h, cur_num+1) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in let sortedlist = List.sort compare l in match sortedlist with |[] -> failwith "list is empty" |hd :: tl -> aux tl (hd, 1) (hd, 1) ;; let removehead (l: 'a list) : 'a list = let h :: t = l in t ;; let removetail(l: 'a list) : 'a list = let h :: t = List.rev l in List.rev t ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length(l) = 1 then failwith "too little elements" else ( match l with |[] -> failwith "list is empty" |_ -> let l1 = removehead(l) in let l2 = removetail(l) in let combinedlist = List.combine(l2)(l1) in mode(combinedlist) ) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (from_unit, val_) else ( match from_unit with |Second -> (to_unit, val_/.3600.) |Hour -> (to_unit, val_*.3600.) ) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = to_unit then (from_unit, val_) else ( match to_unit with |Foot -> if from_unit = Meter then (to_unit, val_/.0.3048) else (to_unit, val_*.5280.) |Meter -> if from_unit = Foot then (to_unit, val_*.0.3048) else (to_unit, val_*.1609.344) |Mile -> if from_unit = Foot then (to_unit, val_/.5280.) else (to_unit, (val_/.0.3048)/.5280.) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let originaldistance = fst(from_unit) in let originaltime = snd(from_unit) in let t = snd(convert_time(originaltime, 1.0)(snd(to_unit))) in let d = snd(convert_dist(originaldistance, val_/.t)(fst(to_unit))) in (to_unit, d) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst(speed_unit), speed_val*.snd(convert_time(time)(snd(speed_unit)))) ;; let rec passes_da_vinci t = let rec summation subtree a = match subtree with |[] -> a |Leaf :: tl -> summation tl a |Branch(w, st) :: tl -> summation (tl)(a +. w**2.) in let rec recursivecheck subtree = match subtree with |[] -> true |Leaf :: tl -> recursivecheck tl |Branch(w, st) :: tl -> if not(recursivecheck(st)) then false else if summation (st)(0.) > w ** 2. then false else recursivecheck tl in recursivecheck [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 |x :: xs -> if x = cur_el then if (cur_num + 1) > max_num then aux xs (cur_el,cur_num+1) (x,cur_num+1) else aux xs (cur_el,cur_num+1) (max_el,max_num) else aux xs (x,1)(max_el,max_num) in let l = List.sort compare l in match List.length l with |0 -> failwith "error message" |1 -> List.hd l |_ -> aux l (List.hd l,0)(List.hd l,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match List.length l with |0 -> failwith "error message" |1 -> failwith "error message" |_ -> let new_list = let rec list_fun l acc = match List.length l with |1-> acc |_ -> match l with |[] -> acc |x::xs -> list_fun xs ((x,List.hd xs) :: acc) in list_fun l [] in mode new_list ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with |(Second,Hour) -> (to_unit,val_ /. 3600.0) |(Hour,Second) -> (to_unit, val_ *. 3600.0) |(_,_) -> (to_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,to_unit) with |(Foot,Meter) ->(to_unit, val_ *. 0.3048) |(Foot,Mile) ->(to_unit, val_ /. 5280.0) |(Meter,Foot) ->(to_unit, val_ /. 0.3048) |(Meter,Mile) ->(to_unit, val_ /. (5280.0*.0.3048)) |(Mile,Foot) ->(to_unit, val_ *. 5280.0) |(Mile,Meter) ->(to_unit, val_ *. (0.3048*.5280.0)) |(_,_) -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((fu,timeu1),(tu,timeu2)) = (from_unit,to_unit) in let (_,v) = convert_time(timeu2,val_) timeu1 in let (_,value) = convert_dist (fu,v) tu in (to_unit, value) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dis_unit,tim_unit) = speed_unit in let (_,time_value) = convert_time (time) (tim_unit)in (dis_unit, time_value *. speed_val) ;; let rec passes_da_vinci t = let sum_width t = match t with |Leaf -> 0. |Branch(width,_) -> width *. width in match t with |Leaf -> true |Branch(width,list) -> let sum = let rec find_num l acc = match l with |[] -> acc |h :: t -> find_num t acc +. sum_width h in find_num list 0. in if sum > width *.width then false else List.for_all passes_da_vinci list ;;
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 if (cur_num + 1) > max_num then aux xs (cur_el,cur_num+1) (x,cur_num+1) else aux xs (cur_el,cur_num+1) (max_el,max_num) else aux xs (x,1)(max_el,max_num) in let l = List.sort compare l in match List.length l with |0 -> failwith "error message" |1 -> List.hd l |_ -> aux l (List.hd l,0)(List.hd l,0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match List.length l with |0 -> failwith "error message" |1 -> failwith "error message" |_ -> let new_list = let rec list_fun l acc = match List.length l with |1-> acc |_ -> match l with |[] -> acc |x::xs -> list_fun xs ((x,List.hd xs) :: acc) in list_fun l [] in mode new_list ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with |(Second,Hour) -> (to_unit,val_ /. 3600.0) |(Hour,Second) -> (to_unit, val_ *. 3600.0) |(_,_) -> (to_unit, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,to_unit) with |(Foot,Meter) ->(to_unit, val_ *. 0.3048) |(Foot,Mile) ->(to_unit, val_ /. 5280.0) |(Meter,Foot) ->(to_unit, val_ /. 0.3048) |(Meter,Mile) ->(to_unit, val_ /. (5280.0*.0.3048)) |(Mile,Foot) ->(to_unit, val_ *. 5280.0) |(Mile,Meter) ->(to_unit, val_ *. (0.3048*.5280.0)) |(_,_) -> (to_unit, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((fu,timeu1),(tu,timeu2)) = (from_unit,to_unit) in let (_,v) = convert_time(timeu2,val_) timeu1 in let (_,value) = convert_dist (fu,v) tu in (to_unit, value) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dis_unit,tim_unit) = speed_unit in let (_,time_value) = convert_time (time) (tim_unit)in (dis_unit, time_value *. speed_val) ;; let rec passes_da_vinci t = let sum_width t = match t with |Leaf -> 0. |Branch(width,_) -> width *. width in match t with |Leaf -> true |Branch(width,list) -> let sum = let rec find_num l acc = match l with |[] -> acc |h :: t -> find_num t acc +. sum_width h in find_num list 0. in if sum > width *.width then false else List.for_all passes_da_vinci list ;;
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 if cur_el > max_el then max_el else cur_el else if cur_num < max_num then max_el else cur_el | hd :: tl -> if cur_el = hd then let cur_num = (cur_num + 1) in if cur_num > max_num then aux tl (hd, cur_num) (hd, cur_num) else aux tl (hd, cur_num) (max_el, max_num) else aux tl (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 l == [] || List.length l < 2 then failwith "Invalid input" else let rm_hd (l: 'a list) : 'a list = match l with | [] -> [] | hd :: tl -> tl in let l1 = rm_hd l in let l2 = List.rev (rm_hd (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 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 -> (to_unit, val_) | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) ) | Meter -> ( match to_unit with | Foot -> (to_unit, val_ /. 0.3048) | Meter -> (to_unit, val_) | Mile -> (to_unit, val_ /. 1609.344) ) | Mile -> ( match to_unit with | Foot -> (to_unit, val_ *. 5280.) | Meter -> (to_unit, val_ *. 1609.344) | Mile -> (to_unit, val_) ) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist = fst(from_unit) in let to_dist = fst(to_unit) in let from_time = snd(from_unit) in let to_time = snd(to_unit) in (to_unit, snd(convert_dist (from_dist, val_) to_dist) /. snd(convert_time (from_time, 1.) to_time)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted_a = convert_speed a b_unit in (b_unit, snd(converted_a) +. 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**2.)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((sum_ subtree 0. <= width**2.) && check subtree) then false else check tl in check [t] ;;
let pair_mode_tests: (int list * (int * int) ) list = [ ([1;1], (1,1)); ([1;6;6;6;1;7;1;7;6;1;7;1;6;1;7;9], (1,7)); ([1;2;1;2;1;2;1;2;5;2;5;2;5;2;5], (1,2)); ] ;;
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Hour -> if from_unit = Second then Hour, val_ /. 3600. else Hour, val_ | Second -> if from_unit = Hour then Second, val_ *. 3600. else Second, val_ ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Foot then Foot, val_ else if to_unit = Meter then Meter, val_ *. 0.3048 else Mile, val_ /. 5280. | Meter -> if to_unit = Meter then Meter, val_ else if to_unit = Foot then Foot, val_ *. 3.2808399 else Mile, val_ *. 3.2808399 /. 5280. | Mile -> if to_unit = Mile then Mile, val_ else if to_unit = Foot then Foot, val_ *. 5280. else Meter, val_ *. 5280. *. 0.3048 ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = ((fst(convert_dist (fst(from_unit), val_) (fst(to_unit)))), fst(convert_time (snd(from_unit),1.) (snd(to_unit)))), (snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) /. (snd(convert_time (snd(from_unit),1.) (snd(to_unit)))) else ((Mile, Hour), 0.9) *) ;;
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 acc = match l with | [] -> acc +. 0. | 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 ((check subtree) && ((sum_ subtree 0.) <= (width ** 2.))) then true else check tl in check [t] ;;
let mode (l: 'a list) : 'a = if l = [] then failwith "Empty List" else 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 (cur_el, cur_num + 1) (cur_el, cur_num + 1) else aux xs (cur_el, cur_num + 1) (max_el, max_num) else if 1 > max_num then aux xs (x,1) (x, 1) else aux xs (x,1) (max_el, max_num) in aux (List.sort compare l) (List.hd l , 0) (List.hd l , 0) ;; let bigram (l: 'a list) = if l = [] then failwith "Empty List" else let rec aux (l: 'a list) lst = match l with | x :: [] -> lst | x :: xs -> aux xs (List.append lst [(x, List.hd xs)]) | [] -> lst in aux l [] ;;
let pair_mode (l: 'a list) : 'a * 'a = if l = [] then failwith "Empty List" else mode (bigram l) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, x) -> if to_unit = Hour then (Hour, x /. 3600.0) else (from_unit, val_) | (Hour, x) -> if to_unit = Second then (Second, x *. 3600.0) else (from_unit, 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 match (from_unit, val_) with | (Foot, x) -> if to_unit = Meter then (Meter, x *. 0.3048) else (Mile, x /. 5280.0) | (Meter, x) -> if to_unit = Foot then (Foot, x /. 0.3048) else (Mile, x *. 0.000621371192) | (Mile, x) -> if to_unit = Foot then (Foot, x *. 5280.0) else (Meter, x *. 5280.0 *. 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 if fst from_unit = fst to_unit then ((fst to_unit, snd to_unit), snd (convert_time (snd to_unit, val_) (snd from_unit))) else if snd from_unit = snd to_unit then ((fst to_unit, snd to_unit), snd (convert_dist (fst from_unit, val_) (fst to_unit))) else ((fst to_unit, snd to_unit), snd (convert_time (snd to_unit, (snd (convert_dist (fst from_unit, val_) (fst to_unit))) ) (snd from_unit))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. snd (convert_speed a b_unit)) ;;
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. width *. width) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && ((width *. width) >= (sum_ subtree 0.))) = false then false else check tl in check [t] ;;