text
stringlengths
0
601k
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,0.3048*.val_) |Mile-> (Mile,(1./.5280.)*.val_)) |Meter -> (match to_unit with |Foot -> (Foot,(1./.0.3048)*.val_) |Meter-> (Meter, val_) |Mile-> (Mile,(val_/.0.3048)/.5280.)) |Mile -> (match to_unit with |Foot -> (Foot, val_*.5280.) |Meter-> (Meter,0.3048*.5280.*.val_) |Mile-> (Mile,val_)) ;; let toflo (b: 'a value) : float = let (y,c) = b in c;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (Meter,Hour) -> (match to_unit with | (Meter,Hour)-> (from_unit, val_) | (Meter,Second) -> ((Meter,Second), toflo (convert_dist (Meter, (val_/. toflo (convert_time (Hour,1.) Second))) Meter)) | (Mile,Second) -> ((Mile,Second), toflo(convert_dist (Meter, val_) Mile)/.toflo(convert_time (Hour,1.) Second)) | (Mile,Hour) -> ((Mile,Hour), toflo(convert_dist (Meter, val_) Mile)/.toflo(convert_time (Hour,1.) Hour)) | (Foot,Second) -> ((Foot,Second), toflo(convert_dist (Meter, val_) Foot)/.toflo(convert_time (Hour,1.) Second)) | (Foot,Hour) -> ((Foot,Hour), toflo(convert_dist (Meter, val_) Foot)/.toflo(convert_time (Hour,1.) Hour))) | (Meter,Second) -> (match to_unit with | (Meter,Hour)-> ((Meter,Hour),toflo (convert_dist (Meter, val_) Meter)/.toflo(convert_time (Second,1.) Hour)) | (Meter,Second) -> ((Meter,Second), toflo(convert_dist (Meter, val_) Meter)/.toflo(convert_time (Second,1.) Second)) | (Mile,Second) ->((Mile,Second), toflo(convert_dist (Meter, val_) Mile)/.toflo(convert_time (Second,1.) Second)) | (Mile,Hour) -> ((Mile,Hour), toflo(convert_dist (Meter, val_) Mile)/.toflo(convert_time (Second,1.) Hour)) | (Foot,Second) -> ((Foot,Second), toflo(convert_dist (Meter, val_) Foot)/.toflo(convert_time (Second,1.) Second)) | (Foot,Hour) ->((Foot,Hour), toflo(convert_dist (Meter, val_) Foot)/.toflo(convert_time (Second,1.) Hour)) ) | (Mile,Second) ->(match to_unit with | (Meter,Hour)-> ((Meter,Hour), toflo(convert_dist (Mile, val_) Meter)/.toflo(convert_time (Second,1.) Hour)) | (Meter,Second) -> ((Meter,Second), toflo(convert_dist (Mile, val_) Meter)/.toflo(convert_time (Second,1.) Second)) | (Mile,Second) ->((Mile,Second), toflo(convert_dist (Mile, val_) Mile)/.toflo(convert_time (Second,1.) Second)) | (Mile,Hour) -> ((Mile,Hour), toflo(convert_dist (Mile, val_) Mile)/.toflo(convert_time (Second,1.) Hour)) | (Foot,Second) -> ((Foot,Second), toflo(convert_dist (Mile, val_) Foot)/.toflo(convert_time (Second,1.) Second)) | (Foot,Hour) -> ((Foot,Hour), toflo(convert_dist (Mile, val_) Foot)/.toflo(convert_time (Second,1.) Hour))) | (Mile,Hour) -> (match to_unit with | (Meter,Hour)-> ((Meter,Hour), toflo(convert_dist (Mile, val_) Meter)/.toflo(convert_time (Hour,1.) Hour)) | (Meter,Second) -> ((Meter,Second), toflo(convert_dist (Mile, val_) Meter)/.toflo(convert_time (Hour,1.) Second)) | (Mile,Second) ->((Mile,Second),toflo (convert_dist (Mile, val_) Mile)/.toflo(convert_time (Hour,1.) Second)) | (Mile,Hour) -> ((Mile,Hour), toflo(convert_dist (Mile, val_) Mile)/.toflo(convert_time (Hour,1.) Hour)) | (Foot,Second) -> ((Foot,Second), toflo(convert_dist (Mile, val_) Foot)/.toflo(convert_time (Hour,1.) Second)) | (Foot,Hour) -> ((Foot,Hour), toflo(convert_dist (Mile, val_) Foot)/.toflo(convert_time (Hour,1.) Hour))) | (Foot,Second) -> (match to_unit with | (Meter,Hour)-> ((Meter,Hour), toflo(convert_dist (Foot, val_) Meter)/.toflo(convert_time (Second,1.) Hour)) | (Meter,Second) -> ((Meter,Second),toflo (convert_dist (Foot, val_) Meter)/.toflo(convert_time (Second,1.) Second)) | (Mile,Second) -> ((Mile,Second), toflo(convert_dist (Foot, val_) Mile)/.toflo(convert_time (Second,1.) Second)) | (Mile,Hour) -> ((Mile,Hour), toflo(convert_dist (Foot, val_) Mile)/.toflo(convert_time (Second,1.) Hour)) | (Foot,Second) -> ((Foot,Second),toflo (convert_dist (Foot, val_) Foot)/.toflo(convert_time (Second,1.) Second)) | (Foot,Hour) -> ((Foot,Hour),toflo (convert_dist (Foot, val_) Foot)/.toflo(convert_time (Second,1.) Hour))) | (Foot,Hour) -> (match to_unit with | (Meter,Hour)-> ((Meter,Hour),toflo (convert_dist (Foot, val_) Meter)/.toflo(convert_time (Hour,1.) Hour)) | (Meter,Second) -> ((Meter,Second),toflo (convert_dist (Foot, val_) Meter)/.toflo(convert_time (Hour,1.) Second)) | (Mile,Second) ->((Mile,Second),toflo(convert_dist (Foot, val_) Mile)/.toflo(convert_time (Hour,1.) Second)) | (Mile,Hour) -> ((Mile,Hour),toflo (convert_dist (Foot, val_) Mile)/.toflo(convert_time (Hour,1.) Hour)) | (Foot,Second) -> ((Foot,Second), toflo(convert_dist (Foot, val_) Foot)/.toflo(convert_time (Hour,1.) Second)) | (Foot,Hour) ->((Foot,Hour), toflo(convert_dist (Foot, val_) Foot)/.toflo(convert_time (Hour,1.) Hour)) );;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (a,b) = speed_unit in (a,(speed_val*. toflo(convert_time time b))) ;; let rec h1 tr sum = match tr with |[] -> sum |x::l -> (match x with |Branch(a,ys) -> h1 l sum+.(a*.a) |Leaf -> h1 l sum) |x::[] -> (match x with |Branch(a,ys) -> sum+.(a*.a) |Leaf -> sum) ;; let rec trav r = match r with |[] -> true |x::xs -> (match x with |Leaf-> true |Branch(g,[]) -> true |Branch(g,h) -> if (h1 h 0. <= (g*.g)) then trav h else false) let rec passes_da_vinci t = trav [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 | a::t -> if a = cur_el then if cur_num+1 > max_num then aux t (cur_el, cur_num + 1) (cur_el, cur_num+1) else aux t (cur_el, cur_num + 1) (max_el, max_num) else aux t (a, 1) (max_el, max_num) | [] -> max_el in let l = List.sort compare l in let x::t = l in aux t (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec list_builder l in_progress = let x::z = l in match z with | y :: t -> let b = List.append in_progress [(x,y)] in list_builder (y::t) b | [] -> mode in_progress in list_builder l [] ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> Second, val_ | Hour -> Hour, val_ /. float_of_int 3600) | Hour -> (match to_unit with | Second -> Second, val_ *. float_of_int 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 | Foot -> Foot, val_ | Meter -> Meter, val_ *. 0.3048 | Mile -> Mile, val_ /. float_of_int 5280) | Meter -> (match to_unit with | Foot -> Foot, val_ /. 0.3048 | Meter -> Meter, val_ | Mile -> Mile, val_ /. 0.3048 /. float_of_int 5280) | Mile -> (match to_unit with | Foot -> Foot, val_ *. float_of_int 5280 | Meter -> Meter, val_ *. float_of_int 5280 *. 0.3048 | Mile -> Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (k, Second) -> (match to_unit with | (i, Second) -> let (x,y) = convert_dist (k, val_) i in (i, Second), y | (j, Hour) -> let (x,y) = convert_dist (k, val_) j in (j, Hour), y *. float_of_int 3600 ) | (k, Hour) ->(match to_unit with | (i, Second) -> let (x,y) = convert_dist (k, val_) i in (i, Second), y /. float_of_int 3600 | (j, Hour) -> let (x,y) = convert_dist (k, val_) j in (j, Hour), y) ;;
let dist_traveled (time: time_unit value) ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (i, j) = speed_unit in let (x, y) = convert_time time j in i, speed_val *. y ;; let rec calc_sum l base acc = match l with | Leaf :: a -> calc_sum a base acc | Branch (x, y) :: a -> if base *. base < acc +. x**2. then -1 else calc_sum a base (acc +. x**2.) | [] -> 0 ;; let rec help_recurse t l f = match l with | [] -> (if f = 1 then true else match t with | Branch (x,y) -> if (calc_sum y x 0.) = 0 then help_recurse t y 1 else false | Leaf -> true) | a :: b -> (match a with | Leaf -> help_recurse t b 1 | Branch (x,y) -> if (calc_sum y x 0.) = 0 then help_recurse t b 1 else false) ;; let rec passes_da_vinci t = help_recurse t [] 0 ;;
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> begin if cur_num > max_num then cur_el else max_el end | hd::tl -> if hd = cur_el then aux tl (cur_el,cur_num+1) (max_el,max_num) else begin if cur_num > max_num then aux tl (hd,1)(cur_el, cur_num) else aux tl (hd,1)(max_el, max_num) end in let l = List.sort compare(l) in match l with | [] -> failwith "List is empty" | [x] -> x | hd::tl -> aux tl (hd,1) (hd,1);;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "List not long enough to evaluate bi-grams" | [x] -> failwith "List not long enough to evaluate bi-grams" | hd::tl -> let hd2::tl2 = List.rev (hd::tl) in mode (List.combine (List.rev(tl2)) tl) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> begin match to_unit with | Second -> (Second,val_) | Hour -> (Hour,val_ /. 3600.) | _ -> failwith "Unit type not recognized" end | Hour -> begin match to_unit with | Second -> (Second,val_ *. 3600.) | Hour -> (Hour,val_) | _ -> failwith "Unit type not recognized" end | _ -> failwith " Unit type not recognized" ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> begin match to_unit with | Foot -> (Foot,val_) | Meter -> (Meter,(val_ *. 0.3048)) | Mile -> (Mile,(val_ /. 5280.)) | _ -> failwith "Unit type not recognized" end | Meter -> begin match to_unit with | Foot -> (Foot,val_ /. 0.3048) | Meter -> (Meter,val_ ) | Mile -> (Mile,(val_ /. 0.3048) /. 5280.) | _ -> failwith "Unit type not recognized" end | Mile -> begin match to_unit with | Foot -> (Foot,val_ *. 5280.) | Meter -> (Meter,(val_ *. 5280.) *. 0.3048) | Mile -> (Mile,val_ ) | _ -> failwith "Unit type not recognized" end | _ -> failwith " Unit type not recognized" ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match fst from_unit, snd from_unit with | Foot, Second -> begin match fst to_unit, snd to_unit with | Foot, Second -> (from_unit, val_) | Foot, Hour -> (to_unit, snd (convert_time (Hour, val_) Second)) | Meter, Second -> (to_unit, snd (convert_dist (Foot, val_) Meter)) | Meter, Hour -> (to_unit, snd (convert_time (Hour, snd (convert_dist (Foot, val_) Meter)) Second)) | Mile, Second -> (to_unit, snd (convert_dist (Foot,val_) Mile)) | Mile, Hour -> (to_unit, snd (convert_time (Hour, snd (convert_dist (Foot, val_) Mile)) Second)) | _ -> failwith "Unit type not recognized" end | Foot, Hour -> begin match fst to_unit, snd to_unit with | Foot, Second -> (to_unit, snd (convert_time(Second,val_) Hour)) | Foot, Hour -> (to_unit, val_) | Meter, Second -> (to_unit, snd (convert_time (Second, snd (convert_dist (Foot, val_) Meter)) Hour)) | Meter, Hour -> (to_unit, snd (convert_dist (Foot, val_) Meter)) | Mile, Second -> (to_unit, snd (convert_time (Second, snd (convert_dist (Foot, val_) Mile)) Hour)) | Mile, Hour -> (to_unit, snd (convert_dist (Foot, val_) Mile)) | _ -> failwith "Unit type not recognized" end | Meter, Second -> begin match fst to_unit, snd to_unit with | Foot, Second -> (to_unit, snd (convert_dist (Meter, val_) Foot)) | Foot, Hour -> (to_unit, snd (convert_time (Hour, snd (convert_dist (Meter, val_) Foot)) Second)) | Meter, Second -> (to_unit, val_) | Meter, Hour -> (to_unit, snd (convert_time (Hour, val_) Second)) | Mile, Second -> (to_unit, snd (convert_dist (Meter,val_) Mile)) | Mile, Hour -> (to_unit, snd (convert_time (Hour, snd (convert_dist (Meter, val_) Mile)) Second)) | _ -> failwith "Unit type not recognized" end | Meter, Hour -> begin match fst to_unit, snd to_unit with | Foot, Second -> (to_unit, snd (convert_time (Second, snd (convert_dist (Meter, val_) Foot)) Hour)) | Foot, Hour -> (to_unit, snd (convert_dist(Meter,val_) Foot)) | Meter, Second -> (to_unit, snd (convert_time(Second, val_) Hour)) | Meter, Hour -> (to_unit, val_) | Mile, Second -> (to_unit, snd (convert_time (Second, snd (convert_dist (Meter, val_) Mile)) Hour)) | Mile, Hour -> (to_unit, snd (convert_dist (Meter, val_) Mile)) | _ -> failwith "Unit type not recognized" end | Mile, Second -> begin match fst to_unit, snd to_unit with | Foot, Second -> (to_unit, snd (convert_dist (Mile, val_) Foot)) | Foot, Hour -> (to_unit, snd (convert_time (Hour, snd (convert_dist (Mile, val_) Foot)) Second)) | Meter, Second -> (to_unit, snd (convert_dist(Mile, val_) Meter)) | Meter, Hour -> (to_unit, snd (convert_time (Hour, snd (convert_dist (Mile, val_) Meter)) Second)) | Mile, Second -> (to_unit, val_) | Mile, Hour -> (to_unit, snd (convert_time (Hour, val_) Second)) | _ -> failwith "Unit type not recognized" end | Mile, Hour -> begin match fst to_unit, snd to_unit with | Foot, Second -> (to_unit, snd (convert_time (Second, snd (convert_dist (Mile, val_) Foot)) Hour)) | Foot, Hour -> (to_unit, snd (convert_dist(Mile,val_) Foot)) | Meter, Second -> (to_unit, snd (convert_time (Second, snd (convert_dist (Mile, val_) Meter)) Hour)) | Meter, Hour -> (to_unit, snd (convert_dist(Mile, val_) Meter)) | Mile, Second -> (to_unit, snd (convert_time(Second, val_) Hour)) | Mile, Hour -> (to_unit, val_) | _ -> failwith "Unit type not recognized" end | _ -> failwith "Unit type not recognized" ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, snd (convert_speed a b_unit) +. b_val) ;;
let passes_da_vinci t = traverse_tree [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 h = cur_el 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 "No empty lists" |h::t -> aux t (h, 1) (h, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec aux first_el l = match l with |[] -> [] |h::t -> (first_el, h) :: (aux h t) in match l with |[] -> failwith "No empty lists" |[x] -> failwith "At least 2 elements" |h::t -> mode (aux h t) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Hour && to_unit = Second then Second, val_ *. 3600. else Hour, val_ /. 3600. ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if from_unit = Foot && to_unit = Meter then Meter, val_ *. 0.3048 else if from_unit = Foot && to_unit = Mile then Mile, val_ /. 5280. else if from_unit = Meter && to_unit = Foot then Foot, val_ /. 0.3048 else if from_unit = Meter && to_unit = Mile then Mile, val_ /. (0.3048 *. 5280.) else if from_unit = Mile && to_unit = Foot then Foot, val_ *. 5280. else Meter, val_ *. (0.3048 *. 5280.) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, (snd(convert_dist (fst from_unit, val_) (fst to_unit))) /. (snd(convert_time (snd from_unit, 1.) (snd to_unit)))) ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = notimplemented () ;; let rec passes_da_vinci t = let rec sum_of_squares_of_list tr x = match tr with |[] -> x |Leaf::t -> sum_of_squares_of_list t x |Branch (thick, subtree) :: t -> sum_of_squares_of_list t (x +. (thick *. thick)) in let rec go_through_subtrees tr = match tr with |[] -> true |Leaf::t -> go_through_subtrees t |Branch (thick, subtree) :: t -> if sum_of_squares_of_list subtree 0. <= thick *. thick && go_through_subtrees subtree then go_through_subtrees t else false in go_through_subtrees [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 if cur_num < max_num then max_el else max_el | head :: tail when head = cur_el -> aux tail (cur_el, cur_num + 1) (max_el, max_num) | head :: tail when cur_num > max_num -> aux tail (head, 1) (cur_el, cur_num) | head :: tail -> aux tail (head, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Empty list given" | head :: tail -> aux tail (head, 1) (head, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl 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, Mile -> (Mile, val_ /. 5280.) | Mile, Foot -> (Foot, val_ *. 5280.) | Foot, Meter -> (Meter, val_ *. 0.3048) | Meter, Foot -> (Meter, val_ /. 0.3048) | Meter, Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Mile, Meter -> (Meter, val_ *. 5280. *. 0.3048) | Foot, Foot -> (Foot, val_) | Meter, Meter -> (Meter, val_) | Mile, Mile -> (Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, (snd (convert_dist (fst from_unit, val_) (fst to_unit))) /. (snd (convert_time (snd from_unit, 1.) (snd 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 = let func_1 t = match t with | Leaf -> 0. | Branch (thickness, _) -> thickness *. thickness in match t with | Leaf -> true | Branch (thickness, trees) -> let x = List.fold_left (fun acc el -> acc +. func_1 el) 0. trees in if x <= thickness *. thickness then List.for_all passes_da_vinci 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 | [] -> if max_num<cur_num then cur_el else max_el | x :: ls when x=cur_el -> aux ls (x, cur_num+1) (max_el,max_num) | x :: ls when max_num<cur_num -> aux ls (x, 1) (cur_el,cur_num) | x :: ls -> aux ls (x,1) (max_el,max_num) in match List.sort compare(l) with | [] -> failwith "Empty List" | x :: ls -> aux ls (x,1) (x,1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let rec aux li b = match li with | [] -> mode b | x :: y :: ls -> aux (y::ls) ((x,y) :: b) | _ -> mode b in aux l [] ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second when to_unit=Hour-> (Hour, val_/.3600.) | Hour when to_unit=Second -> (Second, val_*.3600.) | _ -> (to_unit,val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | _ when from_unit=to_unit -> (to_unit,val_) | Foot -> if to_unit=Meter then (Meter,val_*.0.3048) else (Mile,val_ /. 5280.) | Meter -> if to_unit=Foot then (Foot,val_/.0.3048) else (Mile,val_ /. 0.3048/.5280.) | Mile -> if to_unit=Foot then (Foot,val_*.5280.) else (Meter,val_*.5280.*.0.3048) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with | (x,y) when to_unit=(x,y) -> ((x,y),val_) | (x,y) -> let (a,b)=to_unit in let dis=convert_dist (x,val_) a in let tim=convert_time (y,1.) b in let (_,val1)=dis in let (_,val2)=tim in (to_unit,val1/.val2) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (u,val1)=a in let temp=convert_speed (u,val1) b_unit in let (_,val2)=temp in (b_unit,b_val+.val2) ;;
let passes_da_vinci t = let sumChildren tree = let rec sumList l sum_so_far = match l with | [] -> sum_so_far | Leaf :: ls ->sumList ls sum_so_far | (Branch (u,v)) :: ls -> (sumList ls sum_so_far)+.(u**2.)+.(sumList v sum_so_far) in match tree with | Leaf -> 0. | (Branch (_,v)) -> (sumList v 0.) in let rec branchCorrect li = match li with | [] -> true | Leaf :: ls -> branchCorrect ls | Branch(u,v) :: ls -> if (u**2.)<(sumChildren (Branch(u,v))) then false else branchCorrect ls in match t with | Leaf -> true | (Branch (u,v)) -> match v with | [] -> true | _ -> branchCorrect v && ((u**2.)>=(sumChildren (Branch(u,v)))) ;;
let notimplemented () = failwith "This function is not yet implemented." type dist_unit = Foot | Meter | Mile type time_unit = Second | Hour type speed_unit = dist_unit * time_unit type 'a value = 'a * float type tree = Branch of float * tree list | Leaf let tree_example = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]) ;;
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) : time_unit value = let sec2hour sec = sec /. 3600. in let hour2sec hour = hour *. 3600. in match from_unit, to_unit with | Second, Second -> Second, val_ | Second, Hour -> Hour, sec2hour val_ | Hour, Hour -> Hour, val_ | Hour, Second -> Second, hour2sec val_ ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let ft2m ft = ft *. 0.3048 in let ft2ml ft = ft /. 5280. in let m2ft m = m *. 3.28084 in let m2ml m = m *. 0.000621 in let ml2m ml = ml *. 1609.344 in let ml2ft ml = ml *. 5280. in match from_unit, to_unit with | Foot, Foot -> Foot, val_ | Foot, Meter -> Meter, ft2m val_ | Foot, Mile -> Mile, ft2ml val_ | Meter, Meter -> Meter, val_ | Meter, Foot -> Foot, m2ft val_ | Meter, Mile -> Mile, m2ml val_ | Mile, Mile -> Mile, val_ | Mile, Foot -> Foot, ml2ft val_ | Mile, Meter -> Meter, ml2m 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_ *. 1097.28 | (Foot, Second), (Mile, Second) -> (Mile, Second), val_ *. 0.000189394 | (Foot, Second), (Mile, Hour) -> (Mile, Hour), val_ *. 0.681818 | (Foot, Hour), (Foot, Second) -> (Foot, Second), val_ *. 0.000277778 | (Foot, Hour), (Foot, Hour) -> (Foot, Hour), val_ | (Foot, Hour), (Meter, Second) -> (Meter, Second), val_ *. 0.000084666666666667 | (Foot, Hour), (Meter, Hour) -> (Meter, Hour), val_ *. 0.3048 | (Foot, Hour), (Mile, Second) -> (Mile, Second), val_ *. 0.0000000526094 | (Foot, Hour), (Mile, Hour) -> (Mile, Hour), val_ *. 0.000189394 | (Meter, Second), (Foot, Second) -> (Foot, Second), val_ *. 3.28084 | (Meter, Second), (Foot, Hour) -> (Foot, Hour), val_ *. 11811. | (Meter, Second), (Meter, Second)-> (Meter, Second), val_ | (Meter, Second), (Meter, Hour)-> (Meter, Hour), val_ *. 3600. | (Meter, Second), (Mile, Second)-> (Mile, Second), val_ *. 0.000621371 | (Meter, Second), (Mile, Hour)-> (Mile, Hour), val_ *. 2.23694 | (Meter, Hour), (Foot, Second) -> (Foot, Second), val_ *. 0.000911344 | (Meter, Hour), (Foot, Hour) -> (Foot, Hour), val_ *. 3.28084 | (Meter, Hour), (Meter, Second)-> (Meter, Second), val_ *. 0.000277778 | (Meter, Hour), (Meter, Hour)-> (Meter, Hour), val_ | (Meter, Hour), (Mile, Second)-> (Mile, Second), val_ *. 0.000000172603 | (Meter, Hour), (Mile, Hour)-> (Mile, Hour), val_ *. 0.000621371 | (Mile, Second), (Foot, Second) -> (Foot, Second), val_ *. 5280. | (Mile, Second), (Foot, Hour) -> (Foot, Hour), val_ *. 19010000. | (Mile, Second), (Meter, Second)-> (Meter, Second), val_ *. 1609.34 | (Mile, Second), (Meter, Hour)-> (Meter, Hour), val_ *. 5794000. | (Mile, Second), (Mile, Second)-> (Mile, Second), val_ | (Mile, Second), (Mile, Hour)-> (Mile, Hour), val_ *. 3600. | (Mile, Hour), (Foot, Second) -> (Foot, Second), val_ *. 1.46667 | (Mile, Hour), (Foot, Hour) -> (Foot, Hour), val_ *. 5280. | (Mile, Hour), (Meter, Second)-> (Meter, Second), val_ *. 0.44704 | (Mile, Hour), (Meter, Hour)-> (Meter, Hour), val_ *. 1609.34 | (Mile, Hour), (Mile, Second)-> (Mile, Second), val_ *. 0.000277778 | (Mile, Hour), (Mile, Hour)-> (Mile, Hour), val_ ;;
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = notimplemented () ;; 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) = let sorted_list = List.sort compare l in match sorted_list 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 "cannot find the mode of an empty list" |hd::tl -> aux tl (hd, 1) (hd, 0) ;;
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 l2 = List.rev l in let l3 = List.tl l2 in let l4 = List.rev l3 in mode (List.combine l4 l1) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_, to_unit) with |(Hour, _, Second) -> (Second, val_ *. 3600.) |(Second, _, Hour) -> (Hour, 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, val_, to_unit) with |(Foot, _, Meter) -> (Meter, val_ *. 0.3048) |(Meter, _, Foot) -> (Foot, val_ /. 0.3048) |(Mile, _, Meter) -> (Meter, val_ *. 1609.344) |(Meter, _, Mile) -> (Mile, val_ /. 1609.344) |(Mile, _, Foot) -> (Foot, val_ *. 5280.) |(Foot, _, Mile) -> (Mile, val_ /. 5280.) |(Foot, _, Foot) -> (Foot, val_) |(Mile, _, Mile) -> (Mile, val_) |(Meter, _, Meter) -> (Meter, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist_val = convert_dist (fst from_unit,val_) (fst to_unit) in let time_val = convert_time (snd to_unit, snd(dist_val)) (snd from_unit) in (to_unit, snd(time_val)) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let new_arg1 = convert_speed a b_unit in (b_unit, (snd new_arg1) +. 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 ((sum_ subtree) (acc +. width ** 2.)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> not ((check subtree) && ((width ** 2.) >= sum_ subtree 0.)) || check tl in check [t] ;;
let domain () = failwith "Domain";;
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 when h = cur_el -> if cur_num > max_num then aux t (h,(cur_num+1))(cur_el,cur_num) else aux t (h,(cur_num+1))(max_el, max_num) | h :: t -> aux t (h,1)(max_el, max_num) in match List.sort compare l with | [] -> domain () | h::t -> (aux t (h,1)(h,0)) ;;
let pair_mode (l: 'a list) : 'a * 'a = let r = List.rev (List.tl (List.rev l)) in match l with | [] -> domain () | _ :: t -> match t with | [] -> domain() | _ -> mode((List.combine r t)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | (Hour, Hour) -> (Hour, val_) | (Second, Second) -> (Second, val_) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit ) with | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. 1609.344) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. 1609.344) | (Foot, Foot) -> (Foot, val_) | (Meter, Meter) -> (Meter, val_) | (Mile, Mile) -> (Mile, val_) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let top = convert_dist (fst from_unit, val_) (fst to_unit) in let denom = convert_time (snd from_unit,1.) (snd to_unit) in (to_unit, ((snd top) /. (snd denom))) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let sp_a = convert_speed(a)(b_unit) in let res = (snd sp_a) +. b_val in (b_unit, res) ;;
let passes_da_vinci (t: tree) = let rec all_leaf_wdth_v2 tree_lst a_lst: (float list) = match tree_lst with | [] -> a_lst | Leaf :: xs -> all_leaf_wdth_v2 xs a_lst | Branch (x, _) :: xs -> (all_leaf_wdth_v2 xs (x::a_lst)) in let rec sum_sq lst sum = match lst with | [] -> sum | x :: xs -> sum_sq xs ((x*.x)+.sum) in let curr_br_vin curr_t = match curr_t with | Leaf -> true | Branch(x, tr_list) -> (x*.x) >= (sum_sq (all_leaf_wdth_v2 tr_list []) 0.) in let rec check_all_br t_list c_res = match t_list with | [] -> c_res | Leaf :: xs -> check_all_br xs c_res | x :: xs -> let Branch(_, tr_list) = x in check_all_br xs (c_res && curr_br_vin x && check_all_br tr_list true) in match t with | Leaf -> true | Branch(_, t_list1) -> curr_br_vin t && check_all_br t_list1 true ;;
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 -> let (cur_el, cur_num)= if (hd = cur_el) then (cur_el, cur_num + 1) else (hd,1) in let (max_el, max_num)= if (cur_num > max_num) then (cur_el, cur_num) else (max_el, max_num) in aux tl (cur_el, cur_num) (max_el, max_num);;
let mode (l: 'a list) : 'a = match l with |[] -> failwith "The list is empty" |hd :: _ -> aux (List.sort compare(l)) (hd, 0) (hd,0);;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "The list is empty" | _::tl -> let l1 = tl 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,val_), to_unit with |(Second, x), Hour -> (Hour, x /. 3600.) |(Second, x), Second -> (Second, x ) |(Hour, x), Second -> (Second, x *. 3600.) |(Hour, x), Hour -> (Second, x) ;;
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,val_), to_unit with |(Foot, x), Foot -> (Foot, x) |(Foot, x), Meter -> (Meter, x *. 0.3048) |(Foot, x), Mile -> (Mile, x /. 5280.) |(Meter, x), Meter -> (Meter, x ) |(Meter, x), Foot -> (Foot, x /. 0.3048 ) |(Meter, x), Mile -> (Mile, x /. (5280. *. 0.3048)) |(Mile, x), Mile -> (Mile, x ) |(Mile, x), Meter -> (Meter, x *. (5280. *. 0.3048)) |(Mile, x), Foot -> (Foot, x *. 5280.) ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let a : dist_unit value = (fst(from_unit),val_)in let c = convert_dist (a) (fst(to_unit)) in let b : time_unit value = if (snd(from_unit) = Second) then Hour,snd(c) else Second,snd(c)in let d = if(snd(to_unit) = Second) then convert_time (b) Hour else convert_time (b) Second in to_unit, snd(d) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = b_unit, (snd(convert_speed (a) (b_unit)) +. b_val) ;;
let passes_da_vinci t = check [t] ;;
let mode (l: 'a list) : 'a = if l =[] then failwith "list empty" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match List.sort compare l with | x::t -> if x = cur_el then aux t (x, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux t (x ,1) (cur_el , cur_num) else aux t (x ,1) (max_el, max_num) | [] -> if cur_num > max_num then cur_el else max_el in aux l (List.nth l 0 ,0) ( List.nth l 0 ,0) ;; let rec build l1 l2 acc = match l2 with | [] -> acc | _ -> build (List.nth l2 0) (List.tl l2) (List.append acc [(l1,List.nth l2 0)]) ;;
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Only one element" else match l with | [] -> failwith "list empty" | _ -> let l1 = List.nth l 0 in let l2 = List.tl l in let tuple_list = build l1 l2 [] in mode tuple_list ;; let get_0 (a,_) = a ;; let get_1 (_,a)= a ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_), 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, val_), to_unit with |(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 |((_, _), _) -> from_unit, val_ ;;
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let new_dist = convert_dist (get_0(from_unit),val_) (get_0(to_unit)) in let new_time = convert_time (get_1(from_unit), val_) (get_1(to_unit)) in let new_val = 1./.get_1(convert_time (get_1(from_unit), (1./. get_1(new_dist))) (get_1(to_unit)) ) in let new_speed : speed_unit = (get_0(new_dist), get_0(new_time))in (new_speed, new_val) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted = convert_speed (get_0(a), get_1(a)) b_unit in (b_unit, (get_1(converted) +. b_val)) ;;
let passes_da_vinci t = let rec sum_ l acc : float = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> match subtree with | [] -> sum_ tl (acc +. (width ** 2.)) | Leaf :: tl -> sum_ tl (acc +. (width ** 2.)) | _ -> sum_ subtree (acc +. (width ** 2.)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if width < 0. then failwith "Negative branch width" else if (check subtree && (sum_ subtree 0.) <= width ** 2.) then check tl else false 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 :: y when x = cur_el -> aux y (cur_el, cur_num + 1) (max_el,max_num) | x :: y when cur_num <= max_num -> aux y (x, 1) (max_el,max_num) | x :: y -> aux y (x, 1) (cur_el,cur_num) in match List.sort compare l with | [] -> failwith "make a bigger list and try again" | x :: y -> aux y (x, 1) (x, 1) ;;
let pair_mode (l: 'a list) : 'a * 'a = let x = List.tl l in let z = List.rev l in let y = List.tl z in let p = List.rev y in let m = List.combine p x in mode m ;;
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 | Foot, Mile -> Mile, val_ /. 5280. | Meter, Mile -> Mile, val_ /. (5280. *. 0.3048) | Meter, Foot -> Foot, val_ /. 0.3048 | Mile, Foot -> Foot, val_ *. 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(x,y) = from_unit in let(p,q) = to_unit in let (c,s) = convert_dist(x, val_) p in let (d,r) = convert_time(y, 1.) q in ((p,q), s/.r ) ;;
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let((x,y),z) = a in let (c,s) = convert_speed((x,y),z) b_unit in let q = s+.b_val in (b_unit, q) ;;
let passes_da_vinci t = let rec sum l acc = match l with | [] -> acc | Leaf :: tl -> sum tl acc | Branch(width,subtree) :: tl -> let s = width *. width in let p = s +. acc in sum tl p in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch(width,subtree) :: tl -> let s = sum subtree 0. in let t = width *. width in let q = check subtree in if s <= t && q then check tl else false 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 | cur_el' :: rest -> if cur_el' = cur_el then if cur_num +1 > max_num then aux rest (cur_el',cur_num + 1) (cur_el', cur_num +1) else aux rest (cur_el',cur_num + 1) (max_el, max_num) else if 1 > max_num then aux rest (cur_el',1) (cur_el', 1) else aux rest (cur_el', 1) (max_el, max_num) in let l' = List.sort compare l in match l' with | [] -> notimplemented () | first_el :: rest -> aux l' (first_el, 0) (first_el, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, value) -> if to_unit = Second then (Second ,value) else (Second ,value /. 3600.) | (Hour, value) -> if to_unit = Second then (Hour, value *. 3600.) else (Hour, value) ;;
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 | Leaf :: silbling -> chilren_branch_width_sum silbling acc | Branch (width, _) :: silbling -> chilren_branch_width_sum silbling (acc +. width *. width) in let rec all_branches t: bool = match t with | [] -> true | Leaf :: tail -> all_branches tail | Branch(width, l) :: tail -> if chilren_branch_width_sum l 0. <= width *. width && all_branches l then all_branches tail else false in all_branches [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 | cur_el' :: rest -> if cur_el' = cur_el then if cur_num +1 > max_num then aux rest (cur_el',cur_num + 1) (cur_el', cur_num +1) else aux rest (cur_el',cur_num + 1) (max_el, max_num) else if 1 > max_num then aux rest (cur_el',1) (cur_el', 1) else aux rest (cur_el', 1) (max_el, max_num) in let l' = List.sort compare l in match l' with | [] -> notimplemented () | first_el :: rest -> aux l' (first_el, 0) (first_el, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> notimplemented () | _ -> mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, value) -> if to_unit = Second then (Second ,value) else if to_unit = Hour then (Second ,value /. 3600.) else notimplemented () | (Hour, value) -> if to_unit = Second then (Hour, value *. 3600.) else if to_unit = Hour then (Hour, value) else notimplemented () | (_, value) -> notimplemented () ;;
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 if to_unit = Mile then (Mile, value /. 5280.) else notimplemented () | (Meter, value) -> if to_unit= Foot then (Foot, value /. 0.3048) else if to_unit = Meter then (Meter, value) else if to_unit = Mile then (Mile, value *. (1. /. (5280. *. 0.3048))) else notimplemented () | (Mile, value) -> if to_unit= Foot then (Foot, value *. 5280.) else if to_unit = Meter then (Meter, value *. 0.3048 *. 5280.) else if to_unit = Mile then (Mile, value) else notimplemented () ;;
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 | Leaf :: silbling -> chilren_branch_width_sum silbling acc | Branch (width, _) :: silbling -> chilren_branch_width_sum silbling (acc +. width *. width) in let rec all_branches t: bool = match t with | [] -> true | Leaf :: tail -> all_branches tail | Branch(width, l) :: tail -> if chilren_branch_width_sum l 0. <= width *. width && all_branches l then all_branches tail else false in all_branches [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 | cur_el' :: rest -> if cur_el' = cur_el then if cur_num +1 > max_num then aux rest (cur_el',cur_num + 1) (cur_el', cur_num +1) else aux rest (cur_el',cur_num + 1) (max_el, max_num) else if 1 > max_num then aux rest (cur_el',1) (cur_el', 1) else aux rest (cur_el', 1) (max_el, max_num) in let l' = List.sort compare l in match l' with | [] -> notimplemented () | first_el :: rest -> aux l' (first_el, 0) (first_el, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> notimplemented () | _ :: [] -> notimplemented () | _ -> mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, value) -> if to_unit = Second then (Second ,value) else if to_unit = Hour then (Second ,value /. 3600.) else notimplemented () | (Hour, value) -> if to_unit = Second then (Hour, value *. 3600.) else if to_unit = Hour then (Hour, value) else notimplemented () | (_, value) -> notimplemented () ;;
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 if to_unit = Mile then (Mile, value /. 5280.) else notimplemented () | (Meter, value) -> if to_unit= Foot then (Foot, value /. 0.3048) else if to_unit = Meter then (Meter, value) else if to_unit = Mile then (Mile, value *. (1. /. (5280. *. 0.3048))) else notimplemented () | (Mile, value) -> if to_unit= Foot then (Foot, value *. 5280.) else if to_unit = Meter then (Meter, value *. 0.3048 *. 5280.) else if to_unit = Mile then (Mile, value) else notimplemented () ;;
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 | Leaf :: silbling -> chilren_branch_width_sum silbling acc | Branch (width, _) :: silbling -> chilren_branch_width_sum silbling (acc +. width *. width) in let rec all_branches t: bool = match t with | [] -> true | Leaf :: tail -> all_branches tail | Branch(width, l) :: tail -> if chilren_branch_width_sum l 0. <= width *. width && all_branches l then all_branches tail else false in all_branches [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 | [] -> max_el | cur_el' :: rest -> if cur_el' = cur_el then if cur_num +1 > max_num then aux rest (cur_el',cur_num + 1) (cur_el', cur_num +1) else aux rest (cur_el',cur_num + 1) (max_el, max_num) else if 1 > max_num then aux rest (cur_el',1) (cur_el', 1) else aux rest (cur_el', 1) (max_el, max_num) in let l' = List.sort compare l in match l' with | [] -> notimplemented () | first_el :: rest -> aux l' (first_el, 0) (first_el, 0) ;;
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (List.rev (List.tl (List.rev l))) (List.tl l)) ;;
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, value) -> if to_unit = Second then (Second ,value) else (Second ,value /. 3600.) | (Hour, value) -> if to_unit = Second then (Hour, value *. 3600.) else (Hour, value) ;;