text
stringlengths 0
601k
|
---|
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let speed_dist, speed_time = speed_unit in let _, converted_time = convert_time time speed_time in (speed_dist, converted_time *. speed_val) ;; let sum_squares (l: tree list): float = List.fold_left (fun acc t -> match t with | Leaf -> acc | Branch (node, _) -> node *. node +. acc ) 0. l let rec passes_da_vinci t = match t with | Leaf -> true | Branch(node, children) -> let area = node *. node in let ss = sum_squares children in area >= ss && List.for_all passes_da_vinci children ;; |
let mode (l: 'a list) : 'a = let rec aux (l: 'a list) ((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 | el :: rest -> if el = cur_el then aux rest (el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux rest (el, 1) (cur_el, cur_num) else aux rest (el, 1) (max_el, max_num) in let el1 :: rest = List.sort compare l in aux rest (el1, 1) (el1, 1) ;; let rec pair_maker (l: 'a list) = match l with | el1 :: el2 :: rest -> (el1, el2) :: (pair_maker (el2 :: rest)) | _ -> [] ;; |
let pair_mode (l: 'a list) : 'a * 'a = let pairs = List.sort compare (pair_maker l) in mode 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 | Second -> (Hour, val_ /. 3600.0) | Hour -> (Second, 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, 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_ /. 0.3048 /. 5280.0) | (Mile, Foot) -> (to_unit, val_ *. 5280.0) | (Mile, Meter) -> (to_unit, val_ *. 5280.0 *. 0.3048) ;; |
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 (a, time_conversion_factor) = convert_time (from_time, 1.0) to_time in let (b, dist_conversion_factor) = convert_dist (from_dist, 1.0) to_dist in (to_unit, val_ *. dist_conversion_factor /. time_conversion_factor) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (speed_dist_unit, speed_time_unit) = speed_unit in let (time_new_unit, time_new_val) = convert_time time speed_time_unit in (speed_dist_unit, speed_val *. time_new_val) ;; let rec passes_da_vinci t = let rec compute_branch_children (t : tree) : float = match t with | Leaf -> 0.0 | Branch(width, tree_list) -> let width_list = List.map compute_branch_children tree_list in width *. width +. List.fold_left (+.) 0.0 width_list in match t with | Leaf -> true | Branch(width, tree_list) -> ((2.0 *. width *. width) >= compute_branch_children t ) && List.for_all passes_da_vinci tree_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 | x :: xs when x = cur_el -> aux xs (cur_el, (cur_num + 1)) (max_el, max_num) | y :: xs -> if cur_num > max_num then aux xs (y, 1) (cur_el, cur_num) else aux xs (y, 1) (max_el, max_num) | _ -> if cur_num > max_num then cur_el else max_el in let rec get_first (l: 'a list) : 'a = let x::xs = l in x in match l with | [] -> failwith "empty list" | _ -> aux (List.sort compare (l)) (get_first l, 0) (get_first l, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper (l: 'a list) (acc: ('a * 'a) list) : 'a * 'a = match l with | x :: y :: xs -> helper (y :: xs) ((x,y)::acc) | _ -> mode acc in match l with | x :: y :: [] -> (x, y) | y :: [] -> failwith "list is too small" | _ -> helper l [] ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Hour, Second -> (to_unit, val_ *. 3600.) | Second, Hour -> (to_unit, val_ /. 3600.) | _ -> (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (to_unit, val_ *. 0.3048) | Foot, Mile -> (to_unit, val_ /. 5280.) | Meter, Foot -> (to_unit, val_ /. 0.3048) | Meter, Mile -> (to_unit, val_ /. 0.3048 /. 5280.) | Mile, Foot -> (to_unit, val_ *. 5280.) | Mile, Meter ->(to_unit, val_ *. 5280. *. 0.3048) | _ -> (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((x, y), (w, z)) = (from_unit, to_unit) in let (a, b) = convert_dist (x, val_) (w) in let (c, d) = convert_time (y, 1. /. b) (z) in to_unit, 1. /. d ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (x, y) = (speed_unit) in let (a, b) = convert_time time y in x, (b *. speed_val) ;; let rec passes_da_vinci t = let rec sum t acc = match t with | Leaf :: x -> sum x acc | (Branch (x, a)) :: b -> sum b (acc +. (x ** 2.) +. sum a (0.)) | [] -> acc in match t with | Leaf -> true | Branch (x, []) -> true | Branch (x, y) -> (sum y 0.) <= (x ** 2.) ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd::tl -> if hd = cur_el then if cur_num > max_num then aux tl (cur_el, cur_num + 1) (cur_el, cur_num) else aux tl (hd, cur_num + 1) (max_el, max_num) else aux tl (hd, 1) (max_el, max_num) in let sorted_l = List.sort compare l in match List.sort compare sorted_l with | hd::tl -> aux sorted_l (hd, 0) (hd, 0) | _ -> failwith "Invalid input." ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> failwith "Invalid input." | _::tl -> let l1 = tl in match (List.rev l) with | _::tl -> let l2 = List.rev tl in let l_pairs = List.combine l2 l1 in mode l_pairs | _ -> failwith "Invalid input." ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> if to_unit = Second then (Second, val_ *. 3600.) else (Hour, val_) | Second -> if to_unit = Hour then (Hour, 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 = Meter then (Meter, val_ *. 0.3048) else if to_unit = Mile then (Mile, val_ /. 5280.) else (Foot, val_) | Meter -> let meterf = val_ /. 0.3048 in if to_unit = Foot then (Foot, meterf) else if to_unit = Mile then (Mile, meterf /. 5280.) else (Meter, val_) | Mile -> let milef = val_ *. 5280. in if to_unit = Foot then (Foot, milef) else if to_unit = Meter then (Meter, milef *. 0.3048) else (Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist_unit = fst(from_unit) in let from_time_unit = snd(from_unit) in let to_dist_unit = fst(to_unit) in let to_time_unit = snd(to_unit) in let dist = convert_dist (from_dist_unit, val_) to_dist_unit in let new_dist = snd(dist) in let time = convert_time (to_time_unit, val_) from_time_unit in let new_val = new_dist /. snd(time) in if from_time_unit != to_time_unit && from_dist_unit = to_dist_unit then (to_unit, snd(time)) else if from_dist_unit != to_dist_unit && from_time_unit = to_time_unit then (to_unit, new_dist) else if from_time_unit != to_time_unit && from_dist_unit != to_dist_unit then let new_time = convert_time (to_time_unit, new_dist) from_time_unit in (to_unit, snd(new_time)) else (to_unit, val_) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_val = snd(a) in let a_unit = fst(a) in let add_val = a_val +. b_val in if a_unit = b_unit then (b_unit, add_val) else let new_speed = convert_speed (fst(a),snd(a)) b_unit in let add_val2 = snd(new_speed) +. b_val in (b_unit, add_val2) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. width ** 2.) in let rec check l = match l with | [] -> true | Leaf :: _ -> true | Branch (width, subtree) :: tl -> if not ((check subtree) && ((sum_ subtree 0.) <= (width ** 2.))) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = notimplemented () in notimplemented () ;; |
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Hour, Second) -> (to_unit, 3600. *. val_) | (Second, Hour) -> (to_unit, val_ /. 3600.) | (_, _) -> (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_ *. 0.00018939393) | (Meter, Foot) -> (to_unit, val_ *. 3.28083989501) | (Meter, Mile) -> (to_unit, val_ *. 0.00062137119) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Mile, Meter) -> (to_unit, val_ *. 1609.344) | (_, _) -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, (snd (convert_dist (fst from_unit, snd (convert_time (snd to_unit, val_) (snd from_unit))) (fst to_unit))) ) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = ((fst speed_unit), snd (convert_time time (snd speed_unit)) *. speed_val) ;; let rec passes_da_vinci t = notimplemented () ;; |
let mode (l: 'a list) : 'a = let list1 = (List.sort compare l) in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num>max_num then cur_el else max_el | 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 aux list1 (List.hd list1,0) (List.hd list1,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_aux l acc = match l with | []-> acc | h::t -> if (t!=[]) then pair_aux t ((h,(List.hd t))::acc) else pair_aux t acc in mode (List.rev (pair_aux (l) [])) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_=0. then (to_unit, 0.) else 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 = if val_=0. then (to_unit,0.) else (if from_unit = to_unit then (from_unit, val_) else match from_unit with | Foot -> if to_unit=Meter then (Meter,val_ *. 0.3048) else (Mile, val_ *. 0.000189393939) | Meter -> if to_unit=Foot then (Foot,val_ *. 3.2808399) else (Mile, val_ *.0.000621371192) | Mile -> if to_unit=Foot then (Foot,val_ *.5280.) else (Meter,val_*.1609.344)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (a,b) = from_unit in let (c,d) = to_unit in let (_,temp2) = (convert_dist (a,val_) c) in match (b,d) with | (Second,Hour) -> (to_unit, temp2*.3600.) | (Hour,Second) -> (to_unit, temp2 /.3600.) | (_,_) -> (to_unit, temp2) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (temp1,target_unit) = speed_unit in let (temp3,temp4) = time in match (target_unit,temp3) with | (Second,Hour) -> (temp1 ,speed_val*.temp4 *.3600.) | (Hour,Second) -> (temp1, speed_val *. temp4 /. 3600.) |(_,_) -> (temp1, temp4*.speed_val) ;; let rec passes_da_vinci t = let sumup a b = match b with | Leaf -> a | Branch(c,_) -> a+.(c*.c) in let l_sumup l = List.fold_left sumup 0. l in let find_false a = if a=false then true else false in match t with | Leaf -> true | Branch(a,b) -> (a*.a>=(l_sumup b)) && ((List.exists find_false (List.map (passes_da_vinci) b))=false) ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "Invalid input." else ( let sorted_list = List.sort compare (l) in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> ( if cur_num > max_num then cur_el else max_el ) | hd :: tl -> ( if hd = cur_el then ( if cur_num > max_num then aux tl (cur_el, cur_num + 1) (cur_el, cur_num + 1) else 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 aux sorted_list (List.hd (sorted_list), 0) (List.hd (sorted_list), 0) ) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length (l) < 2 then failwith "Invalid input." else ( let (l_one, l_two) = (l, l) in let (l1, l2) = (List.tl (l_one), List.rev (List.tl (List.rev (l_two)))) in let list_of_pairs = List.combine l2 l1 in mode list_of_pairs ) ;; |
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 = match from_unit with | Foot -> (match to_unit with | Meter -> (Meter, 0.3048 *. val_) | Mile -> (Mile, val_ /. 5280.) | Foot -> (Foot, val_) ) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Meter -> (Meter, val_) ) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 0.3048 *. 5280.) | Mile -> (Mile, val_) ) ;; let first_element (pair : 'a * 'b) : 'a = let (first, _) = pair in first ;; let second_element ((a, b) : 'a * 'b) : 'b = b ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist_unit, time_unit) = from_unit in (to_unit, (second_element (convert_dist (dist_unit, val_) (first_element to_unit))) /. (second_element (convert_time (time_unit, 1.) (second_element to_unit)))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, second_element (convert_speed a b_unit) +. b_val) ;; |
let passes_da_vinci t = check [t] ;; |
let mode (l: 'a list) : 'a = if List.length l < 1 then failwith "List cannot be empty" else let l_sorted = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> ( if cur_num > max_num then cur_el else max_el ) | hd::tl -> ( if hd > cur_el 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.tl l_sorted) (List.hd l_sorted, 1) (List.hd l_sorted, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List must have at least 2 elements" else let generate_pairs (l: 'a list): ('a * 'a) list = let rec aux (acc: ('a * 'a) list) (src: 'a list) (current: 'a) : ('a * 'a) list = match src with | [] -> acc | _ -> ( let next: 'a = List.hd src in aux (acc@[(current, next)]) (List.tl src) next ) in aux ([]) (List.tl l) (List.hd l) in let pairs = generate_pairs l in mode (pairs) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | (Second, Second) | (Hour, Hour) -> (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (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) | (Meter, Meter) | (Mile, Mile) | (Foot, Foot) -> (to_unit, 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 (d, dist_factor) = convert_dist (from_dist, 1.) to_dist in let (t, time_factor) = convert_time (from_time, 1.) to_time in ((d, t), (dist_factor /. time_factor) *. val_) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (speed_dist_unit, speed_time_unit) = speed_unit in let (_, converted_time) = convert_time time speed_time_unit in (speed_dist_unit, converted_time *. speed_val) ;; let rec passes_da_vinci t = let get_width_sq (tr: tree) : float = match tr with | Branch (w, _) -> w ** 2. | Leaf -> 0. in let rec sum_subtree (acc: float) (branches: tree list) = match branches with | hd::tl -> sum_subtree (acc +. (get_width_sq hd)) tl | [] -> acc in let rec check_subtrees (branches: tree list) : bool = match branches with | hd::tl -> (passes_da_vinci hd) && (check_subtrees tl) | [] -> true in match t with | Leaf -> true | Branch (width, branches) -> ( let children_w_sq = sum_subtree 0. branches in width ** 2. >= children_w_sq && (check_subtrees branches) ) ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el then aux xs (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "invalid input" | x::xs -> aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l = [] then failwith "invalid input" else let remove l = match l with | [] -> [] | _::xs -> xs in let l1 = l in let l2 = l in let l2_r = List.rev (remove (List.rev l2)) in let l1_r = remove (l1) in let c = List.combine l2_r l1_r in mode c ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if to_unit = from_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 to_unit = from_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 dist1, time1 = from_unit in let dist2, time2 = to_unit in let dist = convert_dist (dist1, val_) dist2 in let time = convert_time (time1, 1.) time2 in (to_unit, snd(dist) /. snd(time)) ;; |
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, _) :: 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. <= width *. width))) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let l = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h::t -> if h = cur_el 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 t (cur_el, cur_num) (max_el, max_num) else aux t (cur_el, cur_num) (max_el, max_num) else let cur_el = h in let cur_num = 1 in aux t (cur_el, cur_num) (max_el, max_num) in let cur_el::rest = l in aux rest (cur_el, 1) (cur_el, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec make_pair l = match l with | x::xs::[] -> (x,xs)::[] | x::y::ys -> (x,y)::make_pair (y::ys) in let l = make_pair l in mode l ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second,Hour) -> (Hour,val_ /. (3600.)) | (Hour,Second) -> (Second,val_ *. (3600.)) | (Second,Second) -> (Second,val_) | (Hour,Hour) -> (Hour,val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot,Meter) -> (Foot,val_ *. (0.3048)) | (Foot,Mile) -> (Foot, val_ /. (5280.)) | (Foot,Foot) -> (Foot, val_) | (Meter,Foot) -> (Meter, val_ /. (0.3048)) | (Meter,Meter) -> (Meter, val_) | (Meter,Mile) -> (Meter,val_ /. (1609.344)) | (Mile,Foot) -> (Mile, val_ *. (5280.)) | (Mile,Meter) -> (Mile, val_ *. (1609.344)) | (Mile,Mile) -> (Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (x1,y1) = from_unit in let (x2,y2) = to_unit in let value = convert_dist(x1,val_) x2 in let (_,value) = value in let value = convert_time(y2, value) y1 in let (_,value) = value in (to_unit, value) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (u,t) = time in let (di,ti) = speed_unit in let ti = convert_time(u,t) ti in let (_,t) = ti in let dist = t *. speed_val in (di,dist) ;; let rec passes_da_vinci t = let rec sum_squares list acc = match list with | [] -> acc | x::xs -> if x = Leaf then acc+.0. else let Branch (value,_) = x in sum_squares xs (acc +. value*.value) in match t with | Leaf -> true | Branch (x,y) -> let sum = sum_squares y 0. in let x = x*.x in if (x>=sum) then let (x::xs) = y in passes_da_vinci x else false;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "Undefined output" else let sortedList = 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 max_num >= cur_num then max_el else cur_el |hd::tl -> if cur_el == hd 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 aux sortedList (List.hd (sortedList), 0) (List.hd (sortedList), 1) ;; let helpercopy l = let rec helper l acc = match l with |[] -> List.rev acc |hd :: tl -> helper tl (List.cons hd acc) in helper l [] ;; |
let pair_mode (l: 'a list) = if l == [] then failwith "This list is empty" else if (List.length l) < 2 then failwith "This list is too small" else let newlist = List.tl (helpercopy l) in let newlist2 = List.rev (List.tl (List.rev (helpercopy l))) in let rec aux2 l ((cur_el, cur_num)) ((max_el, max_num)) = match l with |[] -> if max_num >= cur_num then max_el else cur_el |hd::tl -> if fst(cur_el) == fst(hd) && snd(cur_el) == snd(hd) then aux2 tl (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux2 tl (hd, 1) (cur_el, cur_num) else aux2 (List.tl l) (hd,1) (max_el, max_num) in aux2 (List.sort compare (List.combine newlist2 newlist)) (List.hd (List.sort compare (List.combine newlist2 newlist)), 0) (List.hd (List.sort compare (List.combine newlist2 newlist)),0) ;; |
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, to_unit with |Second, Hour -> (Second, val_ /. 3600.) |Hour, Second -> (Hour,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, to_unit with |Foot, Meter -> (Foot, val_ *. 0.3048) |Foot, Mile -> (Foot, val_ /. 5280.) |Meter, Mile -> (Meter, val_ /. 0.3048 /. 5280.) |Meter, Foot -> (Meter, val_ /. 0.3048) |Mile, Meter -> (Mile,val_ *. 5280. *. 0.3048) |Mile, Foot -> (Mile, val_ *. 5280.) ;; |
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 ((dist1,time1),(dist2,time2)) = (from_unit, to_unit) in (to_unit, (snd(convert_dist (dist1, val_) dist2)) /. (snd(convert_time (time1, 1.) time2)) ) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if fst (a) == b_unit then (b_unit, (snd (a)) +. b_val) else let (x,y) = ((fst a), b_unit) in (b_unit, (snd(convert_speed (a) b_unit)) +. b_val) ;; |
let passes_da_vinci t = let rec helper tree = match issumofsquaregood tree, tree with |true, Branch(x,hd::hd2::_) -> (match hd with |Branch(_,_) -> if (helper hd) == true then (helper hd2) else false |Leaf -> true) |true, Branch(_,[hd]) -> (match hd with |Branch(x,y) -> helper (Branch(x,y)) |Leaf -> true) |(true, Leaf) -> true |(false, _) -> false in helper t ;; |
let mode (l: 'a list) : 'a = let l = List.sort compare (l) in ( let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> max_el |x::xs -> if x = cur_el then if cur_num + 1 > max_num then aux xs (x, cur_num + 1) (x, cur_num + 1) else aux xs (x, cur_num + 1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in match l with |[] -> failwith "emptylist" |x::xs -> aux xs (x,1) (x,1) ) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.rev(List.tl(List.rev l)) in let l2 = List.tl(l) in mode(List.combine l1 l2) ;; |
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,Foot -> (Foot, val_) |Meter,Meter -> (Meter, val_) |Mile,Mile -> (Mile,val_) |Foot,Meter -> (Meter, val_ *. 0.3048) |Foot,Mile -> (Mile, val_ /. 5280.) |Meter,Foot -> (Foot, val_ /. 0.3048) |Meter,Mile -> (Mile, ((val_ /. 0.3048) /. 5280.)) |Mile,Foot -> (Foot, val_ *. 5280.) |Mile,Meter -> (Meter, ((val_ *. 5280.) *. 0.3048)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((d1,t1),(d2,t2)) = (from_unit,to_unit) in (let x = convert_dist (d1, val_) d2 in ( let (unit,value) = x in (let y = convert_time (t2, value) t1 in ( let (uni,valz) = y in ((d2,t2),valz) ) ) ) ) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (t1, v1), ((d2,t2),v2) = time, (speed_unit, speed_val) in ( let (newtime, newval) = convert_time (t1,v1) t2 in ( (d2,newval *. v2) ) ) ;; let rec sumsq_helper (list_of_trees: tree list) sum: float = match list_of_trees with |[] -> sum |Leaf::xs -> sumsq_helper xs sum |Branch(val_,l)::xs -> sumsq_helper xs (sum +. val_ *. val_) ;; let rec rec_helper (tl: tree list) = match tl with |[] -> true |Leaf::xs -> rec_helper xs |Branch(val_,l)::xs -> (sumsq_helper l 0. <= val_ *. val_) && rec_helper xs && rec_helper l ;; let rec passes_da_vinci t = rec_helper [t] ;; |
let mode (l: 'a list) : 'a = let l = List.sort compare (l) in ( let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> max_el |x::xs -> if x = cur_el then if cur_num + 1 > max_num then aux xs (x, cur_num + 1) (x, cur_num + 1) else aux xs (x, cur_num + 1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in match l with |[] -> failwith "emptylist" |x::xs -> aux xs (x,1) (x,1) ) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.rev(List.tl(List.rev l)) in let l2 = List.tl(l) in mode(List.combine l1 l2) ;; |
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,Foot -> (Foot, val_) |Meter,Meter -> (Meter, val_) |Mile,Mile -> (Mile,val_) |Foot,Meter -> (Meter, val_ *. 0.3048) |Foot,Mile -> (Mile, val_ /. 5280.) |Meter,Foot -> (Foot, val_ /. 0.3048) |Meter,Mile -> (Mile, ((val_ /. 0.3048) /. 5280.)) |Mile,Foot -> (Foot, val_ *. 5280.) |Mile,Meter -> (Meter, ((val_ *. 5280.) *. 0.3048)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((d1,t1),(d2,t2)) = (from_unit,to_unit) in (let x = convert_dist (d1, val_) d2 in ( let (unit,value) = x in (let y = convert_time (t2, value) t1 in ( let (uni,valz) = y in ((d2,t2),valz) ) ) ) ) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (((d1,t1),v1),((d2,t2),v2)) = (a,(b_unit,b_val)) in ( let ((dx,tx),vx) = convert_speed((d1,t1),v1) (d2,t2) in ( ((d2,t2),v2 +. vx) ) ) ;; |
let passes_da_vinci t = rec_helper [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd :: tl -> if cur_num = 0 then aux tl (hd, 1) (hd, 1) else if hd = cur_el then if (cur_num + 1) > max_num then aux tl (hd, (cur_num + 1)) (cur_el, cur_num + 1) else aux tl (hd, (cur_num + 1)) (max_el, max_num) else aux tl (hd, 1) (max_el, max_num) | _ -> max_el in match l with | [] -> failwith "undefined input!!! DINGUS" | hd :: tl -> aux (List.sort compare (l)) (hd,0) (hd,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec tuple_maker l m ((last_el, x1) : 'a * bool) = match l with | [] -> m | hd :: tl -> if not x1 then tuple_maker tl m (hd, true) else tuple_maker tl ((last_el, hd) :: m) (hd, true) | _ -> m in if List.length l < 2 then failwith "the list isn't even long enough" else match l with | [] -> failwith "there's nothing 'ere!" | hd :: tl -> mode (tuple_maker l [] (hd, false)) | _ -> failwith "something messed up" ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) | _ -> failwith "wrong type" ) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) | _ -> failwith "wrong type") | _ -> failwith "wrong type" ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) | _ -> failwith "wrong type") | Meter -> (match to_unit with | Foot -> (Foot, val_ *. (1. /. 0.3048)) | Meter -> (Meter, val_) | Mile -> (Mile, (val_ *. (1. /. 0.3048)) /. 5280.) | _ -> failwith "wrong type") | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, (val_ *. 5280.) *. 0.3048) | Mile -> (Mile, val_) | _ -> failwith "wrong type") | _ -> failwith "wrong type" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (Foot, Second) -> ( match to_unit with | (Foot, Second) -> ((Foot, Second), val_) | (Foot, Hour) -> ((Foot, Hour), snd (convert_time (Hour, val_) Second)) | (Meter, Second) -> ((Meter, Second), snd (convert_dist (Foot, val_) Meter)) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist(Foot, (snd (convert_time(Hour, val_) (Second)))) (Meter))) | (Mile, Second) -> ((Mile, Second), snd (convert_dist(Foot, (snd (convert_time(Second, val_) (Second)))) (Mile))) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist(Foot, (snd (convert_time(Hour, val_) (Second)))) (Mile))) | _ -> failwith "Error in to_unit") | (Foot, Hour) -> ( match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist(Foot, (snd (convert_time(Second, val_) (Hour)))) (Foot))) | (Foot, Hour) -> ((Foot, Hour), val_) | (Meter, Second) -> ((Meter, Second), snd (convert_dist(Foot, (snd (convert_time(Second, val_) (Hour)))) (Meter))) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist(Foot, (snd (convert_time(Hour, val_) (Hour)))) (Meter))) | (Mile, Second) -> ((Mile, Second), snd (convert_dist(Foot, (snd (convert_time(Second, val_) (Hour)))) (Mile))) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist(Foot, (snd (convert_time(Hour, val_) (Hour)))) (Mile))) | _ -> failwith "Error in to_unit") | (Meter, Second) -> ( match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist(Meter, (snd (convert_time(Second, val_) (Second)))) (Foot))) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist(Meter, (snd (convert_time(Hour, val_) (Second)))) (Foot))) | (Meter, Second) ->((Meter, Second), val_) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist(Meter, (snd (convert_time(Hour, val_) (Second)))) (Meter))) | (Mile, Second) -> ((Mile, Second), snd (convert_dist(Meter, (snd (convert_time(Second, val_) (Second)))) (Mile))) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist(Meter, (snd (convert_time(Hour, val_) (Second)))) (Mile))) | _ -> failwith "Error in to_unit") | (Meter, Hour) -> ( match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist(Meter, (snd (convert_time(Second, val_) (Hour)))) (Foot))) | (Foot, Hour) ->((Foot, Hour), snd (convert_dist(Meter, (snd (convert_time(Hour, val_) (Hour)))) (Foot))) | (Meter, Second) -> ((Meter, Second), snd (convert_dist(Meter, (snd (convert_time(Second, val_) (Hour)))) (Meter))) | (Meter, Hour) -> ((Meter, Hour), val_) | (Mile, Second) -> ((Mile, Second), snd (convert_dist(Meter, (snd (convert_time(Second, val_) (Hour)))) (Mile))) | (Mile, Hour) -> ((Mile, Hour), snd (convert_dist(Meter, (snd (convert_time(Hour, val_) (Hour)))) (Mile))) | _ -> failwith "Error in to_unit") | (Mile, Second) -> ( match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist(Mile, (snd (convert_time(Second, val_) (Second)))) (Foot))) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist(Mile, (snd (convert_time(Hour, val_) (Second)))) (Foot))) | (Meter, Second) -> ((Meter, Second), snd (convert_dist(Mile, (snd (convert_time(Second, val_) (Second)))) (Meter))) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist(Mile, (snd (convert_time(Hour, val_) (Second)))) (Meter))) | (Mile, Second) -> ((Mile, Second), val_) | (Mile, Hour) -> ((Meter, Hour), snd (convert_dist(Mile, (snd (convert_time(Hour, val_) (Second)))) (Mile))) | _ -> failwith "Error in to_unit") | (Mile, Hour) -> ( match to_unit with | (Foot, Second) -> ((Foot, Second), snd (convert_dist(Mile, (snd (convert_time(Second, val_) (Hour)))) (Foot))) | (Foot, Hour) -> ((Foot, Hour), snd (convert_dist(Mile, (snd (convert_time(Hour, val_) (Hour)))) (Foot))) | (Meter, Second) -> ((Meter, Second), snd (convert_dist(Mile, (snd (convert_time(Second, val_) (Hour)))) (Meter))) | (Meter, Hour) -> ((Meter, Hour), snd (convert_dist(Mile, (snd (convert_time(Hour, val_) (Hour)))) (Meter))) | (Mile, Second) -> ((Mile, Second), snd (convert_dist(Mile, (snd (convert_time(Second, val_) (Hour)))) (Mile))) | (Mile, Hour) -> ((Mile, Hour), val_) | _ -> failwith "Error in to_unit") | _ -> failwith "Error in from_unit" ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit, (snd (convert_time time (snd speed_unit))) *. speed_val) ;; let rec sub_sum branch sum = match branch with | [] -> sum | Leaf::tl -> sub_sum tl sum | Branch(value, sub)::tl -> sub_sum tl (sum +. (value *. value)) let rec tree_sum tree = match tree with | [] -> true | Leaf :: tl -> tree_sum tl | Branch(value, subs) :: tl -> if (tree_sum subs && ((sub_sum subs 0.) <= (value *. value))) then tree_sum tl else false let rec passes_da_vinci t = tree_sum [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=cur_el then if cur_num+1 > max_num then aux t (h, cur_num+1) (h, cur_num+1) else aux t (h, cur_num+1) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in match List.sort compare (l) with | [] -> failwith "Empty list" | h::t -> (aux t (h, 1) (h, 1)) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let remove_first l = match l with | [] -> [] | h::t -> t in let remove_last l = match List.rev l with | [] -> [] | h::t -> List.rev t in if List.length l <= 1 then failwith "List must contain at least two elements" else mode(List.combine (remove_last(l)) (remove_first(l))) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if to_unit = Second then (Second, val_) else (Hour, val_ /. 3600.0) | Hour -> if to_unit = Hour then (Hour, val_) else (Second, val_ *. 3600.0) ;; |
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.0) | Meter -> if to_unit = Foot then (Foot, val_ /. 0.3048) else if to_unit = Meter then (Meter, val_) else (Mile, val_ /. 1609.344) | Mile -> if to_unit = Foot then (Foot, val_ *. 5280.0) else if to_unit = Meter then (Meter, val_ *. 1609.344) else (Mile, val_) ;; |
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.0)(to_time))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. snd(convert_speed(fst(a), snd(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**2.0) in let rec check l = match l with | [] -> true | Leaf::tl -> check tl | Branch(width, subtree)::tl -> if sum_ (subtree)(0.0) > width**2.0 then false else if 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) = match l with | [] -> max_el | head :: tail -> if head = cur_el then if compare cur_num max_num = 0 then aux tail (cur_el, cur_num + 1) (cur_el, cur_num + 1) else aux tail (cur_el, cur_num + 1) (max_el, max_num) else aux tail (head, 1) (max_el, max_num) in let l = List.sort compare l in match l with | [] -> raise Domain | x :: xs -> aux xs (x,1) (x,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l (acc: 'b list) (head: 'a) = match l with | [] -> mode acc | y :: ys -> let new_l = acc @ [(head, y)] in helper ys new_l y in match l with | [] -> raise Domain | _ :: [] -> raise Domain | x :: xs -> helper xs [] x ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match ((from_unit, val_), to_unit) with | ((Second, _), Second) -> (Second, val_) | ((Second, x), Hour) -> (Hour, x /. 3600.) | ((Hour, x), Second) -> (Second, x *. 3600.) | ((Hour, _), Hour) -> (Hour, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match ((from_unit, val_), to_unit) with | ((Foot, _), Foot) -> (Foot, val_) | ((Foot, x), Meter) -> (Meter, x *. 0.3048) | ((Foot, x), Mile) -> (Mile, x /. 5280.) | ((Meter, x), Foot) -> (Foot, x /. 0.3048) | ((Meter, _), Meter) -> (Meter, val_) | ((Meter, x), Mile) -> (Mile, x /. 0.3048 /. 5280.) | ((Mile, x), Foot) -> (Foot, x *. 5280.) | ((Mile, x), Meter) -> (Meter, x *. 5280. *. 0.3048) | ((Mile, _), Mile) -> (Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (((from_d, from_t), x), (to_d, to_t)) = ((from_unit, val_), to_unit) in let (_, d) = convert_dist (from_d, x) to_d in let (_, t) = convert_time (from_t, 1.) to_t in ((to_d, to_t), d /. t) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (time_unit, time_val) = time in let (d_unit, t_unit) = speed_unit in let (_, a_val) = convert_time (time_unit, time_val) t_unit in (d_unit, a_val *. speed_val) ;; let sum_widths (t_list: tree list): float = let rec sum2 (trees: tree list) (acc: float) = match trees with | [] -> acc | t :: ts -> match t with | Leaf -> sum2 ts (acc +. 0.) | Branch(val_, _) -> sum2 ts (acc +. (val_ *. val_)) in sum2 t_list 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch(p, t_list) -> let sum = sum_widths t_list in sum <= p *. p && let rec iter (trees: tree list): bool = match trees with | [] -> true | t :: ts -> passes_da_vinci t && iter ts in iter t_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 | [] -> max_el | b :: bs -> if b == cur_el then if cur_num+1 > max_num then aux bs (b, cur_num+1) (b, cur_num+1) else aux bs (cur_el,cur_num+1) (max_el,max_num) else aux bs (b,1) (max_el, max_num) in let x::xs = List.sort compare l in aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pairs (e : ('a * 'a) list) (l: 'a list) : ('a *'a) list = match l with | x :: [] -> e | x :: y :: [] -> pairs (((x,y) :: e)) (y :: []) | x :: y :: qs -> pairs (((x,y) :: e)) (y :: qs) in mode (pairs [] l); ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Hour -> if to_unit == Second then (Second, 3600. *. val_) else (Hour,val_); |Second -> if to_unit == Hour then (Hour, (1./.3600. *.val_)) else (Second,val_) ;; let m_mile = 0.3048*.5289.;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.