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 |[]-> if((compare cur_num max_num)>0) then cur_el else max_el |hd::tl ->if(hd = cur_el) then if((compare cur_num max_num) = -1) then aux tl (cur_el, cur_num+1) (max_el, max_num) else aux tl (cur_el, cur_num+1) (cur_el, cur_num+1) else aux tl (hd, 1) (max_el, max_num) in let l = (List.sort compare l) in match l with |[]-> failwith "Undefined Input" |[x] -> x |hd::tl -> aux l (hd,0) (hd,1) ;; let get_pairs (l: 'a list) : ('a *'a) list = let rec helper (l: 'a list) (nl: (('a * 'a) list)) (i0: int) (i1: int) (len: int) = if(i1 >= len) then (List.sort compare nl) else let x = (List.nth l i0) in let y = (List.nth l i1) in let nl = nl @ [(x,y)] in helper l (nl) (i0+1) (i1+1) len in let nl = [] in let len = (List.length l) in match len with |len when len < 2 -> failwith "Undefined Input" |_ -> helper l nl 0 1 len ;; |
let pair_mode (l: 'a list) : 'a * 'a = let nl = (get_pairs l) in let len = (List.length l) in match len with |len when len < 2 -> failwith "Undefinded Input" |_ -> (mode nl) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit,to_unit) with |(Second, Second) -> (to_unit, val_) |(Hour, Hour) -> (to_unit, 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, Meter) -> (Meter, (val_ *. 0.3048)) |(Foot, Mile) -> (Mile, (val_ /. 5280.)) |(Meter, Foot) -> (Foot, (val_ /. 0.3048)) |(Meter, Mile) -> (Mile, (val_ /. 1609.344)) |(Mile, Foot) -> (Foot, (val_ *. 5280.)) |(Mile, Meter) -> (Meter,(val_ *. 1609.344)) |(_,_) -> (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match (from_unit,to_unit) with |((dFrom,Second),(dTo,Hour)) -> let (d_unit,d_value) = (convert_dist (dFrom, val_) dTo) in ((d_unit, Hour), d_value *. 3600.) |((dFrom,Hour),(dTo,Second)) -> let (d_unit,d_value) = (convert_dist (dFrom ,val_) dTo) in ((d_unit, Second),d_value /. 3600.) |((dFrom,Second),(dTo,Second)) -> let (d_unit,d_value) = (convert_dist (dFrom ,val_) dTo) in ((d_unit, Second),d_value) |((dFrom,Hour),(dTo,Hour)) -> let (d_unit,d_value) = (convert_dist (dFrom ,val_) dTo) in ((d_unit, Hour), d_value) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let nA = convert_speed a b_unit in let (_, a_val) = nA in (b_unit, a_val +. b_val) ;; |
let passes_da_vinci t = match t with |Leaf -> true |Branch(width, list) -> if(branch_pass (width,list)) then let rec check (l : tree list) : bool = match l with |[]-> true |Leaf::tl -> check tl |Branch(w,l)::tl -> if((check l) && (branch_pass (w,l))) then check tl else false in check list else false ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> if cur_num > max_num then cur_el else max_el | first :: rest -> if first = cur_el then aux rest (cur_el, (cur_num + 1)) (max_el, max_num) else if cur_num > max_num then aux rest (first, 1) (cur_el, cur_num) else aux rest (first, 1) (max_el, max_num) in let first::rest = List.sort compare l in aux rest (first, 1) (first, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let list1 = l and list2 = l in let first::rest = list1 and first2::rest2= List.rev list2 in let tuples = List.combine (List.rev rest2) rest in mode tuples ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then from_unit, val_ else if (from_unit = Second && to_unit = Hour) then to_unit, val_ /. 3600. else 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, Meter -> to_unit, val_ *. 0.3048 | Foot, Mile -> to_unit, val_ /. 5280. | Meter, Mile -> to_unit, (val_ /. 0.3048) /. 5280. | Meter, Foot -> to_unit, val_ /. 0.3048 | Mile, Foot -> to_unit, val_ *. 5280. | Mile, Meter -> to_unit, (val_ *. 0.3048) *. 5280. | _ -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let x = convert_dist (fst from_unit, 1.) (fst to_unit) and y = convert_time (snd from_unit, 1.) (snd to_unit) in to_unit, ((snd x) /. (snd y)) *. val_ ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let d = fst speed_unit and t = snd speed_unit in d, snd (convert_time time t) *. speed_val ;; let rec passes_da_vinci t = let rec sum_ l = match l with | [] -> 0. | Leaf :: tl -> sum_ tl | Branch (width, subtree) :: tl -> width ** 2. +. sum_ tl in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && ((sum_ subtree) <= (width ** 2.))) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let failure() = failwith "problem with implementation" 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 |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 match List.sort compare l with |[]->failure() |x :: xs -> aux xs (x,1) (x,0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let failure() = failwith "problem with implementation" in let removefirst l = match l with |[] -> failure() |x::xs -> xs in let l1 = removefirst l in let l2 = removefirst (List.rev l) in let l4 = List.rev l2 in let l3 = List.combine l4 l1 in mode l3 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with |Hour -> if from_unit = Second then (to_unit,val_/.3600.0) else (to_unit,val_) |Second -> if from_unit = Hour then (to_unit,val_*.3600.0) else (to_unit,val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> if from_unit = Meter then (to_unit,val_/.0.3048) else if from_unit = Mile then (to_unit,val_*.5280.0) else (to_unit,val_) | Meter -> if from_unit = Foot then (to_unit,val_*.0.3048) else if from_unit = Mile then (to_unit,val_*.1609.344) else (to_unit,val_) | Mile -> if from_unit = Foot then (to_unit,val_/.5280.) else if from_unit=Meter then (to_unit,val_/.1609.344) else (to_unit,val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let unit1 = fst(to_unit) in let x = convert_dist (fst(from_unit),val_) unit1 in let unit2 = snd(from_unit) in let xval = snd(x) in let y = convert_time( snd(to_unit), xval) unit2 in (to_unit, snd(y));; |
let add_speed ((a_unit, a_val): speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let x = convert_speed(a_unit,a_val) b_unit in let y = snd(x) +. b_val in (b_unit,y) ;; |
let passes_da_vinci t = let rec comp_square (l:tree list) : float = match l with |[] -> 0. |x::xs -> match x with |Leaf-> comp_square xs |Branch (wid,sub)-> wid*.wid +. comp_square xs in let rec check (l : tree list) : bool = match l with |[] -> true |Leaf :: xs-> true |Branch (wid,sub) :: xs -> if (check sub && (comp_square sub <= wid*.wid)) then check xs else false in check [t] ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith ("Invalid input list - Empty list") else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | head::tail -> if cur_el = head then if cur_num+1 > max_num then aux tail (cur_el, cur_num+1) (cur_el, cur_num+1) else aux tail (cur_el, cur_num+1) (max_el, max_num) else if 1 > max_num then aux tail (head, 1) (head, 1) else aux tail (head, 1) (max_el, max_num) in let sorted = List.sort compare l in aux sorted (List.hd sorted, 0) (List.hd sorted, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l = [] then failwith ("Invalid input list - Empty list") else let rec iterate (input_list: 'a list) total_list = match input_list with | _::[] -> mode total_list | el1::el2::[] -> mode ((el1,el2)::total_list) | el1::el2::tail -> iterate (el2::tail) ((el1,el2)::total_list) | _ -> notimplemented() in iterate l [] ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if val_ < 0. then failwith ("Invalid input - Time cannot be negative") else match (from_unit, to_unit) with | (Hour, Second) -> (Second, val_ *. 3600.) | (Second, Hour) -> (Hour, val_ /. 3600.) | (from_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_ /. 0.3048 /. 5280.) | (Mile, Foot) -> (Foot, val_ *. 5280.) | (Mile, Meter) -> (Meter, val_ *. 0.3048 *. 5280.) | (from_unit, _) -> (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((from_distance, from_time), (to_distance, to_time)) = (from_unit, to_unit) in ( (to_distance, to_time), snd (convert_dist (from_distance, val_) to_distance) /. snd (convert_time (from_time, 1.) to_time) ) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = (fst speed_unit,speed_val *. snd (convert_time time (snd speed_unit))) ;; let compute_sum (l : tree list) : float = let rec helper (l : tree list) (cur_sum : float) : float = match l with | [] -> cur_sum | head::body -> match head with | Branch (value, _) -> helper body cur_sum +. (value*.value) | Leaf -> helper body cur_sum +. 0. in helper l 0. ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (value, l) -> (value*.value >= compute_sum l) && let rec recursive_call l = match l with | [] -> true | head::body -> passes_da_vinci head && recursive_call body in recursive_call l ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "The list is empty" 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 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 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 head_to_tail = List.rev(List.tl(List.rev l)) in let tail = List.tl l in let combined = List.combine head_to_tail tail in mode combined ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Hour -> if to_unit = Hour then (to_unit, val_) else (to_unit, val_ *. 3600.) | Second -> if to_unit = Hour then (to_unit, val_ /. 3600.) else (to_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Foot then (to_unit, val_) else if to_unit = Meter then (to_unit, val_ *. 0.3048) else (to_unit, val_ /. 5280.) | Meter -> if to_unit = Meter then (to_unit, val_) else if to_unit = Foot then (to_unit, val_ /. 0.3048) else (to_unit, val_ /. 0.3048 /. 5280.) | Mile -> if to_unit = Mile then (to_unit, val_) else if to_unit = Foot then (to_unit, val_ *. 5280.) else (to_unit, val_ *. 5280. *. 0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let from_distance_unit = fst(from_unit) in let from_time_unit = snd(from_unit) in let to_distance_unit = fst(to_unit) in let to_time_unit = snd(to_unit) in let d = snd(convert_dist (from_distance_unit, val_) to_distance_unit) in let t = snd(convert_time (from_time_unit, 1.) to_time_unit) in (to_unit, d /. t) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let time1 = convert_speed (fst(a), snd(a)) b_unit in (b_unit, snd(time1) +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: l -> 0. | Branch (width,subtree) :: xs -> sum_ xs (acc +. width *. width) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width,subtree) :: tl -> if width *. width >= (sum_ subtree 0.) then check subtree else false in check [t] ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "empty list" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h :: t -> let (ce, cn), (me, mn) = (cur_el, cur_num), (max_el, max_num) in if h = ce && (cn + 1 > mn) then aux t (ce, cn+1) (ce, cn+1) else if h = ce && (cn + 1 <= mn) then aux t (ce, cn+1) (me, mn) else aux t (h,1) (me, mn) in let ls = List.sort compare l in aux ls ( List.hd ls, 0) (List.hd ls, 0) ;; let rec to_tuple (ls: 'a list)= match ls with | [] -> [] | h :: t -> match t with | [] -> [] | a :: b -> (h, a) :: to_tuple t ;; let a = to_tuple [3;4;5;6;7];; |
let pair_mode (l: 'a list) = match l with | [] -> failwith "empty list" | h :: t -> mode (to_tuple l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, val_) -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /.3600.) ) | (Hour, val_) -> (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, val_) with | (Foot, val_) -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, 0.3048 *.val_) | Mile -> (Mile, val_ /. 5280.) ) | (Meter, val_) -> (match to_unit with | Foot -> (Foot, val_ *. (1. /. 0.3048)) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /.0.3048 /. 5280.) ) | (Mile, val_) -> (match to_unit with | Foot -> (Foot, 5280. *. val_) | Meter -> (Meter, 5280. *. 0.3048 *.val_) | Mile -> (Mile, val_ ) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((d1,t1), val_), (d2, t2) = ((from_unit, val_), to_unit) in let (distance, val2) = convert_dist (d1, val_) d2 in let (time,val3) = convert_time (t2, val2 ) t1 in ((distance, t2), val3) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (dt, val_) = convert_speed a b_unit in (b_unit, val_ +. b_val) ;; |
let passes_da_vinci t = match t with | Leaf -> true | Branch (v, ls) -> (( v ** 2.) >= sum_squares 0. ls) && recur_sub ls ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "empty list" else let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | h :: t -> let (ce, cn), (me, mn) = (cur_el, cur_num), (max_el, max_num) in if h = ce && (cn + 1 > mn) then aux t (ce, cn+1) (ce, cn+1) else if h = ce && (cn + 1 <= mn) then aux t (ce, cn+1) (me, mn) else aux t (h,1) (me, mn) in let ls = List.sort compare l in aux ls ( List.hd ls, 0) (List.hd ls, 0) ;; let rec to_tuple (ls: 'a list)= match ls with | [] -> [] | h :: t -> match t with | [] -> [] | a :: _ -> (h, a) :: to_tuple t ;; |
let pair_mode (l: 'a list) = match l with | [] -> failwith "empty list" | _ :: t -> match t with | [] -> failwith "one element only" | _ :: _ -> mode (to_tuple l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, val_) with | (Second, val_) -> (match to_unit with | Second -> (Second, val_) | Hour -> (Hour, val_ /.3600.) ) | (Hour, val_) -> (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, val_) with | (Foot, val_) -> (match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, 0.3048 *.val_) | Mile -> (Mile, val_ /. 5280.) ) | (Meter, val_) -> (match to_unit with | Foot -> (Foot, val_ *. (1. /. 0.3048)) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /.0.3048 /. 5280.) ) | (Mile, val_) -> (match to_unit with | Foot -> (Foot, 5280. *. val_) | Meter -> (Meter, 5280. *. 0.3048 *.val_) | Mile -> (Mile, val_ ) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((d1,t1), val_), (d2, t2) = ((from_unit, val_), to_unit) in let (distance, val2) = convert_dist (d1, val_) d2 in let (_,val3) = convert_time (t2, val2 ) t1 in ((distance, t2), val3) ;; |
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 : bool = match t with | Leaf -> true | Branch (v, ls) -> (( v ** 2.) >= sum_squares 0. ls) && recur_sub ls ;; |
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 (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)) else aux tl (hd, 1) (max_el, max_num) in let sorted = List.sort compare l in match sorted with | [] -> failwith "invalid input" | hd :: tl -> aux sorted (hd, 0) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = mode(List.combine (match List.rev l with | [] -> failwith "uh oh" | hd :: tl -> List.rev tl) (match l with | [] -> failwith "uh oh" | hd :: tl -> tl)) ;; |
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 = to_unit, (snd (convert_time ((snd to_unit), (snd (convert_dist ((fst from_unit), val_) (fst to_unit)))) (snd from_unit))) ;; |
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 *. width)) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if not ((check subtree) && ((sum_ subtree 0.) <= (width *. width))) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let 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_num + 1 > max_num then aux tl (cur_el, cur_num + 1) (cur_el, cur_num + 1) else aux tl (cur_el, cur_num +1 ) (max_el, max_num) else aux tl (hd, 1) (max_el, max_num) in match List.sort compare l with | [] -> notimplemented () | hd :: tl -> aux (tl) (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> notimplemented () | x:: [] -> notimplemented () | newList -> let noHead = List.tl (newList) in mode(List.combine (List.rev (List.tl (List.rev l)))(noHead)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Hour && to_unit= Second then (Second, val_ *.3600.) else if from_unit = Hour && to_unit= Hour then (from_unit, val_) else if from_unit = Second && to_unit= Second then (from_unit, val_) else (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) -> (to_unit, val_) |(Foot,Meter) -> (Meter, val_ *. 0.3048) |(Foot,Mile) -> (Mile, val_ /.5280.) |(Meter,Meter) -> (to_unit, val_) |(Meter,Foot) -> (Foot, val_ /. 0.3048 ) |(Meter,Mile) -> (Mile, val_ /.1609.344 ) |(Mile,Mile) -> (to_unit, val_) |(Mile,Foot) -> (Foot, val_ *.5280.) |(Mile,Meter) -> (Meter, val_ *.1609.344) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (distance_, time_) = from_unit in let (dist, val_dist)= convert_dist (distance_, val_) (fst(to_unit)) in let (time, val_time)= convert_time (time_, 1.) (snd(to_unit)) in ((dist,time), val_dist/.val_time) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let ((dist1, time1), val_1) = a in let ((dist2,time2)) = (b_unit) in let ((newdistance, newtime), newval) = convert_speed ((dist1, time1), val_1) (dist2,time2) in ((newdistance,newtime), newval +. 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 (width *. width +. acc) in let rec check l = match l with | [] -> true | Leaf :: tl -> 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 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 | hd :: tl -> if hd = cur_el then begin let cur_num = cur_num + 1 in if cur_num > max_num then aux tl (cur_el, cur_num) (cur_el, cur_num) else aux tl (cur_el, cur_num) (max_el, max_num) end else aux tl (hd, 1) (max_el, max_num) in match l with | [] -> failwith "Undefined input" | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Undefined input" else let l1 = List.tl l in let l2 = List.rev (List.tl (List.rev l)) in mode (List.combine l2 l1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let con = 3600. in match (from_unit, to_unit) with | (Hour, Second) -> (to_unit, val_ *. con) | (Second, Hour) -> (to_unit, val_ /. con) | (_,_) -> (to_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let con1 = 5280. in let con2 = 0.3048 in match (from_unit, to_unit) with | (Mile, Foot) -> (to_unit, val_ *. con1) | (Foot, Mile) -> (to_unit, val_ /. con1) | (Meter, Foot) -> (to_unit, val_ /. con2) | (Foot, Meter) -> (to_unit, val_ *. con2) | (Meter, Mile) -> (to_unit, val_ /. (con1 *. con2)) | (Mile, Meter) -> (to_unit, val_ *. (con1 *. con2)) | (_,_) -> (to_unit, val_) ;; |
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 (unit1,temp_val1) = convert_dist (from_dist, val_) to_dist in let (unit2,temp_val2) = convert_time (to_time, temp_val1) from_time in ((unit1, unit2), temp_val2) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let ((t_unit,t_value),(dis_unit,_)) = (time, speed_unit) in let (_,temp_val) = convert_speed (speed_unit, speed_val) (dis_unit, t_unit) in (dis_unit, (temp_val *. t_value)) ;; let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, children) -> let rec sum_ l num = match l with | [] -> num | Leaf :: tl -> sum_ tl num | Branch (width, _) :: tl -> sum_ tl (num +. width**2.) in if sum_ children 0. > width**2. then false else List.for_all passes_da_vinci children ;; |
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+1) > max_num then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else aux xs (cur_el, cur_num+1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in let lst = List.sort compare l in aux lst ((List.hd lst),0) ((List.hd lst),0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let com l = List.combine (List.rev (List.tl (List.rev l))) (List.tl l) in mode (com 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, Mile) -> (to_unit, val_ /. 5280. /. 0.3048) | (Meter, Foot) -> (to_unit, val_ /. 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 timvalue = convert_time ((snd from_unit), 1.) (snd to_unit) in let disvalue = convert_dist ((fst from_unit), val_) (fst to_unit) in (to_unit, ((snd disvalue) /. (snd timvalue))) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let con = convert_speed a b_unit in (b_unit, ((snd con) +. b_val)) ;; |
let passes_da_vinci t = 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+1>max_num then aux xs (cur_el, cur_num+1) (cur_el, cur_num+1) else aux xs (cur_el, cur_num+1) (max_el, max_num)) else aux xs (x, 1) (max_el, max_num) in (let li=(List.sort compare (l)) in match li with |x :: xs -> aux xs (x, 1) (x,1) |[] ->failwith "The provided list is empty." ) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let r = List.rev l in match l with |_ :: xs -> (match r with |[] ->failwith "The provided list is empty." |_ :: rs -> mode (List.combine (List.rev rs) (xs)) ) |[] ->failwith "The provided list is empty." ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> (match to_unit with |Hour -> Hour, val_/.3600. |_ -> Second, val_) |_ -> (match to_unit with |Second -> Second, val_*.3600. |_ -> Hour, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> (match to_unit with |Meter -> Meter, val_*.0.3048 |Mile -> Mile, val_/.5280. |_ -> Foot, val_) |Meter -> (match to_unit with |Foot -> Foot,val_/.0.3048 |Mile -> Mile, val_/.(0.3048 *. 5280.) |_ -> Meter,val_) |_ -> (match to_unit with |Meter -> Meter,val_*.5280.*.0.3048 |Foot -> Foot,val_*.5280. |_ -> Mile, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist_unit,time_unit) = from_unit in (let (dist_unit2,time_unit2) = to_unit in let (_,new_dist_val) = convert_dist (dist_unit, val_) dist_unit2 in let (_,new_time_val) = convert_time (time_unit, 1.) time_unit2 in (to_unit, (new_dist_val /. new_time_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 (_,new_val) = convert_speed (a_unit, a_val) b_unit in (b_unit, (b_val +. new_val)) ;; |
let passes_da_vinci t = let rec child_list li = match li with |[]-> 0. |x :: xs -> match x with |Leaf -> 0. |Branch (fl, _) -> fl*.fl +. child_list xs in let check_one_tree t = match t with |Leaf -> true |Branch (fl, l) -> (fl *. fl) >= (child_list l) in let rec check_all t l = match t with |Leaf -> (match l with |[]-> true |x :: xs -> check_all x xs) |Branch (_, []) -> (match l with |[]-> true |x :: xs ->(check_one_tree t) && (check_all x xs)) |Branch (_, y :: ys) -> match l with |[]-> (check_one_tree t) && (check_all y ys) |x :: xs -> (check_one_tree t) && ((check_all x xs) && (check_all y ys)) in check_all t [] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with |[] -> if cur_num > max_num then cur_el else max_el |h::t -> if h = cur_el then aux t (cur_el, (cur_num + 1)) (max_el, max_num) else( if cur_num > max_num then aux t (h, 1) (cur_el, cur_num) else aux t (h, 1) (max_el, max_num)) in let l = List.sort compare l in match l with |[] -> failwith "ERROR: you inputted an empty list, there is no mode" |h::t -> aux (h::t) (h, 0) (h, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec get_pairs (l: 'a list) (cur: ('a * 'a) list) (prev :'a) : ('a * 'a) list = match l with | [] -> cur | h::t -> get_pairs t ((prev, h)::cur) h in match l with | [] -> failwith "ERROR: There is no mode since you inputted an empty list" | h::t -> mode (get_pairs t [] h) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if(to_unit = Second) then (to_unit, val_) else (to_unit, val_ /. 3600.) |Hour -> if (to_unit = Hour) then (to_unit, val_) else (to_unit, val_ *. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Foot -> if to_unit = Foot then (to_unit, val_) else if to_unit = Meter then (to_unit, val_ *. 0.3048) else (to_unit, val_ /. 5280.) |Meter -> if to_unit = Foot then (to_unit, val_ /. 0.3048) else if to_unit = Meter then (to_unit, val_) else (to_unit, ((val_ /. 0.3048) /. 5280.)) |Mile -> if to_unit = Foot then (to_unit, val_ *. 5280.) else if to_unit = Meter then (to_unit, (val_ *. 5280. *. 0.3048)) else (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_d_unit, from_t_unit) = (from_unit) in let (to_d_unit, to_t_unit) = (to_unit) in let (_, distance) = convert_dist (from_d_unit, val_) to_d_unit in let (_, time) = convert_time (from_t_unit, 1.) to_t_unit in (to_unit, distance /. time) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_u, time_u) = (speed_unit) in let (_, time_val) = convert_time time time_u in let distance = speed_val *. time_val in (dist_u, distance) ;; let rec passes_da_vinci t = let rec sum_of_sq (t : tree list) (acc : float) = match t with | [] -> acc | h::tl -> match h with | Leaf -> sum_of_sq tl acc | Branch (diam, _) -> sum_of_sq tl (acc +. (diam *. diam)) in let rec traverse subtree = match subtree with | [] -> true | h::tl -> ((passes_da_vinci h) && (traverse tl)) in match t with | Branch (d, list) -> (if (d *. d) >= sum_of_sq list 0. then traverse list else false) | 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 > max_num then cur_el else max_el | hd :: tl -> if hd = cur_el then aux tl (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux tl (hd, 1) (cur_el, cur_num) else aux tl (hd, 1) (max_el, max_num) in match List.sort compare l with | [] -> failwith "Undefined input" | hd :: tl -> aux tl (hd, 1) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> failwith "Undefined input" | _ -> let l1 = List.tl l in let l2 = List.rev (List.tl (List.rev l)) in let result = List.combine l2 l1 in mode result ;; |
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.) | (Second, Second) | (Hour, Hour) -> (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) | (Foot, Mile) -> (to_unit, val_ /. 5280.) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | (Meter, Mile) -> (to_unit, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (to_unit, (val_ *. 5280.) *. 0.3048) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Foot, Foot) | (Meter, Meter) | (Mile, Mile) -> (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let x = convert_dist ((fst from_unit), (val_)) (fst to_unit) in let y = convert_time ((snd to_unit), (snd x)) (snd from_unit) in (to_unit, (snd y)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let x = snd (convert_speed (speed_unit, speed_val) (fst speed_unit, fst time)) *. snd time in (fst speed_unit, x) ;; let rec passes_da_vinci t = notimplemented () ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l, (cur_el, cur_num), (max_el,max_num) with | [],_,_ -> if cur_num > max_num then cur_el else max_el | hd :: tl, (_,0), (_,0) -> aux tl (hd,1) (hd,1) | hd :: tl, (c,cn), (m,mn) when hd = m && hd = c -> aux tl (c,cn+1) (m,mn+1) | hd :: tl, (c,cn), (m,mn) when hd = c -> if cn + 1 > mn then aux tl (c,cn+1) (c,cn+1) else aux tl (c,cn+1) (m,mn) | hd :: tl, (_,_), (m,mn) -> aux tl (hd,1) (m,mn) in let list = List.sort compare (l) in match list with | [] -> failwith "Empty" | hd :: tl -> aux list (hd, 0) (hd, 0) ;; let remove_hd (l: 'a list) : 'a list = match l with | [] -> failwith "Empty" | hd :: tl -> tl let remove_tl (l: 'a list) : 'a list = let revlist = List.rev l in match revlist with | [] -> failwith "Empty" | hd :: tl -> List.rev tl;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "Not long enough" else let list1 = remove_tl l in let list2 = remove_hd l in mode (List.combine list1 list2) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let from_to = (from_unit, to_unit) in match from_to with | (Hour, Hour) -> (Hour, val_) | (Hour, Second) -> let to_sec = 3600. *. val_ in (Second, to_sec) | (Second, Second) -> (Second, val_) | (Second, Hour) -> let to_hour = val_ /. 3600. in (Hour, to_hour) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let from_to = (from_unit, to_unit) in match from_to with | (Foot, Foot) -> (Foot, val_) | (Foot, Meter) -> let to_meter = val_ *. 0.3048 in (Meter, to_meter) | (Foot, Mile) -> let to_mile = val_ /. 5280. in (Mile, to_mile) | (Meter, Meter) -> (Meter, val_) | (Meter, Foot) -> let to_foot = val_ /. 0.3048 in (Foot, to_foot) | (Meter, Mile) -> let to_mile = val_ /. 1609.344 in (Mile, to_mile) | (Mile, Mile) -> (Mile, val_) | (Mile, Foot) -> let to_foot = val_ *. 5280. in (Foot, to_foot) | (Mile, Meter) -> let to_meter = val_ *. 1609.344 in (Meter, to_meter) ;; |
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 let value = snd dist /. snd time in ((fst to_unit, snd to_unit), value) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let conv_speed = convert_speed(a) b_unit in let final = snd conv_speed +. b_val in (b_unit, final) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, subtree) :: tl -> sum_ tl (acc +. width ** 2.) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if ((check subtree) && (sum_ subtree 0. <= width ** 2.)) then check tl else false in check [t] ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.