text
stringlengths 0
601k
|
---|
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 (dist: dist_unit value) = convert_dist ((fst from_unit), 1.) (fst to_unit) in let (time: time_unit value) = convert_time ((snd from_unit), 1.) (snd to_unit) in (((fst to_unit), (snd from_unit)), val_ *. (snd dist) /. (snd time)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speed: speed_unit value) = convert_speed ((fst (fst a), snd (fst a)), snd a) b_unit in (b_unit, snd speed +. b_val) ;; |
let passes_da_vinci t = let rec sum (lst: tree list) (add: float): float = match lst with | Branch (diameter, _) :: next -> sum next (diameter *. diameter +. add) | Leaf :: next -> sum next add | _ -> add in let rec check_branch (branch: tree list): bool = (match branch with | Branch (diameter, lst) :: next -> if ((diameter *. diameter) >= sum lst 0.) then check_branch lst && check_branch next else false | Leaf :: next -> check_branch next | _ -> true ) in check_branch [t] ;; |
let mode (l: 'a list) : 'a = let rec aux lst ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | element :: lst -> if element = cur_el then aux lst (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux lst (element, 1) (cur_el, cur_num) else aux lst (element, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input." | element :: lst -> aux lst (element, 1) (element, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec run (lst: 'a list) (maintain: ('a * 'a) list) (last: 'a) = match lst with | [] -> mode maintain | element :: lst -> run lst ((last, element) :: maintain) element in match l with | [] -> failwith "Undefined input." | one :: [] -> failwith "Input size too small." | element :: lst -> run lst ([]) element ;; |
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 (dist: dist_unit value) = convert_dist ((fst from_unit), 1.) (fst to_unit) in let (time: time_unit value) = convert_time ((snd from_unit), 1.) (snd to_unit) in (((fst to_unit), (snd from_unit)), val_ *. (snd dist) /. (snd time)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speed: speed_unit value) = convert_speed ((fst (fst a), snd (fst a)), snd a) b_unit in (b_unit, snd speed +. b_val) ;; |
let passes_da_vinci t = let rec sum (lst: tree list) (add: float): float = match lst with | Branch (diameter, _) :: next -> sum next (diameter *. diameter +. add) | Leaf :: next -> sum next add | _ -> add in let rec check_branch (branch: tree list): bool = (match branch with | Branch (diameter, lst) :: next -> if ((diameter *. diameter) >= sum lst 0.) then check_branch lst && check_branch next else false | Leaf :: next -> check_branch next | _ -> true ) in check_branch [t] ;; |
let mode (l: 'a list) : 'a = let rec aux lst ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match lst with | [] -> if cur_num > max_num then cur_el else max_el | element :: lst -> if element = cur_el then aux lst (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux lst (element, 1) (cur_el, cur_num) else aux lst (element, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input." | element :: lst -> aux lst (element, 1) (element, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec run (lst: 'a list) (maintain: ('a * 'a) list) (last: 'a) = match lst with | [] -> mode maintain | element :: lst -> run lst ((last, element) :: maintain) element in match l with | [] -> failwith "Undefined input." | one :: [] -> failwith "Input size too small." | element :: lst -> run lst ([]) element ;; |
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 (dist: dist_unit value) = convert_dist ((fst from_unit), 1.) (fst to_unit) in let (time: time_unit value) = convert_time ((snd from_unit), 1.) (snd to_unit) in (((fst to_unit), (snd from_unit)), val_ *. (snd dist) /. (snd time)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speed: speed_unit value) = convert_speed ((fst (fst a), snd (fst a)), snd a) b_unit in (b_unit, snd speed +. b_val) ;; |
let passes_da_vinci t = let rec sum (lst: tree list) (add: float): float = match lst with | Branch (diameter, _) :: next -> sum next (diameter *. diameter +. add) | Leaf :: next -> sum next add | _ -> add in let rec check_branch (branch: tree list): bool = (match branch with | Branch (diameter, lst) :: next -> if ((diameter *. diameter) >= sum lst 0.) then check_branch lst && check_branch next else false | Leaf :: next -> check_branch next | _ -> true ) in check_branch [t] ;; |
let mode (l: 'a list) : 'a = let l = List.sort compare l in match l with | [] -> failwith "Error" | hd :: _ -> let find_max (c: 'a * int) (m: 'a * int) : 'a * int = if (snd c) > (snd m) then c else m in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> fst (find_max (cur_el, cur_num) (max_el, max_num)) | x :: tl when x = cur_el -> aux tl (cur_el, cur_num + 1) (max_el, max_num) | x :: tl -> aux tl (x, 1) (find_max (cur_el, cur_num) (max_el, max_num)) in aux l (hd, 0) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let acces_rest l = match l with | _ :: l1 -> l1 | _ -> failwith "Error" in if (List.length l < 2) then failwith "Error" else let l2 = acces_rest l in let l1 = List.rev (acces_rest (List.rev l)) in mode (List.sort compare (List.combine l1 l2)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (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_ *. (0.3048 *. 5280.)) | (Mile, Mile) -> (Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((d1, t1), (d2,t2)) = (from_unit, to_unit) in ((to_unit), (snd (convert_dist (d1, val_) d2)) /. (snd (convert_time (t1, 1.) t2))) ;; |
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 = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, _) :: tl -> sum_ tl (acc +. (width *. width)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl when (check subtree) && ((sum_ subtree 0.) <= (width *. width)) -> check tl | _ -> 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 | 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_list = List.sort compare (l) in match sorted_list with | [] -> failwith "Invalid input error" | hd::tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec helper l acc = match l with | [] -> acc | _::[] -> acc | x::(y::_ as tl) -> helper tl ((x,y)::acc) in match l with | [] -> failwith "Invalid input error" | x::y -> mode (helper l []) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Second -> Second, val_ | Hour, Hour -> Hour, val_ | Hour, Second -> Second, (val_ *. 3600.) | Second, Hour -> Hour, (val_ /. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Foot -> Foot, val_ | Meter, Meter -> Meter, val_ | Mile, Mile -> Mile, val_ | Foot, Meter -> Meter, (val_ *. 0.3048) | Meter, Foot -> Foot, (val_ /. 0.3048) | Foot, Mile -> Mile, (val_ /. 5280.) | Mile, Foot -> Foot, (val_ *. 5280.) | Mile, Meter -> Meter, (val_ *. 5280. *. 0.3048) | Meter, Mile -> Mile, (val_ /. 0.3048 /. 5280.) ;; |
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 d, d_val = convert_dist (from_dist, val_) to_dist in let t, t_val = convert_time (from_time, 1.) to_time in (d, t), (d_val /. t_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_unit, a_val = a in let _, conv_val = convert_speed (a_unit, a_val) b_unit in b_unit, (b_val +. conv_val) ;; |
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (check subtree = false) && ((traverse_tree subtree) > width**2.) 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 max_el | cur :: b -> if cur = cur_el then aux b (cur,cur_num+1) (max_el,max_num) else if cur_num > max_num then aux b (cur,1) (cur_el,cur_num) else aux b (cur,1) (max_el, max_num) in aux l ((List.hd l),0) ((List.hd l),0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l) < 2 then failwith "List length is too small." else let rec builder (pairs: ('a * 'a) list) (l: 'a list) = let h1 :: h2 :: t = l in if t=[] then mode ((h1,h2)::pairs) else builder ((h1,h2)::pairs) (h2::t) in builder [] l ;; |
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)) | 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) | _ , _ -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_dist, from_time) = from_unit in let (to_dist, to_time) = to_unit in let (_, time_factor) = convert_time (from_time, 1.) to_time in let (_, dist_factor) = convert_dist (from_dist, 1.) to_dist in to_unit, ((val_ /. time_factor) *. dist_factor) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, a') = convert_speed a b_unit in b_unit, (a' +. b_val) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch(f, tr) -> let rec sos (t1:tree) (t2:tree list) = match t1 with | Branch(f1, b) -> if ((counter t1) -. f1**2.)>(f1**2.) then false else if (List.length t2)>0 then let t::t2=t2 in sos t t2 else true | Leaf -> if (List.length t2)>0 then let t1::t=t2 in sos t1 t else true in sos t tr ;; |
let mode (l: 'a list) : 'a = let rec aux t ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match t with | [] -> if cur_num > max_num then cur_el else if cur_num = max_num then max_el else max_el | x :: xs -> if cur_el = x 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 | x :: xs -> aux xs (x,1) (x,1) | _ -> failwith "" ;; let l = [1;3;2;2;5;6;32;32;1;7;1;32] ;; List.sort compare l ;; mode l ;; let removef (l: 'a list) : 'a list = match l with | [] -> [] | _::xs -> xs;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "" else match l with | x :: xs -> let l1 = removef l in let l2 = List.rev (removef (List.rev l)) in let l3 = List.combine l2 l1 in mode l3 | _ -> failwith "" ;; pair_mode [3;1;3;1;2;2;2;2] ;; |
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_ | _ -> failwith "" ;; |
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_ | _ -> failwith "" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let d = convert_dist(fst(from_unit), val_) (fst(to_unit)) in let t = convert_time(snd(from_unit), 1.) (snd(to_unit)) in match (from_unit, to_unit) with | (from_unit, to_unit) -> (fst(d), fst(t)), (snd(d) /. snd(t)) | _ -> failwith "" ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let t = convert_time(time) (snd(speed_unit)) in fst(speed_unit) , (speed_val *. snd(t)) ;; let rec passes_da_vinci t = notimplemented() ;; |
let mode (l: 'a list) : 'a = match l with | [] -> notimplemented() | hd::tl -> let sortList = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd::tl -> if cur_el = hd then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1)(cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in aux sortList (hd,0) (hd,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "undefined" | [x; y] -> failwith "no" | hd::tl -> if List.length l < 2 then notimplemented() else let l1 = match l with | [] -> [] | hd::tl -> tl in let l2 = match List.rev l with | [] -> [] | x::xs -> List.rev xs in let mergetuples = List.combine l2 l1 in mode (List.sort compare mergetuples) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if to_unit = Second then match fst(from_unit, val_) with | Hour -> let v = snd(from_unit, val_) in (Second, 3600. *. v) | Second -> (Second, val_) | _ -> notimplemented() else if to_unit = Hour then match fst(from_unit, val_) with | Second -> let c = snd(from_unit, val_) in (Hour, val_ /. 3600.) | Hour -> (Hour, val_) | _ -> notimplemented() else failwith "error" ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_), to_unit with |(Foot, v1), Meter -> (Meter, v1 *. 0.3048) |(Foot, v1), Mile -> (Mile, v1 /. 5280.) |(Foot, v1), Foot -> (Foot, v1) |(Meter, v1), Foot -> (Foot, v1 /. 0.3048) |(Meter, v1), Mile -> (Mile, (v1 /. 0.3048) /. 5280.) |(Meter, v1), Meter -> (Meter, v1) |(Mile, v1), Foot -> (Foot, v1 *. 5280.) |(Mile, v1), Meter -> (Meter, v1 *. (0.3048 *. 5280.)) |(Mile, v1), Mile -> (Mile, v1) | _ -> notimplemented () ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let time = convert_time (snd(from_unit), 1.) (snd(to_unit)) in let dist = convert_dist (fst(from_unit), val_) (fst(to_unit)) in (to_unit, snd(dist) /. snd(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, a_val +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> 0. | Branch (width, subtree) :: tl -> sum_ tl ((width *. width) +.acc) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | 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 = match l with | [] -> failwith "undefined input" | hd::tl -> let sortList = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | hd::tl -> if cur_el = hd then aux tl (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1)(cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in aux sortList (hd,0) (hd,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "undefined" | [x; y] -> failwith "no" | hd::tl -> if List.length l < 2 then notimplemented() else let l1 = match l with | [] -> [] | hd::tl -> tl in let l2 = match List.rev l with | [] -> [] | x::xs -> List.rev xs in let mergetuples = List.combine l2 l1 in mode (List.sort compare mergetuples) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if to_unit = Second then match fst(from_unit, val_) with | Hour -> let v = snd(from_unit, val_) in (Second, 3600. *. v) | Second -> (Second, val_) | _ -> notimplemented() else if to_unit = Hour then match fst(from_unit, val_) with | Second -> let c = snd(from_unit, val_) in (Hour, val_ /. 3600.) | Hour -> (Hour, val_) | _ -> notimplemented() else failwith "error" ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_), to_unit with |(Foot, v1), Meter -> (Meter, v1 *. 0.3048) |(Foot, v1), Mile -> (Mile, v1 /. 5280.) |(Foot, v1), Foot -> (Foot, v1) |(Meter, v1), Foot -> (Foot, v1 /. 0.3048) |(Meter, v1), Mile -> (Mile, (v1 /. 0.3048) /. 5280.) |(Meter, v1), Meter -> (Meter, v1) |(Mile, v1), Foot -> (Foot, v1 *. 5280.) |(Mile, v1), Meter -> (Meter, v1 *. (0.3048 *. 5280.)) |(Mile, v1), Mile -> (Mile, v1) | _ -> notimplemented () ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let time = convert_time (snd(from_unit), 1.) (snd(to_unit)) in let dist = convert_dist (fst(from_unit), val_) (fst(to_unit)) in (to_unit, snd(dist) /. snd(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, a_val +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> 0. | Branch (width, subtree) :: tl -> sum_ tl ((width *. width) +.acc) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | 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 rec aux l2 ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l2 with |[]-> if cur_num <= max_num then max_el else cur_el |hd::tl -> if cur_el = hd then aux tl (cur_el,(cur_num+1)) (max_el,max_num) else if cur_num > max_num then aux tl (hd,1)(cur_el,cur_num) else aux tl (hd,1) (max_el,max_num) in let l2=List.sort compare(l) in match l2 with | [] -> failwith "invalid input" | hd:: tl -> aux (l2) (hd,0) (hd,0) let remove l= match l with |[] -> [] | _ :: l1 -> l1 ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length (l) <2 then failwith "invalid input" else let l1= remove (l) in let l2 = List.rev(remove (List.rev (l))) in let l3=List.combine l2 l1 in mode (l3) ;; exception Invalid_input;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match ((from_unit,val_),to_unit) with |((Second,x),Second) -> (Second,x) |((Hour,x),Second) -> (Second,x*.3600.) |((Hour,x),Hour)->(Hour,x) |((Second,x),Hour) -> (Hour,x /. 3600.) |_ ->failwith "invalid input" ;; |
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),Mile)-> (Mile,x /. 5280.) |((Foot,x),Meter)-> (Meter,x*.0.3048) |((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.) |_ ->failwith "invalid input" ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match ((from_unit,val_),to_unit) with |(((from_dist, from_time), x),(to_dist, to_time))->((to_dist, to_time),x *. snd((convert_dist(from_dist,1.) to_dist))/.snd(convert_time(from_time,1.) to_time)) |_ ->failwith "invalid input" ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = match (a, (b_unit, b_val)) with |(speed1,(speed2_unit,speed2_val))->(speed2_unit),snd(convert_speed(speed1) speed2_unit)+.speed2_val |_ ->failwith "invalid input" ;; |
let passes_da_vinci t = check [t] ;; |
let mode (l: 'a list) : 'a = match l with | [] -> failwith "The list could not be empty." | _ -> 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 (hd, cur_num + 1) (max_el, max_num) else if hd = cur_el && (cur_num + 1) > max_num then aux tl (hd, cur_num + 1) (cur_el, cur_num + 1) else aux tl (hd, 1) (max_el, max_num) in aux (List.tl (List.sort compare l)) (List.hd (List.sort compare l), 1) (List.hd (List.sort compare l), 1) ;; |
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 li = List.combine l2 l1 in mode li ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith "Time could not be a negative value." else if from_unit = to_unit then (from_unit, val_) else match to_unit with | Second -> (to_unit, 3600. *. val_) | Hour -> (to_unit, val_ /. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0. then failwith "Distance could not be a negative value." else if from_unit = to_unit then (from_unit, val_) else match to_unit with | Foot -> if from_unit = Meter then (to_unit, val_ /. 0.3048) else (to_unit, val_ *. 5280.) | Meter -> if from_unit = Foot then (to_unit, val_ *. 0.3048) else (to_unit, val_ *. 1609.344) | Mile -> if from_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 = if val_ < 0. then failwith "Speed could not be a negative value." else if from_unit = to_unit then (from_unit, val_) else let a = fst(from_unit) in let b = fst(to_unit) in let c = snd(from_unit) in let d = snd(to_unit) in let val1 = snd(convert_time (c, val_) d) in let val2 = val1 /. ((val1 /. val_) *. (val1 /. val_)) in (to_unit, snd(convert_dist (a, val2) b)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let val1 = snd(convert_speed a b_unit) in let total = val1 +. b_val in (b_unit, total) ;; |
let passes_da_vinci t = 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 is_empty (l:'a list) : bool = match l with |[] -> true |xs -> false let pop_first (l:'a list) : 'a = match l with | x :: xs -> x |[] -> notimplemented ();; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = let new_list = match l with | [] -> [] | x :: xs -> xs in if is_empty l then if cur_num > max_num then cur_el else max_el else if (compare (pop_first l) cur_el) = 0 then if cur_num + 1 > max_num then aux new_list ((cur_el, cur_num +1)) ((cur_el, cur_num + 1)) else aux new_list ((cur_el, cur_num + 1)) ((max_el, max_num)) else aux new_list ((pop_first l, 1)) ((max_el, max_num)) in aux (List.sort compare l) ((pop_first (List.sort compare l)), 0) ((pop_first (List.sort compare l), 0)) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let pairs = List.combine (List.rev (List.tl (List.rev l)) ) (List.tl l) in mode pairs ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then (to_unit, val_) else match to_unit with | Second -> (Second, val_ *.3600.0) | Hour -> (Hour, val_ /. 3600.0) ;; let rec 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 | Mile -> if to_unit = Foot then (Foot, 5280.0 *. val_) else convert_dist (Foot, 5280.0 *. val_) Meter | Foot -> if to_unit = Mile then (Mile, val_ /. 5280.0) else (Meter, val_ *. 0.3048) | Meter -> if to_unit = Foot then (Foot, val_ /. 0.3048) else convert_dist (Foot, val_ /. 0.3048) Mile ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (to_unit, val_) else let ((from_dist_unit, from_time_unit), (to_dist_unit, to_time_unit)) = (from_unit, to_unit) in ((to_dist_unit, to_time_unit), snd (convert_dist (from_dist_unit, val_) to_dist_unit) /. snd (convert_time (from_time_unit, 1.0) to_time_unit)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (_, val_) = (convert_speed a b_unit) in (b_unit, val_ +. b_val) ;; |
let passes_da_vinci t = let rec summation (subtree : tree list) (acc : float) = match subtree with | [] -> acc | Leaf :: xs -> summation xs (acc +. 0.0) | Branch (w, _) :: xs -> summation xs (acc +. (w *. w)) in let rec visit_tree (to_visit : tree list) (res : bool) = match to_visit with | [] -> res | Leaf :: xs -> visit_tree xs res | Branch (width, subtree) :: xs -> visit_tree (append xs subtree) (res && ((width *.width) >= (summation subtree 0.0))) in visit_tree (t :: []) true ;; let rec summation (subtree : tree list) (acc : float) = match subtree with | [] -> acc | Leaf :: xs -> summation xs (acc +. 0.0) | Branch (w, _) :: xs -> summation xs (acc +. w);; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd::tl -> let new_el = hd in let l = tl in if new_el = cur_el then let cur_num = cur_num + 1 in (if cur_num > max_num then let max_el = new_el in let max_num = cur_num in aux l (new_el, cur_num) (max_el, max_num) else aux l (new_el, cur_num) (max_el, max_num)) else (let cur_num = 1 in aux l (new_el, cur_num) (max_el, max_num)) in if l = [] then failwith "List has no elements." else let l = List.sort compare l in let cur_el = List.hd l in let l = List.tl l in let cur_num = 1 in let max_el = cur_el in let max_num = cur_num in aux l (cur_el, cur_num) (max_el, max_num) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let length = List.length l in if (l = [] || length = 1) then failwith "List has 1 or no elements." else let l1 = List.tl l in let l2 = List.rev l in let l2 = List.tl l2 in let l2 = List.rev l2 in let tuple_list = List.combine l2 l1 in mode tuple_list ;; let second_convert ((to_unit, val_) : time_unit value ) = match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) | _ -> failwith "'to_unit' is not a valid time unit." ;; let hour_convert ((to_unit, val_) : time_unit value ) = match to_unit with | Hour -> (Hour, val_) | Second -> (Second, val_ *. 3600.) | _ -> failwith "'to_unit' is not a valid time unit." ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let convert_to = (to_unit, val_) in match from_unit with | Second -> second_convert convert_to | Hour -> hour_convert convert_to | _ -> failwith "'from_unit' is not a valid time unit." ;; let foot_convert ((to_unit, val_) : dist_unit value ) = match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.) | _ -> failwith "'to_unit' is not a valid distance unit." ;; let meter_convert ((to_unit, val_) : dist_unit value ) = match to_unit with | Meter -> (Meter, val_) | Foot -> (Foot, val_ /. 0.3048) | Mile -> foot_convert (Mile, val_ /. 0.3048) | _ -> failwith "'to_unit' is not a valid distance unit." ;; let mile_convert ((to_unit, val_) : dist_unit value ) = match to_unit with | Mile -> (Mile, val_) | Meter -> foot_convert (Meter, val_ *. 5280.) | Foot -> (Foot, val_ *. 5280.) | _ -> failwith "'to_unit' is not a valid distance unit." ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let convert_to = (to_unit, val_) in match from_unit with | Foot -> foot_convert convert_to | Meter -> meter_convert convert_to | Mile -> mile_convert convert_to | _ -> failwith "'from_unit' is not a valid distance unit." ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist_unit = fst from_unit in let from_time_unit = snd from_unit in let to_dist_unit = fst to_unit in let to_time_unit = snd to_unit in let converted_val = snd (convert_dist (from_dist_unit, val_) to_dist_unit) in let converted_val = 1. /. converted_val in let converted_val = snd (convert_time (from_time_unit, converted_val) to_time_unit) in (to_unit, 1. /. converted_val) ; ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (new_speed_unit, new_speed_val) = convert_speed a b_unit in (b_unit, new_speed_val +. b_val) ;; |
let passes_da_vinci t = let rec sum list acc = match list with | [] -> acc | Leaf :: tl -> sum tl acc | Branch (width, subtree) :: tl -> let width_square = width *. width in let acc = acc +. width_square in sum tl acc in let rec check list = match list with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> let w_square = width *. width in let acc = 0. in if (check subtree) && (sum subtree acc <= w_square) 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) = if cur_num > max_num then match l with | [] -> if cur_num > max_num then cur_el else max_el | x::t -> if x = cur_el then aux t (cur_el, cur_num+1) (cur_el, cur_num+1) else aux t (x, 1) (cur_el, cur_num) else match l with | [] -> max_el | x::t -> if x = cur_el then aux t (cur_el, cur_num+1) (max_el, max_num) else aux t (x, 1) (max_el, max_num) in let l' = List.sort compare l in match l' with | [] -> failwith "Undefined input." | x :: t -> aux t (x, 1) (x, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Undefined input." else let l1 = List.rev (List.tl (List.rev l)) and l2 = List.tl l in let l' = List.combine l1 l2 in mode l' ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Second) | (Hour, Hour) -> (from_unit, val_) | (Second, Hour) -> (to_unit, val_/.3600.0) | (Hour, Second) -> (to_unit, val_*.3600.0) ;; |
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) -> (from_unit, val_) | (Foot, Meter) -> (to_unit, val_*.0.3048) | (Foot, Mile) -> (to_unit, val_/.5280.0) | (Meter, Foot) -> (to_unit, val_/.0.3048) | (Meter, Mile) -> (to_unit, val_/.0.3048/.5280.0) | (Mile, Foot) -> (to_unit, val_*.5280.0) | (Mile, Meter) -> (to_unit, val_*.0.3048*.5280.0) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist = convert_dist (fst(from_unit), 1.0) (fst(to_unit)) and ti = convert_time(snd(from_unit), 1.0) (snd(to_unit)) in let d = snd(dist) and time = snd(ti) in (to_unit, val_*.d/.time) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a' = convert_speed a b_unit in let val' = snd(a')+.b_val in (b_unit, val') ;; |
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> true && check tl | Branch (w, subtree) :: tl -> (w**2. >= (sum_ subtree 0.)) && (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 | [] -> max_el | x :: xs -> if x = cur_el then if cur_num >= 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 let sorted = List.sort compare l in match sorted with | x :: xs -> aux xs (x, 1) (x, 1) | _ -> failwith "invalid input" ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | _ :: [] | [] -> failwith "input must have at least 2 elements" | _ -> let l2 = List.tl (List.rev l) in let l1 = List.tl l in let pairs = List.combine (List.rev l2) l1 in mode pairs ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> ( match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.) ) | Hour -> ( match to_unit with | 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_ /. 0.3048 /. 5280.) ) | Mile -> ( match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 0.3048 *. 5280.) | Mile -> (Mile, val_) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist, from_time = from_unit in let to_dist, to_time = to_unit in let _, new_dist = convert_dist (from_dist, val_) to_dist in let _, new_time = convert_time (from_time, 1.) to_time in ((to_dist, to_time), new_dist /. new_time) ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.