text
stringlengths 0
601k
|
---|
let mode (l: 'a list) : 'a = if l = [] then failwith "bruh" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if x = cur_el then let cur_num = cur_num+1 in if cur_num > max_num then let max_el = cur_el in let max_num = cur_num in let cur_el = x in aux xs (cur_el, cur_num) (max_el, max_num) else aux xs (cur_el, cur_num) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in let l = List.sort compare l in aux (l) (List.hd l, 0) (List.hd l, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l = [] then failwith "bruh" else let list1 = l in let list2 = l in let list1 = List.tl list1 in let list2 = List.rev list2 in let list2 = List.tl list2 in let list2 = List.rev list2 in let find_pair = List.combine list2 list1 in mode find_pair ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> if to_unit = Hour then (Hour, val_) else (Second, val_ *. 3600.0) | Second -> if to_unit = Second then (Second, val_) else (Hour, val_ /. 3600.0) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Foot then (Foot, val_) else if to_unit = Meter then (Meter, val_ *. 0.3048) else (Mile, val_ /. 5280.0) | Meter -> if to_unit = Meter then (Meter, val_) else if to_unit = Foot then (Foot, val_ /. 0.3048) else (Mile, val_ /. 1609.344) | Mile -> if to_unit = Mile then (Mile, val_) else if to_unit = Foot then (Foot, val_ *. 5280.0) else (Meter, val_ *. 1609.344) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist_from, time_from) = from_unit in let (dist_to, time_to) = to_unit in let (dist, val1) = convert_dist (dist_from, val_) dist_to in let (time, val2) = convert_time (time_from, 1.) time_to in ((dist, time) : speed_unit), val1 /. val2 ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((a_dist, a_speed), a_val) = a in let (bruh, to_add) = convert_speed ((a_dist, a_speed), a_val) b_unit in bruh, (b_val +. to_add) ;; |
let passes_da_vinci t = let rec sum t_prime = match t_prime with | [] -> 0. | Leaf :: rest -> 0. +. sum rest | Branch (width, subtree) :: t1 -> (width *. width) +. (sum subtree) +. (sum t1) in let rec check l = match l with | [] -> true | Leaf :: t1 -> check t1 | Branch (width, subtree) :: t1 -> (check subtree) && ((sum subtree) <= (width *. width)) && (check t1) in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = function | [] -> if cur_num > max_num then cur_el else max_el |x::xs -> if cur_el = x then aux (cur_el,(cur_num+1)) (max_el, max_num) xs else if cur_num > max_num then aux (x,1) (cur_el,cur_num) xs else aux (x,1) (max_el,max_num) xs in match List.sort compare l with |[] -> failwith "List too small" |x::xs -> aux (x,1) (x,0) xs ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec make_pair il lp = match il with |[] -> failwith "List too small" |[]->lp |[x]->lp |x1::(x2::xs) -> (make_pair (x2::xs) (lp@[(x1,x2)])) in mode (make_pair l []) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with |Hour,Hour -> (Hour,val_) |Hour,Second -> (Second,(( *. ) val_ 3600.0)) |Second,Second -> (Second,val_) |Second,Hour -> (Hour,(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 -> (Foot,val_) |Foot,Meter -> (Meter,(val_ *. 0.3048)) |Foot,Mile -> (Mile, (val_ /. 5280.0 )) |Meter,Meter->(Meter,val_) |Meter,Foot ->(Foot,(val_ /. 0.3048)) |Meter,Mile -> (Mile, ((val_ /. 0.3048) /. 5280.0)) |Mile,Mile->(Mile,val_) |Mile,Meter->(Meter,((val_ *. 5280.0) *. 0.3048)) |Mile,Foot->(Foot,(val_ *. 5280.0)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (fromDistUnit,fromTimeUnit) = from_unit in let (toDistUnit,toTimeUnit) = to_unit in let (convertDistUnit,convertDistVal) = convert_dist (fromDistUnit,val_) toDistUnit in let (convertTimeUnit,convertTimeVal) = convert_time (toTimeUnit,1.0) fromTimeUnit in (to_unit,(convertDistVal *. convertTimeVal)) ;; |
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 (convert_a_unit,convert_a_val) = convert_speed (a_unit,a_val) b_unit in let sum = b_val +. convert_a_val in (b_unit,sum) ;; |
let passes_da_vinci t = check_branches [t] ;; |
let mode (l: 'a list) : 'a = if List.length l <= 0 then failwith "Invalid Input"; let l = List.sort compare l in let l = List.rev 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 max_el else cur_el); | hd :: tl -> ( match hd with | x when x = cur_el -> (if (cur_num + 1) >= max_num then aux tl (hd,cur_num + 1) (hd, cur_num + 1) else aux tl (hd,cur_num + 1) (max_el, max_num) ); | _ -> (if 1 >= max_num then aux tl (hd,1) (hd,1) else aux tl (hd,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 "Invalid Input" else( let (_ :: l1) = l in let (_ :: l2) = List.rev l in let final = List.combine (List.rev l2) l1 in mode final ) ;; |
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.0)) | Second -> (Second, val_) ); | (Hour) -> (match to_unit with | Second -> (Second, (val_ *. 3600.0)) | 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.0)) | Foot -> (Foot, val_) ); | Meter -> (match to_unit with | Foot -> (Foot, (val_ /. 0.3048)) | Mile -> (Mile, ((val_ /. 0.3048) /. 5280.0)) | Meter -> (Meter, val_ ) ); | Mile -> (match to_unit with | Foot -> (Foot, (val_ *. 5280.0)) | Meter -> (Meter, ((val_ *. 5280.0) *. 0.3048)) | Mile -> (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.0) (snd to_unit) in ((fst dist,fst time), snd dist /. snd time) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let speed_a = convert_speed (a) (b_unit) in (b_unit, snd speed_a +. 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 (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 | [] -> max_el | x::b -> if x=cur_el then if cur_num > max_num then aux b (x,cur_num+1) (x,cur_num) else aux b (x,cur_num+1) (max_el,max_num) else aux b (x,1) (max_el,max_num) in let l = List.sort compare l in let x::b = l in aux b (x, 1) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux (l: 'a list) : 'b list = match l with | x::y::b -> (x, y) :: (aux (y :: b)) | x::[] -> [] | [] -> [] in mode (aux 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_) | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, 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.) | (Meter, Mile) -> (Mile, val_ /. 1609.344) | (Mile, Meter) -> (Meter, val_ *. 1609.344) ;; |
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 extract_val (unit,value) = value in (to_unit, val_ *. extract_val (convert_dist (from_dist,1.) to_dist) /. extract_val (convert_time (from_time,1.) to_time)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let extract_arg1 (arg1,arg2) = arg1 in let extract_arg2 (arg1,arg2) = arg2 in let correct_time = extract_arg2 (convert_time time (extract_arg2 speed_unit)) in (extract_arg1 speed_unit, correct_time *. speed_val) ;; let rec passes_da_vinci t = let rec square_sum (x:tree list) = match x with | [] -> 0. | Leaf::b -> 0. +. square_sum b | Branch (value,brlist)::b -> value ** 2. +. square_sum b in let rec da_vinci_iterate (trlist : tree list) = match trlist with | [] -> true | x::b -> passes_da_vinci x && da_vinci_iterate b in match t with | Leaf -> true | Branch (value, brlist) -> value ** 2. >= square_sum brlist && da_vinci_iterate brlist ;; |
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 = 0 then failwith "empty" else if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el then aux xs (x, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 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 acclist = let rec helper (l: 'a list) (acclist :('a * 'a) list) = match l with | [] -> failwith "empty" | x::xs -> match xs with | [] -> acclist | y::ys -> helper (y::ys) ((x,y)::acclist) in helper l [] in mode acclist ;; |
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.0 | Hour, Second -> to_unit, val_ *. 3600.0 | _ -> 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_ /. 5280. /. 0.3048 | 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 ((distunit,timeunit),(todist,totime)) = (from_unit, to_unit) in let (distunit1,val1) = convert_dist (distunit, val_) todist in let (tunit,val2) = convert_time (totime, val1) timeunit in ((todist, totime), val2) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speedunit,vala) = convert_speed a b_unit in (b_unit, (vala +. b_val)) ;; |
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> if not ((check subtree) && (sqr subtree 0. <= width**2.)) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num = 0 then failwith "empty" else if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el then aux xs (x, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 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 acclist = let rec helper (l: 'a list) (acclist :('a * 'a) list) = match l with | [] -> failwith "empty" | x::xs -> match xs with | [] -> acclist | y::ys -> helper (y::ys) ((x,y)::acclist) in helper l [] in mode acclist ;; |
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.0 | Hour, Second -> to_unit, val_ *. 3600.0 | _ -> 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_ /. 5280. /. 0.3048 | 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 ((distunit,timeunit),(todist,totime)) = (from_unit, to_unit) in let (distunit1,val1) = convert_dist (distunit, val_) todist in let (tunit,val2) = convert_time (totime, val1) timeunit in ((todist, totime), val2) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speedunit,vala) = convert_speed a b_unit in (b_unit, (vala +. b_val)) ;; |
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> if not ((check subtree) && (sqr subtree 0. <= width**2.)) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num = 0 then failwith "empty" else if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el then aux xs (x, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 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 acclist = let rec helper (l: 'a list) (acclist :('a * 'a) list) = match l with | [] -> failwith "empty" | x::xs -> match xs with | [] -> acclist | y::ys -> helper (y::ys) ((x,y)::acclist) in helper l [] in mode acclist ;; |
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.0 | Hour, Second -> to_unit, val_ *. 3600.0 | _ -> 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_ /. 5280. /. 0.3048 | 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 ((distunit,timeunit),(todist,totime)) = (from_unit, to_unit) in let (distunit1,val1) = convert_dist (distunit, val_) todist in let (tunit,val2) = convert_time (totime, val1) timeunit in ((todist, totime), val2) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speedunit,vala) = convert_speed a b_unit in (b_unit, (vala +. b_val)) ;; |
let passes_da_vinci t = match t with | Branch (x, sublist) -> x**2. = sqr sublist 0. | Leaf -> true ;; |
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 = 0 then failwith "empty" else if cur_num > max_num then cur_el else max_el | x::xs -> if x = cur_el then aux xs (x, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 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 acclist = let rec helper (l: 'a list) (acclist :('a * 'a) list) = match l with | [] -> failwith "empty" | x::xs -> match xs with | [] -> acclist | y::ys -> helper (y::ys) ((x,y)::acclist) in helper l [] in mode acclist ;; |
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.0 | Hour, Second -> to_unit, val_ *. 3600.0 | _ -> 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_ /. 5280. /. 0.3048 | 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 ((distunit,timeunit),(todist,totime)) = (from_unit, to_unit) in let (distunit1,val1) = convert_dist (distunit, val_) todist in let (tunit,val2) = convert_time (totime, val1) timeunit in ((todist, totime), val2) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (speedunit,vala) = convert_speed a b_unit in (b_unit, (vala +. b_val)) ;; |
let passes_da_vinci t = match t with | Branch (x, sublist) -> x**2. >= sqr sublist 0. | Leaf -> true ;; |
let mode (l : 'a list) : 'a = if l = [] then failwith "Undefined input" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd :: tl when hd = cur_el && hd = max_el -> aux tl (cur_el, cur_num + 1) (max_el, max_num + 1) | hd :: tl when hd = cur_el && cur_num < max_num -> aux tl (cur_el, cur_num + 1) (max_el, max_num) | hd :: tl when hd = cur_el && cur_num >= max_num -> aux tl (cur_el, cur_num + 1) (cur_el, cur_num + 1) | hd :: tl -> aux tl (hd, 1) (max_el, max_num) in let list = List.sort compare l in aux list (List.hd list, 0) (List.hd list, 0) ;; |
let pair_mode (l : 'a list) : 'a * 'a = if List.length l < 2 then failwith "Undefined input" else let rec helper l new_list = if List.length l <= 1 then new_list else helper (List.tl l) ((List.nth l 0, List.nth l 1) :: new_list) in let new_list = helper l [] in mode new_list ;; |
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.) | (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, val_), to_unit with | (Foot, _), Foot -> (Foot, val_) | (Foot, _), Meter -> (Meter, val_ *. 0.3048) | (Foot, _), Mile -> (Mile, val_ /. 5280.) | (Meter, _), Foot -> (Foot, val_ /. 0.3048) | (Meter, _), Meter -> (Meter, val_) | (Meter, _), Mile -> (Mile, val_ /. 0.3048 /. 5280.) | (Mile, _), Foot -> (Foot, val_ *. 5280.) | (Mile, _), Meter -> (Meter, val_ *. 5280. *. 0.3048) | (Mile, _), Mile -> (Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_dist, from_time = from_unit in let to_dist, to_time = to_unit in let fin_dist, int_val = convert_dist (from_dist, val_) to_dist in let _fin_time, fin_val = convert_time (to_time, int_val) from_time in (fin_dist, to_time), fin_val ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let _converted_speed, converted_val = convert_speed a b_unit in (b_unit, converted_val +. b_val) ;; |
let passes_da_vinci t = let rec sum (trees : tree list) (acc : float) = match trees with | [] -> acc | Leaf :: tl -> sum tl acc | Branch(width, subtree) :: tl -> sum tl (acc +. width *. width) +. sum subtree 0. in let rec check (trees : tree list) = match trees 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 l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | x :: xs -> if x = cur_el then aux xs (x, cur_num + 1) (max_el, max_num) else (if cur_num > max_num then aux xs (x, 0) (cur_el, cur_num) else aux xs (x, 0) (max_el, max_num)) in match (List.sort compare (l)) with | [] -> failwith "Invalid Input" | x :: xs -> aux xs (x, 0) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pair_tl x l acc = match l with | [] -> if [] = acc then failwith "Invalid Input" else mode acc | h :: t -> pair_tl h t ((x, h) :: acc) in match l with | [] -> failwith "Invalid Input" | x :: xs -> pair_tl x xs [] ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, c) -> if (to_unit = Hour) then (to_unit, c /. 3600.) else (Second, c) | (Hour, c) -> if (to_unit = Second) then (to_unit, c *. 3600.) else (Hour, c) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_) with | (Foot, c) -> if (to_unit = Foot) then (to_unit, c) else if (to_unit = Meter) then (to_unit, c *. 0.3048) else (to_unit, c /. 5280.) | (Meter, c) -> if (to_unit = Meter) then (to_unit, c) else if (to_unit = Foot) then (to_unit, c /. 0.3048) else (to_unit, (c /. (5280. *. 0.3048))) | (Mile, c) -> if (to_unit = Mile) then (to_unit, c) else if (to_unit = Foot) then (to_unit, c *. 5280.) else (to_unit, (c *. 5280. *. 0.3048)) ;; let getFloat (from_unit, val_) = let (_,val_) = (from_unit, val_) in val_;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match (from_unit, val_) with | ((Meter, Second), v) -> ( match to_unit with |(Meter, Second) -> (to_unit, v) |(Foot, Second) -> (to_unit, getFloat (convert_dist (Meter, v) Foot)) |(Mile, Second) -> (to_unit, getFloat (convert_dist (Meter, v) Mile)) |(Meter, Hour) -> (to_unit, getFloat (convert_time (Hour, v) Second)) |(Foot, Hour) -> (to_unit, getFloat (convert_time (Hour, (getFloat (convert_dist (Meter, v) Foot))) Second)) |(Mile, Hour) -> (to_unit, getFloat (convert_time (Hour, (getFloat (convert_dist (Meter, v) Mile))) Second)) ) | ((Foot, Second), v) -> ( match to_unit with |(Foot, Second) -> (to_unit, v) |(Meter, Second) -> (to_unit, getFloat (convert_dist (Foot, v) Meter)) |(Mile, Second) -> (to_unit, getFloat (convert_dist (Foot, v) Mile)) |(Foot, Hour) -> (to_unit, getFloat (convert_time (Hour, v) Second)) |(Meter, Hour) -> (to_unit, getFloat (convert_time (Hour, (getFloat (convert_dist (Foot, v) Meter))) Second)) |(Mile, Hour) -> (to_unit, getFloat (convert_time (Hour, (getFloat (convert_dist (Foot, v) Mile))) Second)) ) | ((Mile, Second), v) -> ( match to_unit with |(Mile, Second) -> (to_unit, v) |(Meter, Second) -> (to_unit, getFloat (convert_dist (Mile, v) Meter)) |(Foot, Second) ->(to_unit, getFloat (convert_dist (Mile, v) Foot)) |(Mile, Hour) -> (to_unit, getFloat (convert_time (Hour, v) Second)) |(Foot, Hour) -> (to_unit, getFloat (convert_time (Hour, (getFloat (convert_dist (Mile, v) Foot))) Second)) |(Meter, Hour) -> (to_unit, getFloat (convert_time (Hour, (getFloat (convert_dist (Mile, v) Meter))) Second)) ) | ((Meter, Hour), v) -> ( match to_unit with |(Meter, Hour) -> (to_unit, v) |(Foot, Hour) -> (to_unit, getFloat (convert_dist (Meter, v) Foot)) |(Mile, Hour) -> (to_unit, getFloat (convert_dist (Meter, v) Mile)) |(Meter, Second) -> (to_unit, getFloat (convert_time (Second, v) Hour)) |(Foot, Second) -> (to_unit, getFloat (convert_time (Second, (getFloat (convert_dist (Meter, v) Foot))) Hour)) |(Mile, Second) -> (to_unit, getFloat (convert_time (Second, (getFloat (convert_dist (Meter, v) Mile))) Hour)) ) | ((Foot, Hour), v) -> ( match to_unit with |(Foot, Hour) -> (to_unit, v) |(Meter, Hour) -> (to_unit, getFloat (convert_dist (Foot, v) Meter)) |(Mile, Hour) -> (to_unit, getFloat(convert_dist (Foot, v) Mile)) |(Foot, Second) -> (to_unit, getFloat (convert_time (Second, v) Hour)) |(Meter, Second) -> (to_unit, getFloat (convert_time (Second, (getFloat (convert_dist (Foot, v) Meter))) Hour)) |(Mile, Second) -> (to_unit, getFloat (convert_time (Second, (getFloat (convert_dist (Foot, v) Mile))) Hour)) ) | ((Mile, Hour), v) -> ( match to_unit with |(Mile, Hour) -> (to_unit, v) |(Meter, Hour) -> (to_unit, getFloat (convert_dist (Mile, v) Meter)) |(Foot, Hour) -> (to_unit, getFloat(convert_dist (Mile, v) Foot)) |(Mile, Second) -> (to_unit, getFloat (convert_time (Second, v) Hour)) |(Meter, Second) -> (to_unit, getFloat (convert_time (Second, (getFloat (convert_dist (Mile, v) Meter))) Hour)) |(Foot, Second) -> (to_unit, getFloat (convert_time (Second, (getFloat (convert_dist (Mile, v) Foot))) Hour)) ) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = (b_unit, (getFloat (convert_speed a b_unit)) +. b_val) ;; let getDist ((dist_unit, time_unit)) = let (dist_unit,_) = (dist_unit, time_unit) in dist_unit ;; let getT (speed_unit) = let (_,time_unit) = speed_unit in time_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 (width *. width +. acc) ) in let rec check l = match l with | [] -> true | Leaf :: tl -> true && check tl | Branch (width, subtree) :: tl -> if ((check subtree && sum_ subtree 0. <= width *. width)) 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 :: xs -> if x = cur_el 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 let l = List.sort compare l in aux l (List.hd l, 0) (List.hd l, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let right_tail = List.tl l in let left_tail = List.rev (List.tl (List.rev l)) in let bigrams = List.combine left_tail right_tail in let most_common_pair = mode bigrams in most_common_pair ;; |
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_ /. 60.) /. 60.) | (Hour, _), Second -> (Second, (val_ *. 60.) *. 60.) | (Second, t), Second -> (Second, t) | (Hour, t), Hour -> (Second, t) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_), to_unit with | (Foot, _), Mile -> (Mile, val_ /. 5280.) | (Meter, _), Mile -> (Mile, (val_ /. 0.3048) /. 5280.) | (Foot, _), Meter -> (Meter, val_ *. 0.3048) | (Mile, _), Meter -> (Meter, (val_ *. 5280.) *. 0.3048) | (Meter, _), Foot -> (Foot, val_ /. 0.3048) | (Mile, _), Foot -> (Foot, val_ *. 5280.) | (Foot, d), Foot -> (Foot, d) | (Meter, d), Meter -> (Meter, d) | (Mile, d), Mile -> (Mile, d) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (((from_d,from_t),s),(to_d,to_t)) = ((from_unit, val_), to_unit) in ((to_d, to_t), s *. ((snd (convert_dist (from_d, 1.) to_d)) /. (snd (convert_time (from_t, 1.) to_t))) ) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let new_speed = convert_speed (speed_unit, speed_val) (fst(speed_unit), fst(time)) in let final_val = snd new_speed *. snd time in fst(speed_unit), final_val ;; let rec compute_squares tr curr_sum = match tr with | [] -> curr_sum | x :: xs -> match x with | Leaf -> compute_squares xs curr_sum | Branch (w, childlist) -> compute_squares xs (curr_sum +. (w *. w)) let rec passes_da_vinci t = match t with | Leaf -> true | Branch (w, childlist) -> (w *. w) >= compute_squares childlist 0. && let rec tree_list_aux tree_list = match tree_list with | [] -> true | child :: remaining -> passes_da_vinci child && tree_list_aux remaining in tree_list_aux childlist ;; |
let mode (l: 'a list) : 'a = if (l = []) then failwith "list cannot be empty" else let (sortedL: 'a list) = List.sort compare l in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd::tl when hd = cur_el -> if cur_num + 1 > max_num then aux tl (hd, cur_num+1) (hd, cur_num+1) else aux tl (hd, cur_num+1) (max_el, max_num) | hd::tl -> aux tl (hd, 1) (max_el, max_num) in aux (List.tl sortedL) (List.hd(sortedL), 1) (List.hd(sortedL), 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if (List.length l < 2) then failwith "length must be greater or equal to 2" else let l1 = List.rev (List.tl (List.rev l)) in let l2 = List.tl l in mode (List.combine l1 l2) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (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 _, nom = convert_dist (from_dist, val_) to_dist in let _, denom = convert_time (from_time, 1.) to_time in (to_unit, nom /. denom) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let to_dist, to_time = speed_unit in let _, time_val = convert_time time to_time in to_dist, speed_val *. time_val ;; let sum_of_squares (list : tree list) : float = let rec aux list acc = match list with | [] -> acc | Leaf :: tl -> aux tl acc | Branch (width, _) :: tl -> aux tl (acc +. width *. width) in aux list 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, l) when width *. width >= sum_of_squares l -> List.for_all passes_da_vinci l | _ -> false ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h::t -> if h = cur_el then aux t (h, 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 "Please enter a valid list " | h::t -> aux t (h, 1)(h, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Please enter a valid list " | h::t-> let l2= List.tl(List.rev(l)) in let l1= List.tl(l) in let l3 = List.combine(List.rev(l2))(l1)in mode(l3) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let f_unit = fst(from_unit, val_) in match f_unit with | Second -> begin match to_unit with | Hour -> (Hour, snd(from_unit, val_) /. 3600.) | Second -> (Second, snd(from_unit, val_)) end | Hour -> begin match to_unit with | Second-> (Second, snd(from_unit, val_) *. 3600.) | Hour -> (Hour, snd(from_unit, val_)) end ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let f_unit = fst(from_unit, val_) in match f_unit with | Foot -> begin match to_unit with | Foot -> (Foot,snd(from_unit, val_)) | Meter -> (Meter, snd(from_unit, val_)*. 0.3048) | Mile -> ( Mile, snd(from_unit, val_) /. 5280. ) end | Meter -> begin match to_unit with | Foot -> (Foot,snd(from_unit, val_) /.0.3048 ) | Meter -> (Meter, snd(from_unit, val_)) | Mile -> ( Mile, snd(from_unit, val_) /. 0.3048 /. 5280. ) end | Mile -> begin match to_unit with | Foot -> (Foot,snd(from_unit, val_) *. 5280. ) | Meter -> (Meter, snd(from_unit, val_) *. 5280. *. 0.3048) | Mile -> ( Mile, snd(from_unit, val_)) end ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist = fst(fst(from_unit, val_)) in let time = snd(fst(from_unit, val_)) in let dist_converted = convert_dist(dist, snd(from_unit, val_))(fst(to_unit)) in let time_converted = convert_time(time, 1.)(snd(to_unit)) in let s_unit= (fst(dist_converted), fst(time_converted)) in (s_unit, snd(dist_converted)/. snd(time_converted)) ;; |
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_ subtree acc = match subtree 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 not(check subtree) then false else if sum_ (subtree)(0.) > width**2. then false else check tl in check [t];; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | (x :: xs) when (x = cur_el) -> 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) | (x :: xs) -> aux xs (x, 1) (max_el, max_num) in match l with | [] -> failwith "list is empty" | _ -> ( 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 length = List.length l in match length with | 0 -> failwith "list is empty" | 1 -> failwith "list only contains one element" | _ -> 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 = if val_ < 0.0 then failwith "negative time" else match from_unit with | Second -> ( match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.0) ) | Hour -> ( match to_unit with | Second -> (Second, val_ *. 3600.0) | Hour -> (Hour, val_) ) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0.0 then failwith "negative distance" else match from_unit with | Foot -> ( match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.0) ) | 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.0) | Meter -> (Meter, val_ *. 1609.344) | Mile -> (Mile, val_) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0.0 then failwith "negative speed" else let (d_from_unit, t_from_unit) = from_unit in let (d_to_unit, t_to_unit) = to_unit in let (dist_conv_unit, dist_conv_val) = convert_dist (d_from_unit, val_) d_to_unit in let (time_conv_unit, time_conv_val) = convert_time (t_from_unit, 1.0) t_to_unit in ((dist_conv_unit, time_conv_unit), dist_conv_val /. time_conv_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = if speed_val < 0.0 then failwith "negative speed" else let (speed_dist_unit, speed_time_unit) = speed_unit in let (_, conv_val) = convert_time time speed_time_unit in (speed_dist_unit, conv_val *. speed_val) ;; let rec passes_da_vinci t = let rec sum_of_squares (l: tree list) acc = match l with | [] -> acc | Branch(val_, _)::xs -> sum_of_squares xs ((val_*.val_) +. acc) | Leaf::xs -> sum_of_squares xs acc in let rec check_all (l: tree list) = match l with | [] -> true | Leaf::xs -> true && (check_all xs) | x::xs -> (passes_da_vinci x) && (check_all xs) in match t with | Leaf -> true | Branch(val_, list) -> ((sum_of_squares list 0.0) <= (val_*.val_)) && (check_all list) ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | (x :: xs) when (x = cur_el) -> 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) | (x :: xs) -> aux xs (x, 1) (max_el, max_num) in match l with | [] -> failwith "list is empty" | _ -> ( 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 = match l with | [] -> failwith "list is empty" | _::[] -> failwith "list only contains one element" | _ -> 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 = if val_ < 0.0 then failwith "negative time" else match from_unit with | Second -> ( match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /. 3600.0) ) | Hour -> ( match to_unit with | Second -> (Second, val_ *. 3600.0) | Hour -> (Hour, val_) ) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if val_ < 0.0 then failwith "negative distance" else match from_unit with | Foot -> ( match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_ /. 5280.0) ) | 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.0) | Meter -> (Meter, val_ *. 1609.344) | Mile -> (Mile, val_) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0.0 then failwith "negative speed" else let (d_from_unit, t_from_unit) = from_unit in let (d_to_unit, t_to_unit) = to_unit in let (_, dist_conv_val) = convert_dist (d_from_unit, val_) d_to_unit in let (_, time_conv_val) = convert_time (t_from_unit, 1.0) t_to_unit in ((d_to_unit, t_to_unit), dist_conv_val /. time_conv_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = if speed_val < 0.0 then failwith "negative speed" else let (speed_dist_unit, speed_time_unit) = speed_unit in let (_, conv_val) = convert_time time speed_time_unit in (speed_dist_unit, conv_val *. speed_val) ;; let rec passes_da_vinci t = let rec sum_of_squares (l: tree list) acc = match l with | [] -> acc | Branch(val_, _)::xs -> sum_of_squares xs ((val_*.val_) +. acc) | Leaf::xs -> sum_of_squares xs acc in let rec check_all (l: tree list) = match l with | [] -> true | Leaf::xs -> check_all xs | x::xs -> (passes_da_vinci x) && (check_all xs) in match t with | Leaf -> true | Branch(val_, list) -> ((sum_of_squares list 0.0) <= (val_*.val_)) && (check_all list) ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.