text
stringlengths 0
601k
|
---|
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_) with | (Foot, value) -> if to_unit= Foot then (Foot, value) else if to_unit = Meter then (Meter, 0.3048 *. value) else (Mile, value /. 5280.) | (Meter, value) -> if to_unit= Foot then (Foot, value /. 0.3048) else if to_unit = Meter then (Meter, value) else (Mile, value *. (1. /. (5280. *. 0.3048))) | (Mile, value) -> if to_unit= Foot then (Foot, value *. 5280.) else if to_unit = Meter then (Meter, value *. 0.3048 *. 5280.) else (Mile, value) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist, time) = from_unit in let (dist_tar, time_tar) = to_unit in let (dist_unit_re, dist_re) = convert_dist (dist, val_) dist_tar in let (time_unit_re, time_re) = (convert_time (time, 1.) time_tar) in (to_unit, dist_re /. time_re) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (t_unit, t_val) = time in let (dist_stand, time_stand) = speed_unit in let (t_unit_conv, t_val_conv) = convert_time (t_unit, t_val) time_stand in (dist_stand, speed_val *. t_val_conv) ;; let rec passes_da_vinci t = let rec chilren_branch_width_sum t acc : float = match t with | [] -> acc | Branch(width, l) :: tail -> chilren_branch_width_sum tail (acc +. width *. width) | Leaf :: tail -> chilren_branch_width_sum tail acc in match t with | Leaf -> true | Branch (width , children) -> chilren_branch_width_sum children 0. <= width *. width ;; |
let mode (l: 'a list) : 'a = let rec aux ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int)= function | [] -> if cur_num > max_num then cur_el else max_el | x::xs -> if cur_el = x then aux (cur_el, (cur_num + 1)) (max_el, max_num) xs else if cur_num > max_num then aux (x, 1) (cur_el, cur_num) xs else aux (x, 1) (max_el, max_num) xs in let x::xs = List.sort compare l in aux (x, 1) (x, 0) xs ;; let cut x = match x with | [] -> [] | first::remain -> remain;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = l in let l2 = l in let new_l1 = cut(l1) in let new_l2 = List.rev(cut(List.rev l2)) in mode(List.combine new_l2 new_l1) ;; let convert_to_second ((from_unit, val_) : time_unit value) = match from_unit with | Second -> val_ | Hour -> val_ *. 3600.;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> Second, convert_to_second ((from_unit, val_) : time_unit value) | Hour -> Hour, convert_to_second ((from_unit, val_) : time_unit value) /. 3600. ;; let convert_to_foot ((from_unit, val_) : dist_unit value) = match from_unit with | Foot -> val_ | Meter -> val_ /. 0.3048 | Mile -> val_ *. 5280.;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> Foot,convert_to_foot ((from_unit, val_) : dist_unit value) | Meter -> Meter, convert_to_foot ((from_unit, val_) : dist_unit value) *. 0.3048 | Mile -> Mile, convert_to_foot ((from_unit, val_) : dist_unit value) /.5280. ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist = fst(from_unit) in let from_time = snd(from_unit) in let to_dist = fst(to_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 new_val = snd(convert_speed(a) b_unit) in b_unit, new_val +. b_val ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl acc +. width *. width 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 = let rec find_mode 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 cur_el = hd then find_mode tl (cur_el,(cur_num + 1)) (max_el,max_num) else if cur_num > max_num then find_mode tl (hd,1) (cur_el,cur_num) else find_mode tl (hd,1) (max_el,max_num) in match List.sort compare l with | [] -> failwith "the list is empty" | hd :: tl -> find_mode tl (hd,1) (hd,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l = 1 || List.length l = 0 then failwith "list is too small" else let rec make_tuples (acc)(l) = match l with | [] -> acc | [_] -> acc | x::y::tl -> make_tuples ((x, y) :: acc)(y::tl) in mode(make_tuples([])(l)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if from_unit = to_unit then (to_unit,val_) else (to_unit, val_ /. 3600.0) | Hour -> if from_unit = to_unit then (to_unit,val_) else (to_unit, val_ *. 3600.0) ;; |
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 | 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.0) | Meter -> (Meter, val_*.1609.344)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (_, dist_converted) = convert_dist (fst from_unit, val_) (fst to_unit) in let (_, inv_factor) = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, dist_converted /. inv_factor) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let t' = convert_time (time)(snd speed_unit) in (fst speed_unit, snd t' *. speed_val) ;; let rec passes_da_vinci t = let get_factor t = match t with | Leaf -> 0. | Branch (diameter, _) -> diameter *. diameter in match t with | Leaf -> true | Branch (diameter, trees) -> let sum_vals = List.fold_left (fun acc el -> acc +. get_factor el) 0. trees in if sum_vals > diameter *. diameter then false else List.for_all passes_da_vinci trees ;; |
let mode (l: 'a list) : 'a = if l=[] then failwith "Invalid input." else let l_alt = List.sort compare l in let rec aux (l_alt: 'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l_alt with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match l_alt with | [] -> failwith "Invalid input." | [e] -> e | e1 :: tl -> aux tl (e1, 1) (e1, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Invalid input." else let (_::l1) = l in let l_alt = List.rev l in let (_::l2_alt) = l_alt in let l2 = List.rev l2_alt in let l_comb = List.combine l2 l1 in mode l_comb ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Second then if to_unit = Hour then (Hour, val_/.3600.0) else if to_unit = Second then (Second, val_) else failwith "Invalid to_unit." else if from_unit = Hour then if to_unit = Second then (Second, val_*.3600.0) else if to_unit = Hour then (Hour, val_) else failwith "Invalid to_unit." else failwith "Invalid from_unit." ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = Foot then if to_unit = Meter then (Meter, val_*.0.3048) else if to_unit = Mile then (Mile, val_/.5280.0) else if to_unit = Foot then (Foot, val_) else failwith "Invalid to_unit." else if from_unit = Meter then if to_unit = Foot then (Foot, val_/.0.3048) else if to_unit = Mile then (Mile, val_/.0.3048/.5280.0) else if to_unit = Meter then (Meter, val_) else failwith "Invalid to_unit." else if from_unit = Mile then if to_unit = Foot then (Foot, val_*.5280.0) else if to_unit = Meter then (Meter, val_*.5280.0*.0.3048) else if to_unit = Mile then (Mile, val_) else failwith "Invalid to_unit." else failwith "Invalid from_unit." ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist = fst(from_unit) in let from_time = snd(from_unit) in let to_dist = fst(to_unit) in let to_time = snd(to_unit) in let (dist, vald) = convert_dist (from_dist, 1.) to_dist in let (time, valt) = convert_time (from_time, 1.) to_time in ((dist, time), val_*.(vald/.valt)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_conv = convert_speed a b_unit in let ab_val = snd(a_conv) +. b_val in (b_unit, ab_val) ;; |
let passes_da_vinci tree : bool = let rec summation (treelist : tree list) (sum : float) : float = match treelist with | [] -> sum | Leaf :: tl -> summation tl sum | Branch (width, _) :: tl -> summation tl (sum +. width*.width) in let rec check (treelist : tree list) : bool = match treelist with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (check tl) && (check subtree) && (summation subtree 0. <= width*.width) in check [tree] ;; |
let mode (l: 'a list) : 'a = if l=[] then failwith "Invalid input." else let l_alt = List.sort compare l in let rec aux (l_alt: 'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l_alt with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match l_alt with | [] -> failwith "Invalid input." | [e] -> e | e1 :: tl -> aux tl (e1, 1) (e1, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Invalid input." else let l1 = List.tl l in let l_rev = List.rev l in let l2_alt = List.tl l_rev in let l2 = List.rev l2_alt in let l_comb = List.combine l2 l1 in mode l_comb ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Second then if to_unit = Hour then (Hour, val_/.3600.0) else if to_unit = Second then (Second, val_) else failwith "Invalid to_unit." else if from_unit = Hour then if to_unit = Second then (Second, val_*.3600.0) else if to_unit = Hour then (Hour, val_) else failwith "Invalid to_unit." else failwith "Invalid from_unit." ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = Foot then if to_unit = Meter then (Meter, val_*.0.3048) else if to_unit = Mile then (Mile, val_/.5280.0) else if to_unit = Foot then (Foot, val_) else failwith "Invalid to_unit." else if from_unit = Meter then if to_unit = Foot then (Foot, val_/.0.3048) else if to_unit = Mile then (Mile, val_/.0.3048/.5280.0) else if to_unit = Meter then (Meter, val_) else failwith "Invalid to_unit." else if from_unit = Mile then if to_unit = Foot then (Foot, val_*.5280.0) else if to_unit = Meter then (Meter, val_*.5280.0*.0.3048) else if to_unit = Mile then (Mile, val_) else failwith "Invalid to_unit." else failwith "Invalid from_unit." ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist = fst(from_unit) in let from_time = snd(from_unit) in let to_dist = fst(to_unit) in let to_time = snd(to_unit) in let (dist, vald) = convert_dist (from_dist, 1.) to_dist in let (time, valt) = convert_time (from_time, 1.) to_time in ((dist, time), val_*.(vald/.valt)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_conv = convert_speed a b_unit in let ab_val = snd(a_conv) +. b_val in (b_unit, ab_val) ;; |
let passes_da_vinci tree : bool = let rec summation (treelist : tree list) (sum : float) : float = match treelist with | [] -> sum | Leaf :: tl -> summation tl sum | Branch (width, _) :: tl -> summation tl (sum +. width*.width) in let rec check (treelist : tree list) : bool = match treelist with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (check tl) && (check subtree) && (summation subtree 0. <= width*.width) in check [tree] ;; |
let mode (l: 'a list) : 'a = if l=[] then failwith "Invalid input." else let l_alt = List.sort compare l in let rec aux (l_alt: 'a list) ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l_alt with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match l_alt with | [] -> failwith "Invalid input." | [e] -> e | e1 :: tl -> aux tl (e1, 1) (e1, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Invalid input." else let rm (l: 'a list) : 'a list = match l with | [] -> failwith "Invalid input." | _ :: xs -> xs in let l1 = rm l in let l_rev = List.rev l in let l2_alt = rm l_rev in let l2 = List.rev l2_alt in let l_comb = List.combine l2 l1 in mode l_comb ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if val_ >= 0. then match to_unit with | Hour -> (Hour, val_/.3600.0) | Second -> (Second, val_) else failwith "Invalid value." | Hour -> if val_ >= 0. then match to_unit with | Hour -> (Hour, val_) | Second -> (Second, val_*.3600.0) else failwith "Invalid value." ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if val_ >= 0. then match to_unit with | Meter -> (Meter, val_*.0.3048) | Mile -> (Mile, val_/.5280.0) | Foot -> (Foot, val_) else failwith "Invalid value." | Meter -> if val_ >= 0. then match to_unit with | Meter -> (Meter, val_) | Mile -> (Mile, val_/.0.3048/.5280.0) | Foot -> (Foot, val_/.0.3048) else failwith "Invalid value." | Mile -> if val_ >= 0. then match to_unit with | Meter -> (Meter, val_*.5280.0*.0.3048) | Mile -> (Mile, val_) | Foot -> (Foot, val_*.5280.0) else failwith "Invalid value." ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist = fst(from_unit) in let from_time = snd(from_unit) in let to_dist = fst(to_unit) in let to_time = snd(to_unit) in let (dist, vald) = convert_dist (from_dist, 1.) to_dist in let (time, valt) = convert_time (from_time, 1.) to_time in ((dist, time), val_*.(vald/.valt)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_conv = convert_speed a b_unit in let ab_val = snd(a_conv) +. b_val in (b_unit, ab_val) ;; |
let passes_da_vinci tree : bool = let rec summation (treelist : tree list) (sum : float) : float = match treelist with | [] -> sum | Leaf :: tl -> summation tl sum | Branch (width, _) :: tl -> summation tl (sum +. width*.width) in let rec check (treelist : tree list) : bool = match treelist with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (check tl) && (check subtree) && (summation subtree 0. <= width*.width) in check [tree] ;; |
let mode (l) = let rec aux l (cur_el, cur_num) (max_el, max_num) = match l with |[]-> if cur_num > max_num then cur_el else max_el | h::t -> if cur_el =h then aux t (cur_el, (cur_num+1)) (max_el, max_num) else if cur_num>max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in match List.sort compare l with |[]->failwith "This list is too small" |h::t->aux t (h, 1) (h, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "This list is too small" else match l with |[] -> failwith "error" |(_ :: back)-> match List.rev l with |[] -> failwith "error" |(_ :: no_back)-> let no_back = List.rev no_back in let to_pair=List.combine no_back back in mode to_pair;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit= Second then (from_unit, val_) else (Hour,(val_/.3600.)) |Hour-> if to_unit= Second then (Second,(val_*.3600.)) else (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> if to_unit= Foot then (from_unit, val_) else if to_unit= Meter then (Meter,(val_*.0.3048)) else (Mile,(val_/.5280.)) |Meter-> if to_unit= Foot then (Foot,(val_/.0.3048)) else if to_unit= Meter then (from_unit, val_) else (Mile,(val_/.1609.344)) |Mile-> if to_unit= Foot then (Foot,(val_*.5280.)) else if to_unit= Meter then (Meter,(val_*.1609.344)) else (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist,time) = from_unit in let (to_dist,to_time)=to_unit in let (time_2,val_2)= convert_time (to_time, val_) time in let (dist_2,val_3)= convert_dist (dist, val_2) to_dist in ((dist_2,time_2),val_3) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_,val_conv)=convert_speed a b_unit in (b_unit,val_conv+.b_val) ;; |
let passes_da_vinci t = check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[]-> if cur_num > max_num then cur_el else max_el | h::t -> if cur_el =h then aux t (cur_el, (cur_num+1)) (max_el, max_num) else if cur_num>max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in match List.sort compare l with |[]->failwith "This list is too small" |h::t->aux t (h, 1) (h, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "This list is too small" else match l with |[] -> failwith "error" |(_ :: back)-> match List.rev l with |[] -> failwith "error" |(_ :: no_back)-> let no_back = List.rev no_back in let to_pair=List.combine no_back back in mode to_pair;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit= Second then (from_unit, val_) else (Hour,(val_/.3600.)) |Hour-> if to_unit= Second then (Second,(val_*.3600.)) else (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> if to_unit= Foot then (from_unit, val_) else if to_unit= Meter then (Meter,(val_*.0.3048)) else (Mile,(val_/.5280.)) |Meter-> if to_unit= Foot then (Foot,(val_/.0.3048)) else if to_unit= Meter then (from_unit, val_) else (Mile,(val_/.1609.344)) |Mile-> if to_unit= Foot then (Foot,(val_*.5280.)) else if to_unit= Meter then (Meter,(val_*.1609.344)) else (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist,time) = from_unit in let (to_dist,to_time)=to_unit in let (time_2,val_2)= convert_time (to_time, val_) time in let (dist_2,val_3)= convert_dist (dist, val_2) to_dist in ((dist_2,time_2),val_3) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_,val_conv)=convert_speed a b_unit in (b_unit,val_conv+.b_val) ;; |
let passes_da_vinci t = check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Input is not valid" | hd :: tl -> aux tl (hd, 1) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Input is not valid" else match l with | [] -> begin failwith "Input is not valid" end |_ -> begin let (_ :: l1) = l in let (_ :: l2_rev) = List.rev l in let l2 = List.rev l2_rev in mode (List.combine l2 l1) end ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit==Second && val_ >=0. then if to_unit==Hour then (Hour, val_ /.3600.) else (Second, val_) else if from_unit==Hour && val_ >=0. then if to_unit ==Second then (Second, val_ *.3600.) else(Hour,val_) else failwith"Input is not valid" ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit==Foot && val_ >=0. then 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"Input is not valid" else if from_unit==Meter && val_ >=0. then 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_ /. 5280. /. 0.3048) else failwith"Input is not valid" else if from_unit==Mile && val_ >= 0. then if to_unit==Foot then (Foot,val_ *. 5280.) else if to_unit==Meter then (Meter,val_ *. 5280. *. 0.3048) else if to_unit==Mile then (Mile, val_) else failwith"Input is not valid" else failwith"Input is not valid" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dis_1 = fst (from_unit) in let time_1 = snd(from_unit) in let dis_2 = fst (to_unit) in let time_2 = snd (to_unit) in let (dis, con_dis) = convert_dist (dis_1, 1.) dis_2 in let (time, con_time) = convert_time (time_1, 1.) time_2 in ((dis,time),(val_ *. (con_dis/.con_time))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((dis,time),con_val) = convert_speed a b_unit in (dis,time),(b_val +. con_val) ;; |
let passes_da_vinci t = let rec sum (l : tree list) (acc :float) :float = match l with | [] -> acc | Leaf :: tl -> sum tl acc | Branch (width, subtree) :: tl -> sum tl (acc +. width *. width) in let rec check (l : tree list) : bool = 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) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Input is not valid" | hd :: tl -> aux tl (hd, 1) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Input is not valid" else let l1 = List.tl l in let l_rev = List.rev l in let l2_rev = List.tl l_rev in let l2 = List.rev l2_rev in mode (List.combine l2 l1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit==Second && val_ >=0. then if to_unit==Hour then (Hour, val_ /.3600.) else (Second, val_) else if from_unit==Hour && val_ >=0. then if to_unit ==Second then (Second, val_ *.3600.) else(Hour,val_) else failwith "Input is not valid" ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit==Foot && val_ >=0. then 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 "Input is not valid" else if from_unit==Meter && val_ >=0. then 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_ /. 5280. /. 0.3048) else failwith "Input is not valid" else if from_unit==Mile && val_ >= 0. then if to_unit==Foot then (Foot,val_ *. 5280.) else if to_unit==Meter then (Meter,val_ *. 5280. *. 0.3048) else if to_unit==Mile then (Mile, val_) else failwith "Input is not valid" else failwith "Input is not valid" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ >= 0. then let dis_1 = fst (from_unit) in let time_1 = snd(from_unit) in let dis_2 = fst (to_unit) in let time_2 = snd (to_unit) in let (dis, con_dis) = convert_dist (dis_1, 1.) dis_2 in let (time, con_time) = convert_time (time_1, 1.) time_2 in ((dis,time),(val_ *. (con_dis /.con_time))) else failwith "Input is not valid" ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((dis,time),con_val) = convert_speed a b_unit in (dis,time),(b_val +. con_val) ;; |
let passes_da_vinci (t :tree) : bool= let rec check (l : tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (check subtree) && (helper_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) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Input is not valid" | hd :: tl -> aux tl (hd, 1) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Input is not valid" else let l1 = List.tl l in let l_rev = List.rev l in let l2_rev = List.tl l_rev in let l2 = List.rev l2_rev in mode (List.combine l2 l1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit==Second && val_ >=0. then if to_unit==Hour then (Hour, val_ /.3600.) else (Second, val_) else if from_unit==Hour && val_ >=0. then if to_unit ==Second then (Second, val_ *.3600.) else(Hour,val_) else failwith "Input is not valid" ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit==Foot && val_ >=0. then 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 "Input is not valid" else if from_unit==Meter && val_ >=0. then 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_ /. 5280. /. 0.3048) else failwith "Input is not valid" else if from_unit==Mile && val_ >= 0. then if to_unit==Foot then (Foot,val_ *. 5280.) else if to_unit==Meter then (Meter,val_ *. 5280. *. 0.3048) else if to_unit==Mile then (Mile, val_) else failwith "Input is not valid" else failwith "Input is not valid" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ >= 0. then let dis_1 = fst (from_unit) in let time_1 = snd(from_unit) in let dis_2 = fst (to_unit) in let time_2 = snd (to_unit) in let (dis, con_dis) = convert_dist (dis_1, 1.) dis_2 in let (time, con_time) = convert_time (time_1, 1.) time_2 in ((dis,time),(val_ *. (con_dis /.con_time))) else failwith "Input is not valid" ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((dis,time),con_val) = convert_speed a b_unit in (dis,time),(b_val +. con_val) ;; |
let passes_da_vinci (t :tree) : bool= let rec check (l : tree list) : bool = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (check subtree) && (helper_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) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs when x = cur_el -> aux xs (cur_el, (cur_num+1)) (max_el, max_num) | x :: xs when x != cur_el -> if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) |_ -> failwith "This path should be impossible" in match l with | [] -> failwith "Unable to deal with an empty list" | cur :: body -> let sortedList = (List.sort compare l) in aux sortedList (cur, 0) (cur, -1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec get_pair_list (list: 'a list) (prev : 'a) (result: ('a * 'a) list) : ('a * 'a) list = match list with | [] -> result | x :: xs -> get_pair_list xs x ((prev, x) :: result) in match l with | [] -> notimplemented () | x :: xs -> mode (get_pair_list xs x ([])) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Second then if to_unit = Hour then (Hour, (val_ /. 3600.00)) else (Second, val_) else if to_unit = Second then (Hour, (val_ *. 3600.00)) 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 = Meter then (Meter, (val_ *. 0.3048)) else if to_unit = Mile then (Mile, (val_ /. 5280.)) else (Foot, val_) | Meter -> if to_unit = Foot then (Foot, (val_ /. 0.3048)) else if to_unit = Mile then (Mile, (val_ /. (0.3048 *. 5280.))) else (Meter, val_) | Mile -> if from_unit = Mile && to_unit = Foot then (Foot, (val_ *. 5280.)) else if from_unit = Mile && to_unit = Meter then (Meter, (val_ *. (0.3048 *. 5280.))) else (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 dist = convert_dist (from_dist, val_) to_dist in let (unit, dist_val)= dist in let time = convert_time (from_time, 1.) to_time in let (unit, time_val) = time in ((to_dist, to_time), dist_val /. time_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_u,time_u) = speed_unit in let time_conv = (convert_time time time_u) in let (time_unit,time_v) = time_conv in (dist_u, speed_val *. time_v) ;; let rec passes_da_vinci t = let rec count_tree li total = match li with | [] -> total | x :: xs -> match x with | Leaf -> count_tree xs total | Branch (v, l) -> count_tree xs (total +. (v *. v)) in let rec check_all_branch li = match li with | [] -> true | x :: xs -> if passes_da_vinci x then check_all_branch xs else false in match t with | Leaf -> true | Branch (val_, l) -> if (val_ *. val_) >= count_tree l 0. then check_all_branch l else false ;; |
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::tl -> if compare cur_el x = 0 then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (x,1) (cur_el, cur_num) else aux tl (x,1) (max_el, max_num) ;; |
let mode (l: 'a list) : 'a = match List.sort compare 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 "invalid input" else let l_rev = List.tl (List.rev l) in let two_lists = List.combine (List.rev l_rev) (List.tl l) in mode two_lists ;; |
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.0) | Hour -> (to_unit, val_ *. 3600.0) ;; let 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.0) | Hour -> (to_unit, val_ /. 3600.0);; |
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 -> 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 = if from_unit = to_unit then (from_unit, val_) else let converted_distance = convert_dist ((fst from_unit),val_) (fst to_unit) in (let converted_time = time ((snd from_unit),(snd converted_distance)) (snd to_unit) in (to_unit, (snd converted_time))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_ = convert_speed a b_unit in (b_unit, (snd a_) +. b_val) ;; |
let passes_da_vinci t = let rec check t = match t with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && (compute_sum subtree 0. <= width *. width)) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = if List.length(l) == 0 then failwith "Input list is not valid." else 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) : time_unit value = if from_unit == to_unit then (to_unit, val_) else match from_unit with | Second -> (Hour, val_ /. 3600.0) | Hour -> (Second, val_ *. 3600.0) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) (to_unit : dist_unit) : dist_unit value = if from_unit == to_unit then (to_unit, val_) else match from_unit with | Foot -> if to_unit == Meter then (Meter, val_ *. 0.3048) else (Mile, val_ /. 5280.0) | Meter -> if to_unit == Foot then (Foot, val_ /. 0.3048) else (Mile, val_ /. 1609.344) | Mile -> if to_unit == Meter then (Meter, val_ *. 1609.344) else (Foot, val_ *. 5280.0) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) (to_unit : speed_unit) : speed_unit value = if from_unit == to_unit then (to_unit, val_) else let (dist: dist_unit value) = convert_dist (fst(from_unit), val_) (fst(to_unit)) in let (time: time_unit value) = convert_time (snd(from_unit), 1.0) (snd(to_unit)) in ((fst(dist), fst(time)), (snd(dist) /. snd(time))) ;; |
let dist_traveled (time : time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = if fst(time) == snd(speed_unit) then (fst(speed_unit), snd(time) *. speed_val) else let (new_time : time_unit value) = convert_time (time) (snd(speed_unit)) in (fst(speed_unit), snd(new_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 | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> if hd=cur_el then aux tl (hd, cur_num+1) (max_el, max_num) else if cur_num>max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in let l' = List.sort compare l in match l' with | [] -> failwith "empty list" | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let (_ :: l1) = l in let (_ :: l2) = List.rev l in if List.length l = 0 then failwith "empty list" else if List.length l= 1 then failwith "only one value" else mode (List.combine (List.rev l2) l1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Hour -> (Hour, val_ /. 3600.0) | Second -> (Second, val_ )) | 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 | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.0) | Foot -> (Foot, val_) ) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Mile -> (Mile, (val_/.5280.0)/. 0.3048) | Meter -> (Meter, val_) ) | Mile -> (match to_unit with | Meter -> (Meter, (val_ *. 5280.0)*.0.3048) | Foot -> (Foot, val_*.5280.0) | Mile -> (Mile, val_) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (f1 : dist_unit) = fst(from_unit) in let (f2 : time_unit) = snd(from_unit) in let (t1 : dist_unit) = fst(to_unit) in let (t2: time_unit)= snd(to_unit) in ((t1, t2) ,(snd(convert_dist (f1, val_) t1) /. snd(convert_time(f2, 1.0) t2))) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match snd(speed_unit) with | Second -> (match fst(time) with | Second -> fst(speed_unit), (speed_val *. snd(time) ) | Hour -> fst(speed_unit), (speed_val *.snd(convert_time time (snd(speed_unit)))) ) | Hour -> (match fst(time) with | Hour -> fst(speed_unit), (speed_val *. snd(time) ) | Second -> fst(speed_unit), (speed_val *. snd(convert_time time (snd(speed_unit))) ) ) ;; let rec passes_da_vinci t = notimplemented () ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | h :: t -> if h == cur_el then aux t (h, 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 h::t = List.sort compare l in aux t (h, 1) (h, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pairs (l: 'a list) (pair_list : ('a * 'a) list) = match l with | [] -> pair_list | h :: t -> match t with | [] -> pair_list | s :: d -> pairs d ((h, s) :: pair_list) in let list = List.concat [pairs l [] ; pairs (List.tl l) []] in mode list ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.