text
stringlengths 0
601k
|
---|
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) ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if List.length l = 0 then if cur_num > max_num then cur_el else if max_num > cur_num then max_el else min cur_el max_el else let x = List.hd l in let xs = List.tl l in 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 if max_num>cur_num then aux xs (x, 1) (max_el, max_num) else aux xs (x, 1) (min max_el cur_el, max_num) in let l = List.sort compare l in match l with | [] -> failwith "list was empty" | hd::tl -> aux tl (hd, 1) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l <= 1 then failwith "list does not have pairs of elements" else let tuple a b = (a, b) in let tl = List.tl l in let list_rev = List.rev l in let rev_less_tail = List.tl list_rev in let l_less_last_element = List.rev rev_less_tail in mode (List.map2 tuple l_less_last_element tl) ;; |
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 -> (Hour, val_ *. 3600. ) | from_unit, to_unit -> (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit,to_unit with | Foot, Meter -> (Meter, val_ *. 0.3048) | Foot, Mile -> (Mile, val_ /. 5280.) | Meter, Foot -> (Foot, val_ /. 0.3048) | Meter, Mile -> (Mile, val_ /. (5280. *. 0.3048)) | Mile, Foot -> (Foot, val_ *. 5280.) | Mile, Meter -> (Meter, val_ *. (5280. *. 0.3048)) | from_unit,to_unit -> (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 dist_, amount = convert_dist (from_dist, val_) to_dist in let time_, amount = convert_time (to_time, amount) from_time in (dist_,to_time), amount ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time_unit, time_amt = time in let right_speed = convert_speed (speed_unit, speed_val) (fst speed_unit, time_unit) in fst speed_unit, ((snd right_speed) *. time_amt) ;; let rec passes_da_vinci t = let get_branch_width_squared bran = match bran with | Leaf -> 0. | Branch (width_, branches_) -> (width_ *. width_) in match t with | Leaf -> true | Branch (root_width, root_branches) -> let get_sums node = match node with | Leaf -> 0. | Branch (width, branches)-> List.fold_left (+.) 0. (List.map get_branch_width_squared branches) in let branch_list = List.map passes_da_vinci root_branches in let root_sum = get_sums t in not (List.mem false branch_list) && (root_width *. root_width) >= root_sum ;; |
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 | x :: xs -> aux xs (x,1) (x,1) |_ -> failwith "Invalid input" ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l2 = l in match l with |x :: xs -> let l = xs in (match List.rev l2 with |f :: fs -> let l2 = List.rev fs in mode (List.combine l2 l) |_ -> failwith "Invalid input") |_ -> failwith "Invalid input" ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,val_) with |(Second, x) -> (match to_unit with |Second -> (Second, x) |Hour -> (Hour, x/.3600.0)) |(Hour, x) -> (match to_unit with |Second ->(Second, x *. 3600.0) |Hour -> (Hour,x)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,val_) with |(Foot,x) -> (match to_unit with |Foot -> (Foot,x) |Meter -> (Meter, x *. 0.3048) |Mile -> (Mile,x /. 5280.0)) |(Meter,x) -> (match to_unit with |Foot -> (Foot,x /. 0.3048) |Meter -> (Meter, x) |Mile -> (Mile,(x /. 0.3048)/.5280.0)) |(Mile,x) -> (match to_unit with |Foot -> (Foot,x *. 5280.0) |Meter -> (Meter,x *. 5280.0 *. 0.3048) |Mile -> (Mile,x)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let fdist = convert_dist ((fst from_unit),val_) (fst to_unit) in let ftime = convert_time ((snd to_unit),(snd fdist)) (snd from_unit) in (to_unit, (snd ftime)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let new_a = convert_speed ((fst a),(snd a)) b_unit in (b_unit, (snd new_a) +. b_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) && (sum_ (Branch(width, subtree)) <= 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 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 | x :: xs -> aux xs (x,1) (x,1) |_ -> failwith "Invalid input" ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l2 = l in match l with |x :: xs -> let l = xs in (match List.rev l2 with |f :: fs -> let l2 = List.rev fs in mode (List.combine l2 l) |_ -> failwith "Invalid input") |_ -> failwith "Invalid input" ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,val_) with |(Second, x) -> (match to_unit with |Second -> (Second, x) |Hour -> (Hour, x/.3600.0)) |(Hour, x) -> (match to_unit with |Second ->(Second, x *. 3600.0) |Hour -> (Hour,x)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit,val_) with |(Foot,x) -> (match to_unit with |Foot -> (Foot,x) |Meter -> (Meter, x *. 0.3048) |Mile -> (Mile,x /. 5280.0)) |(Meter,x) -> (match to_unit with |Foot -> (Foot,x /. 0.3048) |Meter -> (Meter, x) |Mile -> (Mile,(x /. 0.3048)/.5280.0)) |(Mile,x) -> (match to_unit with |Foot -> (Foot,x *. 5280.0) |Meter -> (Meter,x *. 5280.0 *. 0.3048) |Mile -> (Mile,x)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let fdist = convert_dist ((fst from_unit),val_) (fst to_unit) in let ftime = convert_time ((snd to_unit),(snd fdist)) (snd from_unit) in (to_unit, (snd ftime)) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let new_a = convert_speed ((fst a),(snd a)) b_unit in (b_unit, (snd new_a) +. b_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) && (sum_ (Branch(width, subtree)) <= width*.width)) then check tl else false 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 | h::t -> if h = cur_el then aux t (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux t (h,1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num) in match l with | [] -> failwith "List too small." | h::t -> aux t (h, 1) (h, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "List too small." | _::tl -> let copy_1 = tl in let reversed_list = List.rev l in let take_off_first = List.tl reversed_list in let copy_2 = List.rev take_off_first in let list_of_tuples = List.combine copy_2 copy_1 in mode list_of_tuples ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Second, Hour -> (Hour, val_ /. 3600.) | Hour, Second -> (Second, val_ *. 3600.) | Hour, Hour -> (Hour, val_) | Second, Second -> (Second, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (Meter, val_ *. 0.3048) | Foot, Mile -> ( Mile, val_ /. 5280.) | Meter, Foot -> ( Foot, val_ /. 0.3048) | Meter, Mile -> ( Mile, val_ /. (0.3048 *. 5280.)) | Mile, Foot -> ( Foot, val_ *. 5280. ) | Mile, Meter -> ( Meter, val_ *. (0.3048 *. 5280.)) | Foot, Foot -> (Foot, val_) | Meter, Meter -> (Meter, val_) | Mile, Mile -> (Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let dist_1 = fst(from_unit) in let time_1 = snd(from_unit) in let dist_2 = fst(to_unit) in let time_2 = snd(to_unit) in let dist_converted = snd(convert_dist (dist_1, val_) dist_2) in let time_converted = snd(convert_time (time_1, val_) time_2) in if (dist_1, time_1) = (dist_2,time_2) then (dist_2,time_2), val_ else (dist_2, time_2), val_ *. (dist_converted /. time_converted);; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let unit_a = fst(a) in let val_a = snd(a) in let conversion = convert_speed (unit_a, val_a) b_unit in let new_val_a = snd(conversion) in b_unit, new_val_a +. b_val;; |
let passes_da_vinci t = let rec sum_ trees = match trees with | [] -> 0. | h::t -> match h with | Leaf -> sum_ t | Branch (a, l1) -> a *. a +. sum_(t) in let rec check a b c = sum_(c) <= b *. b && match c with | [] -> true | h::t -> match h with | Leaf -> true | Branch (width, subtree) -> check (Branch(width, subtree)) width subtree in if t=Leaf then true else let Branch (a, d) = t in check t a d ;; |
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 | [] -> failwith "List too short." | [_] -> max_el | cur :: l -> let next_el = List.hd l in if cur = next_el then let new_cur_num = cur_num+1 in if new_cur_num > max_num then aux l (next_el, new_cur_num) (cur_el, new_cur_num) else aux l (next_el, new_cur_num) (max_el, max_num) else aux l (next_el, 1) (max_el, max_num) in if (List.length l) = 0 then failwith "List too small." else aux (List.sort compare l) (List.hd l, 1) (List.hd l, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l2_rev = (List.rev l) in let l2_rev_2 = List.tl l2_rev in let l2 = List.rev l2_rev_2 in let l3 = List.combine l2 l1 in mode l3 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Second then match to_unit with | Second -> (Second, val_); | Hour -> (Hour,val_ /. 3600.); else 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 = if from_unit = Foot then match to_unit with | Foot -> (Foot, val_); | Meter-> (Meter, val_ *. 0.3048); | Mile -> (Mile, val_ /. 5280.); else if from_unit = Meter then match to_unit with | Foot -> (Foot, val_ /. 0.3048); | Meter-> (Meter, val_); | Mile -> (Mile, (val_ /. 0.3048) /. 5280.); else 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 = 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 add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_speed_converted = convert_speed ((fst a), (snd a)) b_unit in (b_unit, (snd a_speed_converted) +. b_val) ;; |
let passes_da_vinci t = let rec sum_ (l: tree list) (acc: float) : float = match l with | [] -> 0.; | Leaf :: tl -> sum_ tl acc; | Branch (width, subtree) :: tl -> sum_ subtree acc+.width**2. +. sum_ tl acc; in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> (check subtree) && ((sum_ subtree 0.) <= (width**2.)) 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 (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 let l = List.sort compare l in let x :: xs = l in aux l (x, 0) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let pair_list l = let rec aux l acc = match l with | [] -> acc | x :: [] -> acc | x :: y :: xs -> aux (y :: xs) ((x, y) :: acc) in aux l [] in mode (pair_list l) ;; |
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 from_unit with | Second -> (Hour, val_ /. 3600.) | Hour -> (Second, val_ *. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let con_ratio (d : dist_unit) = match d with | Foot -> 1. | Mile -> 5280. | Meter -> 1. /. 0.3048 in let val_ = val_ *. (con_ratio from_unit /. con_ratio to_unit) in (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_d, from_t) = from_unit in let (to_d, to_t) = to_unit in let (dist, dval) = convert_dist (from_d, val_) to_d in let (time, tval) = convert_time (from_t, 1.) to_t in (to_unit, dval /. tval) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (d, t) = speed_unit in let (t_unit, t_val) = convert_time time t in (d, t_val *. speed_val) ;; let rec passes_da_vinci t : bool = let branch_square (t : tree) = match t with | Leaf -> 0. | Branch (width, sub) -> width *. width in let child_square_sum (Branch (width, sub)) = List.fold_left (fun sum b -> sum +. (branch_square b)) 0. sub in match t with | Leaf -> true | Branch (width, sub) -> ((child_square_sum t) <= width *. width) && List.for_all passes_da_vinci sub ;; |
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 (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 | [] -> failwith "Empty" | x::xs -> aux xs (x,1) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pairs (l: 'a list) (curr: 'a) (temp: ('a * 'a) list): ('a * 'a) list= match l with |[] -> temp |x::xs -> pairs xs x ((curr,x)::temp) in match l with |[] -> failwith "Empty" |x::xs -> match xs with |[] -> failwith "Only 1 Element IE no Pairs" |y::ys -> mode (pairs xs x []) ;; |
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.0)) |Hour -> (match to_unit with |Second -> (Second,3600.0*.val_) |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,0.3048*.val_) |Mile -> (Mile,val_/.5280.0)) |Meter -> (match to_unit with |Foot -> (Foot,val_/.0.3048) |Meter -> (Meter,val_) |Mile -> (Mile,(val_/.0.3048)/.5280.0)) |Mile -> (match to_unit with |Foot -> (Foot,5280.0*.val_) |Meter -> (Meter,5280.0*. 0.3048*.val_) |Mile -> (Mile,val_)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (d1,t1)= from_unit in let (d2,t2)= to_unit in let (x,interm)=(convert_dist (d1,val_) d2) in let (y,final)=convert_time (t2,interm) t1 in ((d2,t2),final) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (x,magn2)=convert_speed a b_unit in (b_unit, magn2+.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.0)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && (sum_ subtree 0.0 <= width**2.0)) then false else check tl in check [t] ;; |
let notimplemented () = failwith "This function is not yet implemented." type dist_unit = Foot | Meter | Mile type time_unit = Second | Hour type speed_unit = dist_unit * time_unit type 'a value = 'a * float type tree = Branch of float * tree list | Leaf let tree_example = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., []) ]) ;; |
let mode (l: 'a list) : 'a = if(List.length l = 0) then failwith "The list is empty."; let sortedList = List.sort compare (l) in let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = if (List.length l = 0) then ( if(cur_num>max_num) then cur_el else max_el ) else if(List.hd l = cur_el) then aux (List.tl l) ((List.hd l),(cur_num+1)) (max_el,max_num) else ( if (cur_num > max_num) then aux (List.tl l) ((List.hd l),1) (cur_el,cur_num) else aux (List.tl l) ((List.hd l),1) (max_el,max_num) ) in aux (List.tl sortedList) ((List.hd sortedList),1) (List.hd sortedList,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if(List.length l < 2) then failwith "The list has no pairs"; let taillessList = List.rev (List.tl (List.rev (l))) in let headlessList = List.tl l in let pairList = List.combine taillessList headlessList in mode pairList ;; |
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 from_dist = fst from_unit in let from_time = snd from_unit in let dest_dist = fst to_unit in let dest_time = snd to_unit in if(from_dist = dest_dist && from_time = dest_time) then (to_unit, val_) else if (from_dist = dest_dist) then (to_unit, snd (convert_time (dest_time,val_) from_time)) else if (from_time = dest_time) then (to_unit, snd (convert_dist (from_dist,val_) dest_dist)) else (to_unit, snd (convert_dist (from_dist, snd (convert_time (dest_time,val_) from_time)) dest_dist)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let newTimeVal = snd (convert_time time (snd speed_unit)) in let resDist = newTimeVal *. speed_val in (fst speed_unit, resDist) ;; let (tree1: tree) = Leaf let (tree2: tree) = Branch (0.0, []) let (tree3: tree) = Branch (12.0, [ Branch(12.,[]); Leaf; Branch(5.0,[ Branch(4.0,[]); Branch(3.0,[]); ]); ]) let (tree4: tree) = Branch(6.0, [ Branch(4.0,[ Branch(3.0,[]) ]); Branch(4.0,[]) ]) let (tree5: tree) = Branch(145.,[ Branch(144.,[ Branch(6.0,[]); Branch(7.0,[]); Branch(3.0,[]); Branch(2.0,[]); ]); Branch(17., [ Branch(8.,[]); Branch(15.,[ Branch(12.,[]); Branch(7.,[]); Leaf; Leaf; ]); ]); ]) let rec sum_of_squares (prev: float) (kids: tree list) : float = match kids with | [] -> prev | Leaf :: t -> sum_of_squares prev t | Branch (x,_) :: t -> sum_of_squares (prev+.x*.x) t ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, branches) -> ((sum_of_squares 0. branches) <= (width *. width)) && (List.for_all passes_da_vinci branches) ;; |
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 max_el else cur_el | hd::tl -> if hd = cur_el then aux tl (hd, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Must enter non empty list!" | hd::tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | _::[] -> failwith "Must enter a list of length greater than 1" | _::tl -> let l1 = tl in let lrev = List.rev l in match lrev with | _::tl -> let l2 = List.rev tl in let lcompare = List.combine l2 l1 in mode lcompare | [] -> failwith "Reversed list is empty" ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Hour, Hour) -> (Hour, val_) | (Second, Second) -> (Second, val_) | (Hour, Second) -> (to_unit, val_ *. 3600.) | (Second, Hour) -> (to_unit, val_ /. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Foot) -> (Foot, val_) | (Meter, Meter) -> (Meter, val_) | (Mile, Mile) -> (Mile, val_) | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. (5280. *. 0.3048)) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. 5280. *. 0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match (from_unit, to_unit) with | ( to_unit, from_unit) when from_unit = to_unit -> (to_unit, val_) | ((from_dist_unit, from_time_unit), (to_dist_unit, to_time_unit)) -> let (_,distConversion) = convert_dist (from_dist_unit, 1.) to_dist_unit in let (_, timeConversion) = convert_time (from_time_unit, 1.) to_time_unit in (to_unit, val_ *. distConversion /. timeConversion) ;; |
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 branchSummation branch = match branch with | [] -> 0. | Leaf :: tl -> branchSummation tl | Branch (width, _) :: tl -> (width *. width) +. branchSummation tl in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && ((branchSummation subtree) <= (width ** 2.))) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | l::ls -> if l = cur_el && cur_num = max_num then aux ls (l, cur_num + 1) (l, max_num + 1) else if l = cur_el then aux ls (l, cur_num + 1) (max_el, max_num) else aux ls (l, 1) (max_el, max_num) in let l = List.sort compare l in match l with | [] -> failwith "Empty list" | l::ls -> aux ls (l, 1) (l, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux l acc = match l with | [] -> acc | l::[] -> acc | l::ls -> aux ls ((l,List.nth ls 0)::acc) in let l = aux l [] in mode l ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | (unit_1, _) -> (unit_1, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. (0.3048 *. 5280.)) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. 5280. *. 0.3048) | (unit_1, _) -> (unit_1, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_unit_dist, from_unit_time), val_, (to_unit_dist, to_unit_time)) = (from_unit, val_, to_unit) in let val_1 = let (_, another_val) = convert_dist (from_unit_dist, val_) to_unit_dist in another_val in let val_2 = let (_, another_value) = convert_time (from_unit_time, 1.) to_unit_time in another_value in ((to_unit_dist, to_unit_time), val_1 /. val_2) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (((dist_1, time_1), val_1), (dist_2, time_2)) = (a, b_unit) in let to_add = let (_, value) = convert_speed ((dist_1, time_1), val_1) (dist_2, time_2) in value in (b_unit, to_add +. b_val) ;; |
let passes_da_vinci t = let l = tree_traversal t [] in List.for_all (fun x -> x) 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 | [] -> max_el | l::ls -> if l = cur_el && cur_num = max_num then aux ls (l, cur_num + 1) (l, max_num + 1) else if l = cur_el then aux ls (l, cur_num + 1) (max_el, max_num) else aux ls (l, 1) (max_el, max_num) in let l = List.sort compare l in match l with | [] -> failwith "Empty list" | l::ls -> aux ls (l, 1) (l, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec aux l acc = match l with | [] -> acc | l::[] -> acc | l::ls -> aux ls ((l,List.nth ls 0)::acc) in let l = aux l [] in mode l ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | (unit_1, _) -> (unit_1, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Meter, Mile) -> (Mile, val_ /. (0.3048 *. 5280.)) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. 5280. *. 0.3048) | (unit_1, _) -> (unit_1, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_unit_dist, from_unit_time), val_, (to_unit_dist, to_unit_time)) = (from_unit, val_, to_unit) in let val_1 = let (_, another_val) = convert_dist (from_unit_dist, val_) to_unit_dist in another_val in let val_2 = let (_, another_value) = convert_time (from_unit_time, 1.) to_unit_time in another_value in ((to_unit_dist, to_unit_time), val_1 /. val_2) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (((dist_1, time_1), val_1), (dist_2, time_2)) = (a, b_unit) in let to_add = let (_, value) = convert_speed ((dist_1, time_1), val_1) (dist_2, time_2) in value in (b_unit, to_add +. b_val) ;; |
let passes_da_vinci t = let l = tree_traversal t [] in List.for_all (fun x -> x) l ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "Invalid input"; let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd::tl -> if hd = cur_el then( if cur_el = max_el then aux tl (cur_el, cur_num + 1) (max_el, max_num + 1) else(if cur_num >= 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 aux tl (hd, 1) (max_el, max_num) in aux (List.sort compare l) (List.hd (List.sort compare l), 0) (List.hd (List.sort compare l), 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid input" | x::xs -> if (List.length l) < 2 then failwith "Invalid input"; let l1 = xs in match List.rev l with | [] -> failwith "Invalid input" | hd::tl -> let l2 = List.rev tl 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 to_unit with | Hour -> (match from_unit with | Hour -> (to_unit, val_) | Second -> (to_unit, val_ /. 3600.)) | Second -> (match from_unit with | Second -> (to_unit, val_) | Hour -> (to_unit, val_ *. 3600.)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> (match from_unit with | Foot -> (to_unit, val_) | Meter -> (to_unit, val_ /. 0.3048) | Mile -> (to_unit, val_ *. 5280.)) | Meter -> (match from_unit with | Foot -> (to_unit, val_ *. 0.3048) | Meter -> (to_unit, val_) | Mile -> (to_unit, val_ *. 5280. *. 0.3048)) | Mile -> (match from_unit with | Foot -> (to_unit, val_ /. 5280.) | Meter -> (to_unit, val_ /. 0.3048 /. 5280.) | Mile -> (to_unit, val_)) ;; |
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 match snd(from_unit) with | Second -> (match snd(to_unit) with | Second -> (to_unit, snd(d)) | Hour -> (to_unit, snd(d) *. 3600.)) | Hour -> (match snd(to_unit) with | Second -> (to_unit, snd(d) /. 3600.) | Hour -> (to_unit, snd(d))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let s2 = convert_speed (fst(a), snd(a)) b_unit in let sum = (b_unit, snd(s2) +. b_val) in sum ;; |
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 not((check subtree) && ((sum subtree 0.) <= width ** 2.)) then false else check tl in check [t] ;; |
let get_el l = match l with | [] -> failwith "Empty List" | hd :: tl -> hd;; |
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 if (cur_num > max_num) then cur_el else max_el | hd :: tl -> if (hd = cur_el) then aux tl (hd, cur_num + 1) (max_el, max_num) else if (hd = max_el) then aux tl (cur_el, cur_num) (max_el, max_num + 1) 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 (List.sort compare l) (get_el(List.sort compare l), 0) (get_el((List.sort compare l)),0) ;; |
let pair_mode (l: 'a list) = match l with | [] -> failwith "Empty list" | _ -> let (_ :: l2) = List.rev l in let (_ :: l3) = l in let comb = List.combine (List.rev l2) ( l3) in mode comb ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Hour, Second -> (to_unit, val_ *. 3600.) | Second, Hour -> (to_unit, val_ /. 3600.) | _ -> (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Meter -> (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) | _ -> (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit, to_unit with | (_, Hour), (_, Hour) -> (to_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) | (_, Second), (_, Second) -> (to_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit)))) | (_, Hour), (_, Second) -> (to_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit))) /. 3600.) | (_, Second), (_, Hour) -> (to_unit, snd(convert_dist (fst(from_unit), val_) (fst(to_unit))) *. 3600.) ;; |
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, 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 (width ** 2. >= sum_ subtree 0.) && (check subtree) 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::t -> if cur_el = x then aux t (cur_el, cur_num +1) (max_el, max_num) else if cur_num > max_num then aux t (x, 1) (cur_el, cur_num) else aux t (x, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input." | x::t -> aux t (x, 1) (x, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input" | _::t -> 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, val_), to_unit with | (Second, val_), Hour -> (Hour, val_ /. 3600.) | (Hour, val_), Second -> (Second, val_ *. 3600.) | (Hour, val_), Hour -> (Hour, val_) | (Second, val_), Second -> (Second, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match (from_unit, val_), to_unit with | (Foot, val_), Meter -> (Meter, val_ *. 0.3048) | (Foot, val_), Mile -> (Mile, val_ /. 5280.) | (Foot, val_), Foot -> (Foot, val_) | (Meter, val_), Foot -> (Foot, val_ /. 0.3048) | (Meter, val_), Mile -> (Mile, val_ /. 0.3048 /. 5280.) | (Meter, val_), Meter -> (Meter, val_) | (Mile, val_), Foot -> (Foot, val_ *. 5280.) | (Mile, val_), Meter -> (Meter, val_ *. 5280. *. 0.3048) | (Mile, val_), Mile -> (Mile, val_) ;; let first ((a, b) : 'a * 'b) : 'a = a;; let second ((a, b) : 'a * 'b) : 'b = b;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.