text
stringlengths 0
601k
|
---|
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((a,b), val_), (c, d) = ((from_unit, val_), to_unit) in (a, b), second (convert_dist (a, val_) c) /. second (convert_time (b, 1.) d) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, second (convert_speed (a) (b_unit)) +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. width ** 2.) in let rec check l = match l with | [] -> true | Leaf :: tl -> 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 ((prev_el, prev_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if prev_num < max_num then max_el else prev_el | h :: t -> aux t (if h = prev_el then (prev_el, prev_num + 1) else (h, 1)) (if prev_num >= max_num then (prev_el, prev_num + 1) else (max_el, max_num)) in let sorted = List.sort compare l in match sorted with | [] -> failwith "It's empty !!!" | h :: t -> aux t (h, 1) (h, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (list_remaining: 'a list) (list_built: ('a * 'a) list) = match list_remaining with | [] -> failwith "Empty list" | [ _ ] -> failwith "Too few elements" | item1 :: [ item2 ] -> (item1, item2) :: list_built | h1 :: h2 :: t -> aux (h2 :: t) ((h1, h2) :: list_built) in mode (aux l []) ;; |
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_) | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) ;; let rec 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) -> (Meter, val_ *. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Meter, Mile) -> convert_dist (convert_dist (Meter, val_) Foot) Mile | (Mile, Meter) -> convert_dist (convert_dist (Mile, val_) Foot) Meter ;; |
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 (to_unit, snd (convert_time (to_time, (snd (convert_dist (from_dist, val_) to_dist))) from_time)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (target_distance_unit, target_time_unit) = speed_unit in let time_val_converted = snd (convert_time time target_time_unit) in (target_distance_unit, time_val_converted *. speed_val) ;; let tree0 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]);; let tree1 = Branch (5., [ Branch (6., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]);; let tree2 = Branch (5., [ Branch (2., [Leaf; Leaf; Leaf]); Leaf; Branch (2., [ Branch (2., []) ]) ]);; let tree3 = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., [ Branch (2., [ Branch (2., []) ]); Branch (2., [ Branch (2., []) ]) ]) ]);; let tree4 = Leaf;; let rec passes_da_vinci t = let largeur_de noeud = match noeud with | Leaf -> 0. | Branch (largeur, _) -> largeur in let carre (nombre: float) : float = nombre *. nombre in let somme (l: float list) = let rec somme_rec (l: float list) acc = match l with | [] -> acc | h :: t -> somme_rec t (h +. acc) in somme_rec l 0. in match t with | Leaf -> true | Branch (largeur, enfants) -> let somme_des_carres_des_largeurs_des_enfants = somme (List.map carre (List.map largeur_de enfants)) in if somme_des_carres_des_largeurs_des_enfants > carre largeur then false else let rec aux enfants = match enfants with | [] -> true | h :: t -> if passes_da_vinci h then aux t else false in aux enfants ;; |
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 max_el else cur_el | x :: xs -> if x = cur_el then aux xs (x,cur_num+1) (max_el,max_num) else ( if x != cur_el && cur_num > max_num then aux xs (x,1) (cur_el,cur_num) else aux xs (x,1) (max_el,max_num) ) in match l with | [] -> failwith "Invalid Input: empty list" | x :: _ -> aux (List.sort compare l) (x,0) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid Input: empty list" | [_] -> failwith "Invalid Input: no pair exists" | _ -> let l2 = List.combine l (List.tl l @ [List.hd l]) in let l3 = List.tl (List.rev l2) in mode l3 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with | (Hour,Second) -> (Second,val_ *. 3600.) | (Second,Hour) -> (Hour,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,Mile) -> (Mile,val_ /. 5280.) | (Mile,Foot) -> (Foot,val_ *. 5280.) | (Foot,Meter) -> (Meter,val_ *. 0.3048) | (Meter,Foot) -> (Foot,val_ /. 0.3048) | (Meter,Mile) -> (Mile,val_ /. 0.3048 /. 5280.) | (Mile,Meter) -> (Meter,val_ *. 5280. *. 0.3048) | (_,_) -> (to_unit,val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_dist_unit,from_time_unit),(to_dist_unit,to_time_unit)) = (from_unit, to_unit) in let dist = convert_dist (from_dist_unit,val_) to_dist_unit in let time = convert_time (from_time_unit,1.) to_time_unit in let ((d_unit,d_value),(t_unit,t_value)) = (dist,time) in ((d_unit,t_unit), d_value /. t_value) ;; |
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 let (_,a_val) = a_speed in (b_unit,a_val +. b_val) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch (val',l) -> let rec helper l = match l with | [] -> true | x::xs -> match x with | Leaf -> true && helper xs | Branch (val_,l_) -> (cal_sum l_ <= val_ *. val_) && helper l_ && helper xs in (cal_sum l <= val' *. val') && helper l;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if max_num >= cur_num then max_el else cur_el | x :: xs -> if x = cur_el then aux xs (x,cur_num+1) (max_el,max_num) else ( if x <> cur_el && cur_num > max_num then aux xs (x,1) (cur_el,cur_num) else aux xs (x,1) (max_el,max_num) ) in match l with | [] -> failwith "Invalid Input: empty list" | x :: _ -> aux (List.sort compare l) (x,0) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid Input: empty list" | [_] -> failwith "Invalid Input: no pair exists" | _ -> let l2 = List.combine l (List.tl l @ [List.hd l]) in let l3 = List.tl (List.rev l2) in mode l3 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with | (Hour,Second) -> (Second,val_ *. 3600.) | (Second,Hour) -> (Hour,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,Mile) -> (Mile,val_ /. 5280.) | (Mile,Foot) -> (Foot,val_ *. 5280.) | (Foot,Meter) -> (Meter,val_ *. 0.3048) | (Meter,Foot) -> (Foot,val_ /. 0.3048) | (Meter,Mile) -> (Mile,val_ /. 0.3048 /. 5280.) | (Mile,Meter) -> (Meter,val_ *. 5280. *. 0.3048) | (_,_) -> (to_unit,val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_dist_unit,from_time_unit),(to_dist_unit,to_time_unit)) = (from_unit, to_unit) in let dist = convert_dist (from_dist_unit,val_) to_dist_unit in let time = convert_time (from_time_unit,1.) to_time_unit in let ((d_unit,d_value),(t_unit,t_value)) = (dist,time) in ((d_unit,t_unit), d_value /. t_value) ;; |
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 let (_,a_val) = a_speed in (b_unit,a_val +. b_val) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch (val',l') -> (cal_sum l' <= val' *. val') && tree_branch l' ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if max_num >= cur_num then max_el else cur_el | x :: xs -> if x = cur_el then aux xs (x,cur_num+1) (max_el,max_num) else ( if x <> cur_el && cur_num > max_num then aux xs (x,1) (cur_el,cur_num) else aux xs (x,1) (max_el,max_num) ) in match l with | [] -> failwith "Invalid Input: empty list" | x :: _ -> aux (List.sort compare l) (x,0) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid Input: empty list" | _ -> let l2 = List.combine l (List.tl l @ [List.hd l]) in let l3 = List.tl (List.rev l2) in match l3 with | [] -> failwith "Invalid Input: no pair exists" | _ -> mode l3 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with | (Hour,Second) -> (Second,val_ *. 3600.) | (Second,Hour) -> (Hour,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,Mile) -> (Mile,val_ /. 5280.) | (Mile,Foot) -> (Foot,val_ *. 5280.) | (Foot,Meter) -> (Meter,val_ *. 0.3048) | (Meter,Foot) -> (Foot,val_ /. 0.3048) | (Meter,Mile) -> (Mile,val_ /. 0.3048 /. 5280.) | (Mile,Meter) -> (Meter,val_ *. 5280. *. 0.3048) | (_,_) -> (to_unit,val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_dist_unit,from_time_unit),(to_dist_unit,to_time_unit)) = (from_unit, to_unit) in let dist = convert_dist (from_dist_unit,val_) to_dist_unit in let time = convert_time (from_time_unit,1.) to_time_unit in let ((d_unit,d_value),(t_unit,t_value)) = (dist,time) in ((d_unit,t_unit), d_value /. t_value) ;; |
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 let (_,a_val) = a_speed in (b_unit,a_val +. b_val) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch (val',l') -> (cal_sum l' <= val' *. val') && tree_branch l' ;; |
let getFirst (l: 'a list) : 'a = match l with | x::xs -> x | _ -> failwith "empty 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 | [] -> (match cur_num > max_num with | true -> cur_el | false -> max_el ) | x::xs -> (match x = cur_el with | true -> aux xs (cur_el, cur_num+1) (max_el, max_num) | false -> (match cur_num > max_num with | true -> aux xs (x, 1) (cur_el, cur_num) | false -> aux xs (x, 1) (max_el, max_num) ) ) in aux (List.sort compare l) ((getFirst (List.sort compare l)),0) ((getFirst (List.sort compare l)), 0) ;; let remFirst (l: 'a list) : 'a list = match l with | x::xs -> xs | _ -> failwith "empty list" let remLast (l: 'a list) : 'a list = List.rev (remFirst (List.rev l));; |
let pair_mode (l: 'a list) : 'a * 'a = mode (List.combine (remLast l) (remFirst l)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Hour -> (Hour, val_ /. 3600.) | _ -> (Second, val_)) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | _ -> (Hour, val_)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> (match to_unit with | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) | _ -> (Foot, val_)) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Mile -> (Mile, val_ /. 0.3048 /. 5280.) | _ -> (Meter, val_)) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) | _ -> (Mile, val_)) ;; let getT1 (a,b) = a;; let getT2 (a, b) = b;; let getT3 ((a, b), c) = c;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match to_unit with | (Foot, Second) -> ((Foot, Second), getT2 (convert_dist (getT1 from_unit, getT2 (convert_time (getT2 to_unit, val_) (getT2 from_unit))) Foot)) | (Meter, Second) -> ((Meter, Second), getT2 (convert_dist (getT1 from_unit, getT2 (convert_time (getT2 to_unit, val_) (getT2 from_unit))) Meter)) | (Mile, Second) -> ((Mile, Second), getT2 (convert_dist (getT1 from_unit, getT2 (convert_time (getT2 to_unit, val_) (getT2 from_unit))) Mile)) | (Foot, Hour) -> ((Foot, Hour), getT2 (convert_dist (getT1 from_unit, getT2 (convert_time (getT2 to_unit, val_) (getT2 from_unit))) Foot)) | (Meter, Hour) -> ((Meter, Hour), getT2 (convert_dist (getT1 from_unit, getT2 (convert_time (getT2 to_unit, val_) (getT2 from_unit))) Meter)) | (Mile, Hour) -> ((Mile, Hour), getT2 (convert_dist (getT1 from_unit, getT2 (convert_time (getT2 to_unit, val_) (getT2 from_unit))) Mile)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = match speed_unit with | (Foot, Second) -> (Foot, (getT2 time) *. (getT3 (convert_speed ((Foot, Second), speed_val) (Foot, getT1 time)))) | (Meter, Second) -> (Meter, (getT2 time) *. (getT3 (convert_speed ((Meter, Second), speed_val) (Meter, getT1 time)))) | (Mile, Second) -> (Mile, (getT2 time) *. (getT3 (convert_speed ((Mile, Second), speed_val) (Mile, getT1 time)))) | (Foot, Hour) -> (Foot, (getT2 time) *. (getT3 (convert_speed ((Foot, Hour), speed_val) (Foot, getT1 time)))) | (Meter, Hour) -> (Meter, (getT2 time) *. (getT3 (convert_speed ((Meter, Hour), speed_val) (Meter, getT1 time)))) | (Mile, Hour) -> (Mile, (getT2 time) *. (getT3 (convert_speed ((Mile, Hour), speed_val) (Mile, getT1 time)))) ;; let rec mean (l: int list) tot count: float = match l with | [] -> float_of_int tot /. count | x::xs -> mean xs (tot+x) (count +. 1.) ;; let sumOfSquares (l: int list): float = let rec sos (l1: int list) (l2: int list) meanN (finalSum: float) = match l1 with | [] -> finalSum | x::xs -> sos xs l2 meanN (finalSum +. ((float_of_int x -. meanN) *. (float_of_int x -. meanN))) in sos l l (mean l 0 0.) 0. ;; let treeSummer (t: tree): float = let sums (node: tree) total = if node = Leaf then total else total +. sumOfSquares([]) in sums t 0. ;; let rec passes_da_vinci t = not ((treeSummer t) < (sumOfSquares [])) ;; |
let mode (l: 'a list) : 'a = let rec aux l (cl,cn) (ml,mn) : 'a = match l with | [] -> ml | hd :: tl -> if cl = hd then if cn >= mn then aux tl (hd,cn+1) (cl,cn+1) else aux tl (hd,cn+1) (ml,mn) else aux tl (hd,1) (ml,mn) 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 = match l with | _::[] -> failwith "This list has only 1 element!" | [] -> failwith "This list is empty!" | _ -> let (_ :: no_head) = l in let (_ :: no_tail) = (List.rev l) in mode (List.combine (List.rev no_tail) no_head) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit,to_unit with | Second, Second -> (to_unit,val_) | Second, Hour -> (to_unit,val_ /. 3600.) | Hour,Second -> (to_unit,3600. *. val_) | Hour,Hour -> (to_unit,val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit,to_unit with | Foot, Meter -> (to_unit,val_ *. 0.3048) | Foot, Mile -> (to_unit,val_ /. 5280.) | Meter,Foot -> (to_unit,val_ /. 0.3048) | Meter,Mile -> (to_unit,val_ /. 1609.344) | Mile, Foot -> (to_unit,val_ *. 5280.) | Mile, Meter -> (to_unit,val_ *. 1609.344) | Foot,Foot -> (to_unit,val_) | Meter,Meter -> (to_unit,val_) | Mile,Mile -> (to_unit,val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist1 = snd (convert_dist (fst from_unit, val_) (fst to_unit)) in let inv1 = snd (convert_time (snd from_unit, 1.) (snd to_unit)) in (to_unit, dist1 /. inv1) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let t = snd(convert_time (fst time,snd time) (snd speed_unit)) in let res = speed_val *. t in (fst speed_unit,res) ;; let get_width (t : tree) : float = match t with | Leaf -> 0. | Branch (w, _) -> w ;; let rec iter t = match t with | Leaf -> 0. | Branch (w,st) -> w**2. +. ( let rec help st_arr = match st_arr with | [] -> 0. | hd::tl -> iter hd +. help tl in help st ) ;; let rec passes_da_vinci t = match t with | Leaf -> ((iter t) -. (get_width t)**2.) <= (get_width t)**2. | Branch (_,st) -> let rec help st_arr = match st_arr with | [] -> ((iter t) -. (get_width t)**2.) <= (get_width t)**2. | hd :: tl -> passes_da_vinci hd && help tl in help st ;; |
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 compare hd cur_el == 0 then (if compare hd max_el == 0 then aux tl (cur_el, cur_num +1) (max_el, max_num+1) else (if cur_num +1 > 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 compare hd max_el == 0 then aux tl (hd, max_num +1) (max_el, max_num+1) else (if 1 > max_num then aux tl (hd, 1) (hd, 1) else aux tl (hd, 1) (max_el, max_num) ) ) in if l == [] then failwith "Empty list" else aux (List.sort compare l) (List.nth l 0, 0) (List.nth l 0, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l == [] || List.length l == 1 then failwith "List not long enough" else ( let l1 = l in let l2 = l in let (_ :: no_head1) = l1 in let (_ :: no_head2) = List.rev l2 in mode (List.combine (List.rev no_head2) no_head1) ) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (to_unit, val_ /. 3600.) | (Hour, Second) -> (to_unit, val_ *. 3600.) | _ -> (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) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | (Foot, Mile) -> (to_unit, val_ /. 5280.) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Meter, Mile) -> (to_unit, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (to_unit, (val_ *. 5280.) *. 0.3048) | _ -> (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)), val_) (fst(to_unit))) /. snd(convert_time ((snd(from_unit)), 1.) (snd(to_unit)))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, b_val +. snd(convert_speed a b_unit)) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> true | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> ( if width *. width > acc then false else sum_ tl (acc -. width *. width) ) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (if (sum_ subtree (width *. width) && check subtree) == false then false else check tl ) in check [t] ;; |
let failure () = failwith "Wrong Input Type" ;; |
let mode (l: 'a list) : 'a = match l with | [] -> failure () | _ -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if (List.length (List.tl l) != 0) then let filtered = (List.filter (fun x -> x = List.hd l) l) in if List.length filtered > max_num then aux (List.filter (fun x -> x != List.hd l) l) (List.hd l, List.length filtered) (List.hd l, List.length filtered) else aux (List.filter(fun x -> x != List.hd l) l) (List.hd l, List.length filtered) (max_el, max_num) else max_el in aux (List.sort compare l) (List.hd l, 1) (List.hd l, 1);; let empty = [];; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failure () | _ -> let rec aux l newlist = if (List.length (List.tl l) != 1) then aux (List.tl l) ((List.hd l, List.nth l 1) :: newlist) else mode newlist in aux l empty ;; |
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 | 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 | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) ) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /. 1609.344) ) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 1609.344) | Mile -> (Mile, val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (((fst to_unit), (snd to_unit)), snd (convert_dist (fst from_unit, val_) (fst to_unit)) /. snd (convert_time (snd from_unit, 1.) (snd to_unit))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (((fst b_unit),(snd b_unit)), b_val +. snd (convert_speed a b_unit)) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf ::tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. width *. width) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> ( if 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; | x :: xs -> if x = cur_el then if cur_num+1 > max_num then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else aux xs (cur_el, cur_num+1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in aux l (List.hd l, 0) (List.hd l, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (l: 'a list) (tuple_list: ('a * 'a) list ) = match l with |[] -> tuple_list |_ :: [] -> tuple_list |x::xs -> (x, List.hd xs):: aux xs tuple_list in mode (aux l []) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_)) | Second -> (match to_unit with |Second -> (Second, val_) |Hour -> (Hour, val_ /. 3600.)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> (match to_unit with |Meter -> (Meter, val_ *. 0.3048) |Mile -> (Mile, val_ /. 5280.) |Foot -> (Foot, val_)) | Meter -> (match to_unit with |Meter -> (Meter, val_) |Mile -> (Mile, val_ /. 1609.344) |Foot -> (Foot, val_ /. 0.3048)) | Mile -> (match to_unit with |Meter -> (Meter, val_ *. 1609.344) |Mile -> (Mile, val_) |Foot -> (Foot, val_ *. 5280.)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (unit_dist, unit_time) = from_unit in let (to_dist, to_time) = to_unit in let (new_dist, newval_) = convert_dist(unit_dist, val_) to_dist in let (new_time, newerval_) = convert_time(to_time, newval_) unit_time in (new_dist, new_time), newerval_ ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit, time_unit) = speed_unit in let (real_time_unit, real_time_val) = time in let (newspeed_unit, newspeedvalue) = convert_speed ((dist_unit, time_unit), speed_val) (dist_unit, real_time_unit) in let (good_dist, good_time) = newspeed_unit in (good_dist, newspeedvalue*.real_time_val) ;; let rec sum_squares (list_:tree list) (cum: float)= match list_ with |[] -> cum |Leaf::x -> sum_squares x cum |Branch(f, list)::x -> sum_squares x (cum +. (f**2.)) ;; let rec passes_da_vinci t = match t with |Leaf -> true |Branch(f, tree) -> if (f**2.) < (sum_squares tree 0.) then false else ( match tree with |[] -> true |x::xs -> passes_da_vinci x ) ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl -> (if hd = cur_el then aux tl (cur_el, cur_num + 1) (max_el, max_num) else (if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num))) in let sorted_l = List.sort compare l in match sorted_l with | [] -> failwith "Input list is empty" | x :: xs -> aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Input list is empty" | x :: [] -> failwith "Input list has only one element" | _ -> (let (_ :: l1) = l in let (_ :: l2_rev) = List.rev l in let l2 = List.rev l2_rev in mode (List.combine l2 l1)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = 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 | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.)) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /. 0.3048 /. 5280.)) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) | Mile -> (Mile, val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let numer = snd (convert_dist (fst from_unit, 1.) (fst to_unit)) in let denom = snd (convert_time (snd from_unit, 1.) (snd to_unit)) in (to_unit, val_ *. numer /. denom) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_unit, a_val) = convert_speed a b_unit in (b_unit, a_val +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. width**2.) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (if ((check subtree) && ((sum_ subtree 0.) <= width ** 2.)) then 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 | h :: xs -> ( if h = cur_el then aux xs (cur_el , cur_num + 1) (max_el, max_num) else (if cur_num > max_num then aux xs (h , 1) (cur_el, cur_num) else ( aux xs (h , 1) (max_el, max_num) ))) in if( l = []) then failwith "Empty list" else let sortedList = List.sort compare l in let x :: xs = sortedList in aux xs (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec make_list l1 l2 = match l1 with |[] -> l2 | x :: xs -> match xs with |[] -> l2 | y :: ys -> make_list xs ((x,y) :: l2) in let l2 = make_list l [] in mode l2 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if (val_ < 0.0) then failwith "No negative number" else 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 = if (val_ < 0.0) then failwith "No negative number" else let meter_mile = 0.3048 *. 5280. in match from_unit with |Foot -> if ( to_unit = Mile) then (Mile , val_ /. 5280.) else (if (to_unit = Meter) then (Meter, val_ *.0.3048) else (Foot, val_)) |Meter -> if ( to_unit = Mile) then (Mile , val_ /. meter_mile) else (if (to_unit = Foot) then (Foot, val_ /.0.3048) else (Meter, val_)) |Mile -> if ( to_unit = Meter) then (Mile , val_ *. 5280. *. 0.3048) else (if (to_unit = Foot) then (Foot, val_ *. 5280.) else (Mile, val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ( _ ,dist) = convert_dist (fst from_unit, val_) (fst to_unit) in let ( _ , time) = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, dist /. time) ;; |
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, snd a_val +. b_val) ;; |
let passes_da_vinci t = let rec check t = match t with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> ( width *. width >= sum_square (subtree)) && check subtree && 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 |[] -> (match cur_num > max_num with | true -> cur_el | _ -> max_el ) |_ -> (match ((cur_el = (List.hd l)),(cur_num + 1) > max_num) with | true,false -> aux (List.tl l) ((List.hd l),cur_num+1)(max_el,max_num) | true,true -> aux (List.tl l) ((List.hd l),cur_num+1)(cur_el,cur_num+1) | false,_ -> aux (List.tl l) ((List.hd l),1)(max_el,max_num)) in match l with | [] -> failwith "Undefined input." | _ -> let ls = List.sort compare l in aux (List.tl ls) ((List.hd ls),1) ((List.hd ls),1) ;; let rec c_list (l: 'a list)(cl: 'a list list)(i : int)(len : int):('a list) list = match i=len with | true -> cl | _ -> let el1 = List.nth l i in let el2 = List.nth l (i+1) in c_list l ((el1::[el2]) :: cl) (i+1) len ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input." | _ -> let cl = c_list l [] 0 ((List.length l) - 1) in let el = mode cl in (List.nth el 0,List.nth el 1) ;; |
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 with | Foot ->(match to_unit with | Meter -> (Meter, (val_ *. 0.3048)) | Mile -> (Mile, (val_ /. 5280.)) | _ -> (Foot, val_)) | Meter ->(match to_unit with | Foot -> (Foot, (val_ /. 0.3048)) | Mile -> (Mile, (val_ /. 0.3048 /. 5280.)) | _ -> (Meter, val_)) | _ -> (match to_unit with | Foot -> (Foot, (val_ *. 5280.)) | Meter -> (Meter, (val_ *. 5280. *. 0.3048)) | _ -> (Mile, val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist = convert_dist ((fst from_unit), (val_)) (fst to_unit) in let time = convert_time ((snd from_unit), (1.)) (snd to_unit) in (to_unit, ((snd dist) /. (snd time))) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let timeA = convert_time ((fst time), (snd time)) (snd speed_unit) in ((fst speed_unit), ((snd timeA) *. speed_val)) ;; let rec list_sqrsum (nodes: tree list) : float = match nodes with | [] -> 0. | x :: xs -> match x with | Branch(val_, _nodes) -> (val_ *. val_) +. list_sqrsum xs | _ -> list_sqrsum xs ;; let rec passes_da_vinci t = match t with | Branch(val_, nodes) -> let branch_square = (val_) *. (val_) in let children_square = list_sqrsum (nodes) in let rec child_traversal (nodes2 : tree list) : bool = ( match nodes2 with | [] -> true | x :: xs -> (passes_da_vinci x) && (child_traversal xs)) in (branch_square >= children_square) && (child_traversal nodes) | _ -> true ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "Error: Empty:" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] when cur_num > max_num -> cur_el |[] -> max_el |x::xs when x = cur_el -> aux (List.tl l) (cur_el, cur_num + 1) (max_el, max_num) |x::xs when cur_num > max_num -> aux (List.tl l) (List.hd l, 1) (cur_el, cur_num) |x::xs -> aux (List.tl l) (List.hd l, 1) (max_el, max_num) in aux (List.sort compare l) (List.nth l 0,0) (List.nth l 0,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l2 = List.rev(List.tl(List.rev l)) in let l3 = List.combine l2 l1 in mode l3 ;; |
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) | (Second, Second) -> (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with |(Foot, Foot) | (Mile, Mile) | (Meter, Meter) -> (from_unit, val_) |(Foot,Meter) -> (Meter, val_ *. 0.3048) |(Foot, Mile) ->(Mile, val_ *. (1./.5280.)) |(Meter, Foot) ->(Foot, val_ *. (1. /. 0.3048)) |(Meter, Mile) ->(Mile, val_ /. 1609.344) |(Mile, Meter) ->(Meter, val_ *. 1609.344) |(Mile, Foot) ->(Foot, val_*. 5280.) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let distance = fst(from_unit) in let time = snd(from_unit) in let numer = convert_dist (distance, val_) (fst(to_unit)) in let denom = convert_time (time, 1.) (snd(to_unit)) in (to_unit, snd(numer) /. snd(denom)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let var = convert_speed (fst(a), snd(a)) b_unit in (b_unit, snd(var) +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l w = match l with |[] -> true |Leaf :: tl -> true |Branch(width, subtree) :: tl -> true in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> true 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 | h::t -> if compare h cur_el = 0 then if (succ cur_num) > max_num then aux t (h, succ cur_num) (h, succ cur_num) else aux t (h, succ cur_num) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in let sorted = List.sort compare l in aux sorted (List.hd sorted, 0) (List.hd sorted, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux l1 l2 prev = match l1 with | [] -> l2 | h::t -> aux t ((prev, h)::l2) h in aux (List.tl l) [] (List.hd l) |> mode ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let from_to = (from_unit, to_unit) in match from_to with | (Second, Hour) -> (Hour, val_ /. 3600.0) | (Hour, Second) -> (Second, val_ *. 3600.0) | (Hour, Hour) -> (Hour, val_) | (Second, Second) -> (Second, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let from_to = (from_unit, to_unit) in match from_to with | (Foot, Foot) -> (Foot, val_) | (Meter, Meter) -> (Meter, val_) | (Mile, Mile) -> (Mile, val_) | (Foot, Meter) -> (Meter, 0.3048 *. val_) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.0) | (Mile, Foot) -> (Foot, val_ *. 5280.0) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.0) | (Mile, Meter) -> (Meter, (val_ *. 5280.0) *. 0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_dist, from_time), (to_dist, to_time)) = (from_unit, to_unit) in let c_dist = convert_dist (from_dist, val_) to_dist in let c_time = convert_time (to_time, snd c_dist) from_time in ((to_dist, to_time), snd c_time) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let c_t = snd speed_unit |> convert_time time in (fst speed_unit, snd c_t *. speed_val) ;; let sums_tlis tlis = let f x y = match y with | Leaf -> x | Branch (val_, _) -> x +. val_ *. val_ in List.fold_left f 0. tlis ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (val_, list) -> if sums_tlis list <= (val_ *. val_) then List.for_all passes_da_vinci list 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 cur_num > max_num then cur_el else max_el | x :: xs -> if cur_el = x then aux xs (x, (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 "list cannot be empty" | x :: xs -> aux xs (x,1) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let help123 (li: 'a list) : 'b list = if List.length(li) < 2 then failwith "list cannot be empty or with 1 element" else let helper1 (l1: 'a list): 'a list = let hd::tl = l1 in tl in let s1 = helper1 li in let s2 = List.rev li in let s3 = helper1 s2 in let s4 = List.rev s3 in List.combine s4 s1 in let call = help123(l) in mode call ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> if from_unit = Second then (Second, val_) else (Second,(val_ *. 3600.)) | Hour -> if from_unit = Hour then (Hour, val_) else (Hour, (val_ /. 3600.)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> if from_unit = Foot then (Foot, val_) else if from_unit = Meter then (Meter, (val_ /. 0.3048)) else (Mile, (val_ *. 5280.)) | Meter -> if from_unit = Foot then (Foot, (val_ *. 0.3048)) else if from_unit = Meter then (Meter, val_) else (Mile, (val_ *. 1609.344)) | Mile -> if from_unit = Foot then (Foot, (val_ /. 5280. )) else if from_unit = Meter then (Meter, (val_ /. 1609.344)) else (Mile, val_) ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.