text
stringlengths 0
601k
|
---|
let mode (l: 'a list) : 'a= match l with | [] -> failwith "Undefined input." | _ -> 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 | [] -> List.hd(l) | x::xs -> aux xs (x, 1) (x, 0) ;; let rec len (l: 'a list) : int = match l with | [] -> 0 | x::xs -> 1 + len xs;; let rec separate xs = match xs with | x::y::tail -> let a,b = separate tail in x::a, y::b | x::[] -> [x],[] | [] -> [],[];; let rec remove_at n = function | [] -> [] | h :: t -> if n = 0 then t else h :: remove_at (n - 1) t;; let rec merge a b = match (a,b) with | (x::xs), (y::ys) -> x::y::merge xs ys | xs,[] -> xs | [],ys -> ys;; |
let pair_mode (l: 'a list) : 'a * 'a= let lenofl = len l in match lenofl with | 0 -> failwith "Undefined input." | 1 -> failwith "Undefined input." | _ -> if lenofl mod 2 =1 then let getFirst (a,b) =a in let getSecond (a,b) =b in let tup1 = separate l in let sub1 = getFirst (tup1) in let sub2 = getSecond (tup1) in let temp = sub1 in let sub1 = remove_at 0 sub1 in let final1 = List.combine sub2 sub1 in let temp = remove_at (len temp -1) temp in let final2 = List.combine temp sub2 in let final = merge final1 final2 in mode final else let getFirst (a,b) =a in let getSecond (a,b) =b in let temp = l in let tup1 = separate l in let sub1 = getFirst (tup1) in let sub2 = getSecond (tup1) in let final1 = List.combine sub1 sub2 in let temp = remove_at 0 temp in let temp = remove_at (lenofl-2) temp in let tup2 = separate temp in let subbb1 = getFirst (tup2) in let subbb2 = getSecond (tup2) in let final2 = List.combine subbb1 subbb2 in let final = merge final1 final2 in mode final;; |
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;;*);; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> if to_unit= Second then ( to_unit , val_) else ( to_unit ,val_ /. 3600. ) | Hour -> if to_unit = Hour then ( to_unit ,val_) else ( to_unit ,val_ *. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit= Meter then ( to_unit ,val_ *. 0.3048 ) else if to_unit = Mile then ( to_unit ,val_ /. 5280.) else ( to_unit ,val_) | Meter -> if to_unit = Foot then ( to_unit ,val_ /. 0.3048) else if to_unit = Mile then ( to_unit ,val_ *. 0.000621371192) else ( to_unit ,val_) | Mile -> if to_unit = Meter then ( to_unit ,val_ *. 1609.344) else if to_unit = Foot then ( to_unit ,val_ *. 5280.) else ( to_unit ,val_) ;; let get_tupleb(_,b) = b;; let get_tuplea(a,_) = a;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_time = get_tupleb from_unit in let tup1 = to_unit in let to_time = get_tupleb tup1 in let converted = convert_time (to_time, 1.) from_time in let converted_timename = get_tupleb converted in let time_frac = get_tupleb converted in let converted_dis = time_frac *. val_ in let ori_dis = get_tuplea from_unit in let converted_disname = get_tuplea (to_unit) in let final = convert_dist (get_tuplea from_unit, converted_dis) converted_disname in let final_num = get_tupleb final in (( to_unit), final_num) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let final_timename = get_tupleb speed_unit in let numconvertedtime = get_tupleb(convert_time time final_timename) in let ori_dis = numconvertedtime *. speed_val in let final_disname = get_tuplea speed_unit in final_disname, ori_dis;; let rec sum_up work tree = match tree with | Leaf -> work | Branch (w,trees) ->List.fold_left sum_up (work +. w *. w) trees;; let rec traverse3 work tree = match tree with | Leaf -> true | Branch (w,trees) -> let sub = Branch (0.,trees) in let result = sum_up 0. sub in if w *. w >= result then if len trees != 0 then let newsub = List.nth trees 0 in traverse3 work newsub else true else false ;; let rec passes_da_vinci t = traverse3 0. t;; let ex = (Branch (17.4445773441247489, [Branch (3.43462299216136, [Branch (0.236436889887767288, [Leaf; Leaf]); Leaf; Branch (3.66399823447992734, [Leaf; Leaf; Leaf]); Leaf])] ));; passes_da_vinci ex;; let ex2 = (Branch (65.0032098310899187, [Branch (2.71827345538800236, [Branch (1.48721963379711397, [Leaf; Leaf; Leaf]); Branch (0.78492855843611764, [Leaf])]); Branch (56.9028916652587071, [Branch (17.0843328374204297, [Leaf; Leaf; Leaf; Leaf]); Leaf; Branch (42.2803710033402496, [Leaf; Leaf; Leaf])])]));; passes_da_vinci ex2;; sum_up 0. ex2;; |
let mode (l: 'a list) : 'a = let empty() = failwith "EMPTY" in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | 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) (max_el, max_num) else aux tl (hd,1) (cur_el, cur_num) | [] -> if max_num >= cur_num then max_el else cur_el; in match List.sort compare(l) with | hd::tl -> aux tl (hd,1)(hd, 0); | [] -> empty(); ;; |
let pair_mode (l: 'a list) : 'a * 'a = let len = List.length(l) in if len < 2 then failwith "ERROR" else let b = List.tl l in let a = List.rev(List.tl (List.rev l)) in let list_of_tuples = List.combine a b in mode (list_of_tuples ) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if fst (from_unit, val_) == to_unit then (from_unit, val_) else if Second == to_unit 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 fst (from_unit, val_) == to_unit then (from_unit, val_) else match to_unit with |Foot ->( if Meter == from_unit then (from_unit, val_ /. 0.3048) else (from_unit, val_ *. 5280.)) |Meter-> ( if Mile == from_unit then (from_unit, val_ *. 1609.344) else (from_unit, val_ *. 0.3048)) |Mile -> (if Foot == from_unit then (from_unit, val_ /. 5280.) else (from_unit, val_ /. 1609.344)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let converted_distance = convert_dist (fst from_unit, val_) (fst to_unit) in let converted_time = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit , snd converted_distance /. snd converted_time) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_converted = convert_speed a b_unit in (b_unit, snd a_converted +. b_val) ;; |
let passes_da_vinci t = let rec sum_ (l : tree list): float = match l with |[] -> 0. |hd :: tl -> match hd with | Leaf -> 0. +. sum_ tl | Branch ( x2,y2) -> sum_ tl +. x2 *. x2 in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> if check(subtree) && (sum_ subtree <= width *. width) then check tl else false in check [t];; |
let mode (l: 'a list) : 'a = if l = [] then failwith "List is empty" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> failwith "Error" |[x] -> max_el |x :: (y :: _ as z) -> if x = y && cur_num=max_num then aux z (x, cur_num+1) (x, max_num+1) else if x = y && cur_num != max_num then aux z (x, cur_num+1) (max_el, max_num) else aux z (y, 1) (max_el, max_num) in let sorted = List.sort compare (l) in aux sorted (List.nth sorted 0, 1) (List.nth sorted 0, 1) ;; let rec add_pair (single: 'a list) (fix: 'b list) : (('a * 'a) list) = match single with |[] -> failwith "empty list" |_ :: [] -> failwith "list too short" |[x;y] -> List.append fix [(x, y)] |x :: (y :: _ as z) -> (x, y) :: add_pair z fix;; |
let pair_mode (l: 'a list) : 'a * 'a = let pair_list = add_pair l [] in mode pair_list ;; |
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_ *. (1. /. 3600.)) |Second -> (Second, val_)) |Hour -> (match to_unit with |Hour -> (Hour, val_) |Second -> (Second, val_ *. 3600.)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> (match to_unit with |Meter -> (Meter, val_ *. 0.3048) |Mile -> (Mile, val_ /. 5280.) |Foot -> (Foot, val_)) |Meter -> (match to_unit with |Foot -> (Foot, val_ *. (1250. /. 381.)) |Mile -> (Mile, val_ *. ((1250. /. 381.) /. 5280.)) |Meter -> (Meter, val_)) |Mile -> (match to_unit with |Foot -> (Foot, val_ *. 5280.) |Meter -> (Meter, val_ *. 5280. *. 0.3048) |Mile -> (Mile, val_)) ;; let getUnit(a,b)=a;; let getValue(a,b)=b;; |
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), val_ /. getValue(convert_time(Second, 1.)(Hour))) |(Meter, Second) -> ((Meter, Second), val_ *. getValue(convert_dist(Foot, 1.)(Meter))) |(Meter, Hour) -> ((Meter, Hour), val_ *. getValue(convert_dist(Foot, 1.)(Meter)) /. getValue(convert_time(Second, 1.)(Hour))) |(Mile, Second) -> ((Mile, Second), val_ *. getValue(convert_dist(Foot, 1.)(Mile))) |(Mile, Hour) -> ((Mile, Hour), val_ *. getValue(convert_dist(Foot, 1.)(Mile)) /. getValue(convert_time(Second, 1.)(Hour)))) |(Foot, Hour) -> (match to_unit with |(Foot, Second) -> ((Foot, Second), val_ /. getValue(convert_time(Hour, 1.)(Second))) |(Foot, Hour) -> ((Foot, Hour), val_) |(Meter, Second) -> ((Meter, Second), val_ *. getValue(convert_dist(Foot, 1.)(Meter)) /. getValue(convert_time(Hour, 1.)(Second))) |(Meter, Hour) -> ((Meter, Hour), val_ *. getValue(convert_dist(Foot, 1.)(Meter))) |(Mile, Second) -> ((Mile, Second), val_ *. getValue(convert_dist(Foot, 1.)(Mile)) /. getValue(convert_time(Hour, 1.)(Second))) |(Mile, Hour) -> ((Mile, Hour), val_ *. getValue(convert_dist(Foot, 1.)(Mile)))) |(Mile, Second) -> (match to_unit with |(Foot, Second) -> ((Foot, Second), val_ *. getValue(convert_dist(Mile, 1.)(Foot))) |(Foot, Hour) -> ((Foot, Hour), val_ /. getValue(convert_time(Second, 1.)(Hour)) *. getValue(convert_dist(Mile, 1.)(Foot))) |(Meter, Second) -> ((Meter, Second), val_ *. getValue(convert_dist(Mile, 1.)(Meter))) |(Meter, Hour) -> ((Meter, Hour), val_ *. getValue(convert_dist(Mile, 1.)(Meter)) /. getValue(convert_time(Second, 1.)(Hour))) |(Mile, Second) -> ((Mile, Second), val_) |(Mile, Hour) -> ((Mile, Hour), val_ /. getValue(convert_time(Second, 1.)(Hour)))) |(Mile, Hour) -> (match to_unit with |(Foot, Second) -> ((Foot, Second), val_ *. getValue(convert_dist(Mile, 1.)(Foot)) /. getValue(convert_time(Hour, 1.)(Second))) |(Foot, Hour) -> ((Foot, Hour), val_ *. getValue(convert_dist(Mile, 1.)(Foot))) |(Meter, Second) -> ((Meter, Second), val_ *. getValue(convert_dist(Mile, 1.)(Meter)) /. getValue(convert_time(Hour, 1.)(Second))) |(Meter, Hour) -> ((Meter, Hour), val_ *. getValue(convert_dist(Mile, 1.)(Meter))) |(Mile, Second) -> ((Mile, Second), val_ /. getValue(convert_time(Hour, 1.)(Second))) |(Mile, Hour) -> ((Mile, Hour), val_)) |(Meter, Second) -> (match to_unit with |(Foot, Second) -> ((Foot, Second), val_ *. getValue(convert_dist(Meter, 1.)(Foot))) |(Foot, Hour) -> ((Foot, Hour), val_ /. getValue(convert_time(Second, 1.)(Hour)) *. getValue(convert_dist(Meter, 1.)(Foot))) |(Meter, Second) -> ((Meter, Second), val_) |(Meter, Hour) -> ((Meter, Hour), val_ /. getValue(convert_time(Second, 1.)(Hour))) |(Mile, Second) -> ((Mile, Second), val_ *. getValue(convert_dist(Meter, 1.)(Mile))) |(Mile, Hour) -> ((Mile, Hour), val_ /. getValue(convert_time(Second, 1.)(Hour)) *. (getValue(convert_dist(Meter, 1.)(Mile))))) |(Meter, Hour) -> (match to_unit with |(Foot, Second) -> ((Foot, Second), val_ *. getValue(convert_dist(Meter, 1.)(Foot)) /. getValue(convert_time(Hour, 1.)(Second))) |(Foot, Hour) -> ((Foot, Hour), val_ *. getValue(convert_dist(Meter, 1.)(Foot))) |(Meter, Second) -> ((Meter, Second), val_ /. getValue(convert_time(Hour, 1.)(Second))) |(Meter, Hour) -> ((Meter, Hour), val_) |(Mile, Second) -> ((Mile, Second), val_ /. getValue(convert_time(Hour, 1.)(Second)) *. getValue(convert_dist(Meter, 1.)(Mile))) |(Mile, Hour) -> ((Mile, Hour), val_ *. getValue(convert_dist(Meter, 1.)(Mile)))) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let converted = convert_time (getUnit(time), getValue(time)) (getValue(speed_unit)) in (getUnit(speed_unit), (speed_val *. getValue(converted))) ;; let tree_test1 = Branch (5., [Leaf]);; let tree_test2 = Branch (1., [Branch (4., [Leaf; Leaf])]);; let tree_test3 = Leaf;; let getBranchWidth(t: tree) : float = match t with |Leaf -> 0. |Branch (w, trees) -> w;; let rec sum_of_squares (t: tree list) : float = match t with |[] -> 0. |x :: y -> if x = Leaf then sum_of_squares(y) else getBranchWidth(x) *. getBranchWidth(x) +. sum_of_squares(y) ;; let rec passes_da_vinci t = match t with |Leaf -> true |Branch(w, trees) -> if (w *. w) >= (sum_of_squares trees) then let rec check_list (trees: tree list) = match trees with |[] -> true |x :: y -> passes_da_vinci x && check_list y in true && check_list trees else false ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | head::body -> if (head != cur_el) then begin cur_el = head; cur_num = 1 end else cur_num = cur_num + 1; if (cur_num > max_num) then begin max_el = cur_el; max_num = cur_num end aux l (cur_el, cur_num) (max_el, max_num) in let sList = List.sort compare (l) in aux (sList) (List.hd sList, 1) (List.hd sList, 1) ;; |
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) -> val_ *. 3600 | (Second, Hour) -> val_ /. 3600 | (x, x) -> val_ ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Mile) -> val_ /. 5280 | (Mile, Foot) -> val_ *. 3600 | (Meter, Foot) -> val_ /. 0.3048 | (Foot, Meter) -> val_ *. 0.3048 | (Meter, Mile) -> val_ /. 0.3048 /. 5280 | (Mile, Meter) -> val_ *. 0.3048 *. 5280 | (_, _) -> val_ ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match (from_unit, to_unit) with | ((a, b), (c, d)) -> convert_dist (a, (convert_time (d, val_) b)) c ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match speed_unit with | (dUnit, tUnit) -> (dUnit, (convert_time time tUnit) * speed_val) ;; let rec passes_da_vinci t = notimplemented () ;; |
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 | a :: tl when a = cur_el -> if cur_num < max_num then aux tl (cur_el, cur_num +1) (max_el, max_num) else aux tl (cur_el, cur_num +1) (cur_el, cur_num+1) | a :: tl -> aux tl (a, 1) (max_el, max_num) | [] -> max_el in if l = [] then failwith "invalid input" else aux l (List.nth l 0 ,0) (List.nth l 0 ,0) ;; let rec count_oc (count: int) ((a,b) : 'a * 'a) (l: 'a list) : int = match l with | x :: y :: l2 when x = a && y=b -> count_oc (count + 1) (a, b) (y :: l2) | x:: y :: l2 -> count_oc count (a,b) (y :: l2) | y :: [] -> count;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper_func (l: 'a list) (ltemp: 'a list) (((max_a, max_b), occs):('a * 'a) * int ) : 'a * 'a = match ltemp with | x :: y :: l2 when x = max_a && y = max_b-> helper_func l (y :: l2) ((max_a, max_b), occs) | x :: y :: l2 -> let c = count_oc 0 (x,y) l in (if c > occs then helper_func l (y::l2) ((x,y), c) else ( if c < occs then helper_func l (y::l2) ((max_a, max_b), occs) else ( if x < max_a || (x = max_a && y < max_b) then helper_func l (y::l2) ((x,y), c) else helper_func l (y::l2) ((max_a, max_b), occs) ))) | x :: [] -> (max_a, max_b) in if List.length l < 2 then failwith "invalid input" else helper_func l l ((List.nth l 0 , List.nth l 1 ), count_oc 0 (List.nth l 0 , List.nth l 1 ) l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> (from_unit, val_) | Hour -> (to_unit, val_ /. 3600.0) ) | Hour -> (match to_unit with | Second -> (to_unit, val_ *. 3600.0) | Hour -> (from_unit, val_) ) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Foot -> (from_unit, val_) | Meter -> ( to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.0) ) | Meter -> (match to_unit with | Foot -> (to_unit, val_ /. 0.3048) | Meter -> (to_unit, val_) | Mile -> (to_unit, val_ /. 0.3048 /. 5280.0) ) | Mile -> (match to_unit with | Foot -> (to_unit, val_ *. 5280.0) | Meter -> (to_unit, val_ *. 5280.0 *. 0.3048) | Mile -> (to_unit, val_) ) ;; let get_first ((a,b) : 'a * 'b) : 'a = a let get_second ((a,b) : 'a * 'b) : 'b = b;; |
let convert_speed ((from_unit, val_) : speed_unit value) (to_unit: speed_unit) : speed_unit value = (to_unit,val_*.(get_second ( convert_time (get_second to_unit, 1.0) (get_second from_unit))) *.(get_second ( convert_dist (get_first from_unit, 1.0) (get_first to_unit)))) ;; let (++) (((dv1,tv1), v1) : speed_unit value) (((dv2, tv2), v2) : speed_unit value) : speed_unit value = ( if dv1 = dv2 && tv1 = tv2 then ((dv1, tv1), v1 +. v2) else failwith "passed mismatched units to ++" );; |
let dist_traveled (time : time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let new_t = convert_time time (get_second speed_unit) in (get_first speed_unit, speed_val *. (get_second new_t)) ;; let rec sqr_sum_list (l : tree list) = match l with | Branch(w,t) :: l2 -> w*.w +. sqr_sum_list l2 | Leaf :: l2 -> sqr_sum_list l2 | [] -> 0. let rec passes_da_vinci t = match t with | Branch(w, tr) -> w*.w >= (sqr_sum_list tr) && (List.for_all passes_da_vinci tr) | Leaf -> true;; |
let invalid ()= failwith "Invalid Input" ;; |
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 if max_num = 0 then aux t (h,1) (h,1) else aux t (h,1) (max_el,max_num) in let l1 = List.sort compare l in match l1 with | [] -> invalid () | h :: _ -> aux l1 (h,0) (h,0) ;; let x = [3;1;2];; mode x;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec split l : ('a * 'a) list = match l with | [] -> [] | h :: [] -> [] | h :: t -> List.concat [[h,List.hd t];split t] in match l with | [] -> invalid () | h :: [] -> invalid () | h :: t -> mode (split 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.)) | Second,Second -> (Second, val_) | Hour,Second -> (Second,val_ *. 3600.) | Hour,Hour -> (Hour,val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit,to_unit with | Foot,Meter -> (Meter,(val_ *. 0.3048)) | Foot,Mile -> (Mile,(val_ /. 5280.)) | Foot,Foot -> (Foot,val_) | Meter,Foot -> (Foot,(val_ /. 0.3048)) | Meter,Mile -> (Mile,(val_ /. 0.3048 /. 5280.)) | Meter,Meter -> (Meter,val_) | Mile,Foot -> (Foot,(val_ *. 5280.)) | Mile,Meter -> (Meter,(val_ *. 5280. *. 0.3048)) | Mile,Mile -> (Mile,val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit,to_unit with | (Foot,Second),(Foot,Second) -> ((Foot,Second),val_) | (Foot,Second),(Foot,Hour) -> ((Foot,Hour),(val_ *. 3600.)) | (Foot,Second),(Meter,Second) -> ((Meter,Second),(val_ *. 0.3048)) | (Foot,Second),(Meter,Hour) -> ((Meter,Hour),(val_ *. 0.3048 *. 3600.)) | (Foot,Second),(Mile,Second) -> ((Mile,Second),(val_ /. 5280.)) | (Foot,Second),(Mile,Hour) -> ((Mile,Hour),(val_ /. 5280. *. 3600.)) | (Foot,Hour),(Foot,Second) -> ((Foot,Second),(val_ /. 3600.)) | (Foot,Hour),(Foot,Hour) -> ((Foot,Hour),val_) | (Foot,Hour),(Meter,Second) -> ((Meter,Second),(val_ *. 0.3048 /. 3600.)) | (Foot,Hour),(Meter,Hour) -> ((Meter,Hour),(val_ *. 0.3048)) | (Foot,Hour),(Mile,Second) -> ((Mile,Second),(val_ /. 5280. /. 3600.)) | (Foot,Hour),(Mile,Hour) -> ((Mile,Hour),(val_ /. 5280.)) | (Meter,Second),(Foot,Second) -> ((Foot,Second),(val_ /. 0.3048)) | (Meter,Second),(Foot,Hour) -> ((Foot,Hour),(val_ /. 0.3048 *. 3600.)) | (Meter,Second),(Meter,Second) -> ((Meter,Second),val_) | (Meter,Second),(Meter,Hour) -> ((Meter,Hour),(val_ *. 3600.)) | (Meter,Second),(Mile,Second) -> ((Mile,Second),(val_ /. 0.3048 /. 5280.)) | (Meter,Second),(Mile,Hour) -> ((Mile,Hour),(val_ /. 0.3048 /. 5280. *. 3600.)) | (Meter,Hour),(Foot,Second) -> ((Foot,Second),(val_ /. 0.3048 /. 3600.)) | (Meter,Hour),(Foot,Hour) -> ((Foot,Hour),(val_ /. 0.3048)) | (Meter,Hour),(Meter,Second) -> ((Meter,Second),(val_ /. 3600.)) | (Meter,Hour),(Meter,Hour) -> ((Meter,Hour),val_) | (Meter,Hour),(Mile,Second) -> ((Mile,Second),(val_ /. 0.3048 /. 5280. /. 3600.)) | (Meter,Hour),(Mile,Hour) -> ((Mile,Hour),(val_ /. 0.3048 /. 5280.)) | (Mile,Second),(Foot,Second) -> ((Foot,Second),(val_ *. 5280.)) | (Mile,Second),(Foot,Hour) -> ((Foot,Hour),(val_ *.5280. *. 3600.)) | (Mile,Second),(Meter,Second) -> ((Meter,Second),(val_ *. 5280. *. 0.3048)) | (Mile,Second),(Meter,Hour) -> ((Meter,Hour),(val_ *. 3600. *. 5280. *. 0.3048)) | (Mile,Second),(Mile,Second) -> ((Mile,Second),val_) | (Mile,Second),(Mile,Hour) -> ((Mile,Hour),(val_ *. 3600.)) | (Mile,Hour),(Foot,Second) -> ((Foot,Second),(val_ *. 5280. /. 3600.)) | (Mile,Hour),(Foot,Hour) -> ((Foot,Hour),(val_ *. 5280.)) | (Mile,Hour),(Meter,Second) -> ((Meter,Second),(val_ *. 5280. *. 0.3048 /. 3600.)) | (Mile,Hour),(Meter,Hour) -> ((Meter,Hour),(val_ *. 5280. *. 0.3048)) | (Mile,Hour),(Mile,Second) -> ((Mile,Second),(val_ /. 3600.)) | (Mile,Hour),(Mile,Hour) -> ((Mile,Hour),val_) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (x:speed_unit value) = convert_speed ((fst a),(snd a)) b_unit in let speed = (snd x) +. (b_val) in ((fst x),speed) ;; |
let passes_da_vinci t : bool= let square_sum = sum (extract t) in match t with | Leaf -> true | Branch(width,_) -> square_sum <= (width *. width) ;; |
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) = 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, Hour) | (Second, Second) -> to_unit, (val_) | (Hour, Second) -> to_unit, (val_ *. 3600.) | (Second, Hour) -> to_unit, (val_ /. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | (Foot, Foot) | (Meter , Meter ) | (Mile, Mile) -> to_unit, (val_) | (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) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (distance_from_unit, time_from_unit) = from_unit in let (distance_to_unit, time_to_unit) = to_unit in match distance_from_unit, distance_to_unit with | (Foot, Foot) | (Meter, Meter) | (Mile, Mile) -> (match time_from_unit,time_to_unit with | (Hour, Hour) | (Second, Second) -> to_unit, val_ | _ -> let (_, val_) = convert_time (time_to_unit, val_) time_from_unit in to_unit, val_ ) | _ -> let (_, val_) = convert_dist (distance_from_unit, val_) distance_to_unit in (match time_from_unit,time_to_unit with | (Hour, Hour) | (Second, Second) -> to_unit, val_ | _ -> let (_, val_) = convert_time (time_to_unit, val_) time_from_unit in to_unit, val_ ) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (distance_unit, time_unit) = speed_unit in let (_, time_travelled) = convert_time time time_unit in distance_unit, time_travelled *. speed_val ;; let rec passes_da_vinci t = true ;; |
let mode (l: 'a list) : 'a = match List.sort compare l with | [] -> failwith "[] is not a valid input" | x :: xs -> 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 (x, cur_num + 1) (max_el, max_num) else ( if max_num >= cur_num then aux xs (x, 1) (max_el, max_num) else aux xs (x, 1) (cur_el, cur_num) ) in aux xs (x, 1) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "The list is not long enough" else ( let l1 = List.rev l in let l1 = List.tl l1 in let l1 = List.rev l1 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 = if to_unit = from_unit then (to_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 (to_unit, val_) else ( match from_unit with Foot -> if to_unit = Meter then (to_unit, val_ *. 0.3048) else (to_unit, val_ /. 5280.0) | Meter -> if to_unit = Foot then (to_unit, val_ /. 0.3048) else (to_unit, val_ /. (5280.0 *. 0.3048)) | Mile -> if to_unit = Foot then (to_unit, val_ *. 5280.0) else (to_unit, val_ *. (5280.0 *. 0.3048)) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (_, val2) = convert_dist (fst from_unit, val_) (fst to_unit) in let (_, val3) = convert_time (snd to_unit, val2) (snd from_unit) in (to_unit, val3) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, a_val) = convert_speed a b_unit in (b_unit, a_val +. b_val) ;; |
let passes_da_vinci t = let rec sum_ subtree acc : float = match subtree 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 sum_ subtree 0. > (width ** 2.) 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 | [] -> if cur_num > max_num || (cur_num = max_num && max_el > cur_el) then cur_el else max_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 if cur_num = max_num && cur_el > max_el then (aux tl (hd, 1) (max_el, max_num)) else (aux tl (hd,1) (max_el, max_num)) in match List.sort compare l with | [] -> failwith "Invalid Input." | hd :: tl -> aux tl (hd, 1) (hd, 0) ;; let removeFirst l = let _::l = l in l;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = removeFirst l in let l2 = List.rev (removeFirst (List.rev l)) in let l3 = List.combine l2 l1 in match l3 with | [] -> failwith "Invalid Input." | _ -> 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 && to_unit = Hour then (to_unit, val_/.3600.) else (to_unit, val_*.3600.) ;; let convert_timeSpeed ((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 && to_unit = Hour 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 (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_ /. 1609.344)) | Mile -> (if to_unit = Meter then (to_unit, val_ *. 1609.344) else (to_unit,val_ *. 5280.)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (finalUnit : speed_unit) = ((fst to_unit), (snd to_unit)) in if from_unit = to_unit then (finalUnit,val_) else let dist = convert_dist (fst from_unit, val_) (fst to_unit) in let time = convert_timeSpeed (snd from_unit, (snd dist)) (snd to_unit) in (finalUnit, (snd time)) ;; |
let add_speed (a_unit,a_val : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = if a_unit = b_unit then (b_unit, a_val +. b_val) else let conv = convert_speed (a_unit, a_val) b_unit in let total = (snd conv) +. b_val in (b_unit, total) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl acc +. width**2. in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (check subtree && (sum_ subtree 0. <= width*.width)) == false 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 cur_el with | max_el -> 0 | _ -> 0 in aux (List.sort compare l) (List.hd l, 0) (List.hd l, 0) ;; let add1 = (+) 1;; |
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> if to_unit = Hour then (Hour, val_) else if to_unit = Second then (Second, val_ *. 3600.) else notimplemented() | Second -> if to_unit = Second then (Second, val_) else if to_unit = Hour then (Hour, val_ /. 3600.) else notimplemented() | _ -> notimplemented() ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Foot then (Foot, val_) else if to_unit = Meter then (Meter, val_ *. 0.3048) else if to_unit = Mile then (Mile, val_ *. (1. /. 5280.)) else notimplemented() | Meter -> if to_unit = Meter then (Meter, val_) else if to_unit = Foot then (Foot, val_ *. 3.28083989501) else if to_unit = Mile then (Mile, val_ *. 0.00062137119) else notimplemented() | Mile -> if to_unit = Mile then (Mile, val_) else if to_unit = Foot then (Foot, val_ *. 5280.) else if to_unit = Meter then (Meter, val_ *. 1609.344) else notimplemented() | _ -> notimplemented() ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let distance = ((snd(convert_dist (fst(from_unit), val_) (fst(to_unit))))) in let time = ((snd(convert_time (snd(from_unit), distance) (snd(to_unit))))) in (to_unit, (distance /. time) *. distance) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst(speed_unit), snd((convert_time (fst(time), snd(time)) (snd(speed_unit)))) *. 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 && cur_num+1>max_num then aux tl (cur_el, cur_num+1) (cur_el, cur_num+1) else if hd = cur_el && cur_num+1<=max_num then aux tl (cur_el, cur_num+1) (max_el, max_num) else aux tl (hd, 1) (max_el, max_num) in let l1 =List.sort compare (l) in match l1 with | [] -> failwith "Invalid Input" | x::xs -> aux l1 (x,0) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid Input" |_-> let l1 = List.tl l in let l2 = List.rev (List.tl (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 | Second -> if to_unit = Hour then let y:time_unit value = (Hour, val_ /. 3600.0) in y else let y:time_unit value =(Second, val_) in y | Hour -> if to_unit = Second then let y:time_unit value = (Second, val_*. 3600.0) in y else let y:time_unit value =(Hour, val_) in y;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Meter then let y:dist_unit value = (Meter, val_*. 0.3048) in y else if to_unit = Mile then let y:dist_unit value =(Mile, val_ /. 5280.0) in y else let y:dist_unit value =(Foot, val_) in y | Meter -> if to_unit = Foot then let y:dist_unit value = (Foot, val_ /. 0.3048) in y else if to_unit = Mile then let y:dist_unit value =(Mile, val_ /. 1609.344) in y else let y:dist_unit value =(Meter, val_) in y | Mile -> if to_unit = Foot then let y:dist_unit value = (Foot, val_*. 5280.0) in y else if to_unit = Meter then let y:dist_unit value =(Meter, val_ *. 1609.344) in y else let y:dist_unit value =(Mile, val_) in y ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let fromdistunit = fst(from_unit) in let fromtimeunit = snd(from_unit) in let todistunit = fst(to_unit) in let totimeunit = snd(to_unit) in let y: speed_unit value = (to_unit, (val_ *. snd(convert_dist(fromdistunit,1.0) todistunit))/.(snd(convert_time (fromtimeunit,1.0) totimeunit))) in y ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let c = convert_speed a b_unit in let d = snd(c) +. b_val in let y:speed_unit value = ((b_unit),d) in y;; |
let passes_da_vinci t = let rec sum_ l acc= match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> let y = acc +. width*.width in sum_ tl y in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not (check subtree) then false else if not (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 | [] -> if cur_num > max_num then cur_el else if cur_num = max_num then if cur_el < max_el then cur_el else max_el else max_el | hd :: tl -> if hd = cur_el then if cur_num >= 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) in match l with | [] -> failwith "List must contain at least one element." | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; let cut l = match l with | [] -> [] | hd :: tl -> tl ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = cut l in let l2 = List.rev (cut (List.rev l)) in if List.length l < 2 then failwith "List must contain at least 2 elements." else let ls = List.combine l2 l1 in mode ls ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match ((from_unit, val_), to_unit) with | ((Second, t), Hour) -> (Hour, t /. 3600.) | ((Hour, t), Second) -> (Second, t *. 3600.) | ((_, t), to_unit) -> (to_unit, t) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match ((from_unit, val_), to_unit) with | ((Foot, d), Meter) -> (Meter, d *. 0.3048) | ((Foot, d), Mile) -> (Mile, d /. 5280.) | ((Meter, d), Foot) -> (Foot, d /. 0.3048) | ((Meter, d), Mile) -> (Mile, d /. 0.3048 /. 5280.) | ((Mile, d), Foot) -> (Foot, d *. 5280.) | ((Mile, d), Meter) -> (Meter, d *. 5280. *. 0.3048) | ((_, d), to_unit) -> (to_unit, d) ;; |
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 if from_dist = to_dist && from_time != to_time then (to_unit, (val_ /. snd(convert_time (from_time, 1.) to_time))) else if from_dist != to_dist && from_time = to_time then (to_unit, (val_ *. snd(convert_dist (from_dist, 1.) to_dist))) else if from_dist != to_dist && from_time != to_time then (to_unit, (val_ *. snd(convert_dist (from_dist, 1.) to_dist) /. snd(convert_time (from_time, 1.) to_time))) else (to_unit, val_) ;; |
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 (b_unit, snd(convert_speed (fst(a), snd(a)) (b_unit)) +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l = match l with | [] -> 0. | Leaf :: tl -> sum_ tl | Branch (width, _) :: tl -> sum_ tl +. width**2. in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && (sum_ subtree <= 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) = match l with | [] -> max_el | e :: els -> let num = if e = cur_el then cur_num + 1 else 1 in let max = if num > max_num then (e, num) else (max_el, max_num) in aux els (e, num) max in let ls = List.sort compare l in match ls with | [] -> failwith "Undefined input. Empty lists are not allowed." | x :: _ -> aux ls (x, 0) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input. Empty lists are not allowed." | x :: [] -> failwith "Undefined input. Lists of length 1 are not allowed." | _ :: ls -> let pop l = match List.rev l with | [] -> [] | _ :: rev -> List.rev rev in mode (List.combine (pop l) ls) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> Hour, val_ /. 3600. | Hour, Second -> Second, val_ *. 3600. | _, _ -> from_unit, val_ ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> Meter, val_ *. 0.3048 | 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 from_dist_unit, from_time_unit = from_unit in let to_dist_unit, to_time_unit = to_unit in let _, distance = convert_dist (from_dist_unit, val_) to_dist_unit in let _, time = convert_time (from_time_unit, 1.) to_time_unit in (to_dist_unit, to_time_unit), distance /. time ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let _, a_speed = convert_speed a b_unit in b_unit, a_speed +. b_val ;; |
let passes_da_vinci t = let rec sum_squares (tl : tree list) acc : float = match tl with | [] -> acc | Leaf :: trl -> sum_squares trl acc | Branch (f, trls) :: trl -> sum_squares trl (acc +. f *. f) in let rec pass tl = match tl with | [] -> true | Leaf :: trl -> pass trl | Branch (f, trls) :: trl -> f *. f >= sum_squares trls 0. && pass trls && pass trl in pass [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 | e :: els -> let num = if e = cur_el then cur_num + 1 else 1 in let max = if num > max_num then (e, num) else (max_el, max_num) in aux els (e, num) max in let ls = List.sort compare l in match ls with | [] -> failwith "Undefined input. Empty lists are not allowed." | x :: _ -> aux ls (x, 0) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input. Empty lists are not allowed." | x :: [] -> failwith "Undefined input. Lists of length 1 are not allowed." | _ :: ls -> let pop l = match List.rev l with | [] -> [] | _ :: rev -> List.rev rev in mode (List.combine (pop l) ls) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> Hour, val_ /. 3600. | Hour, Second -> Second, val_ *. 3600. | _, _ -> from_unit, val_ ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> Meter, val_ *. 0.3048 | 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 from_dist_unit, from_time_unit = from_unit in let to_dist_unit, to_time_unit = to_unit in let _, distance = convert_dist (from_dist_unit, val_) to_dist_unit in let _, time = convert_time (from_time_unit, 1.) to_time_unit in (to_dist_unit, to_time_unit), distance /. time ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let _, a_speed = convert_speed a b_unit in b_unit, a_speed +. b_val ;; |
let passes_da_vinci t = let rec sum_squares (tl : tree list) acc : float = match tl with | [] -> acc | Leaf :: trl -> sum_squares trl acc | Branch (f, _) :: trl -> sum_squares trl (acc +. f *. f) in let rec pass tl = match tl with | [] -> true | Leaf :: trl -> pass trl | Branch (f, trls) :: trl -> f *. f >= sum_squares trls 0. && pass trls && pass trl in pass [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_count) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | e :: ls -> if e > cur_el then if cur_count > max_num then aux ls (e, 1) (cur_el, cur_count) else aux ls (e, 1) (max_el, max_num) else if cur_count + 1 > max_num then aux ls (cur_el, cur_count + 1) (cur_el, cur_count + 1) else aux ls (cur_el, cur_count + 1) (max_el, max_num) in let l = List.sort compare (l); in match l with | [] -> failwith "List too small." | e :: ls -> aux ls (e, 1) (e, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux tup_ls el_ls = match el_ls with | [] -> tup_ls | e :: ls -> match ls with | [] -> tup_ls | f :: rest_of_ls -> let tup_ls = (e,f) :: tup_ls in aux tup_ls ls in match l with | [] -> failwith "List too small." | e :: [] -> failwith "List too small." | e :: ls -> let tup_ls = aux [] l in mode tup_ls ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let from_coeff = match from_unit with | Second -> 1.0/.3600.0 | Hour -> 1.0 in let to_coeff = match to_unit with | Second -> 3600.0 | Hour -> 1.0 in (to_unit , val_ *. from_coeff *. to_coeff) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let from_coeff = match from_unit with | Foot -> 1.0 | Meter -> 1.0/.0.3048 | Mile -> 5280.0 in let to_coeff = match to_unit with | Foot -> 1.0 | Meter -> 0.3048 | Mile -> 1.0/.5280.0 in (to_unit , val_ *. from_coeff *. to_coeff) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_dist_unit,from_time_unit) = from_unit in let (to_dist_unit, to_time_unit) = to_unit in let distance = convert_dist (from_dist_unit, val_) to_dist_unit in let time = convert_time (from_time_unit, 1.0) to_time_unit in let (d_unit, d_val) = distance in let (t_unit, t_val) = time in let speed = d_val/.t_val in (to_dist_unit, to_time_unit), speed ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (in_time_unit, in_time_val) = time in let (out_dist_unit, out_time_unit) = speed_unit in let time_coeff = convert_time (in_time_unit, 1.0) out_time_unit in let (time_unit,time_coeff_val) = time_coeff in out_dist_unit, (in_time_val *. speed_val *. time_coeff_val) ;; let rec passes_da_vinci t = let rec sum_of_squares (treelist : tree list) (count : float) = match treelist with | [] -> 0.0 | cur_branch :: rest_of_branches -> let cur_branch_value = match cur_branch with | Leaf -> 0.0 | Branch(width, smaller_branches) -> width*.width in match rest_of_branches with | [] -> 0.0 +. cur_branch_value +. count | _ -> cur_branch_value +. sum_of_squares rest_of_branches count in let rec tree_ls_passes tree_ls = match tree_ls with | [] -> true | tree :: trees -> passes_da_vinci tree && tree_ls_passes trees in match t with | Leaf -> true | Branch(width, sub_branches) -> let width_ok = width*.width >= sum_of_squares sub_branches 0.0 in let sub_branches_ok = tree_ls_passes sub_branches in width_ok && sub_branches_ok ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.