text
stringlengths 0
601k
|
---|
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) -> (Second, val_ *. 3600.0) | (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_) | (Meter, Meter) -> (Meter, val_) | (Mile, Mile) -> (Mile, val_) | (Foot, Mile) -> (to_unit, val_ /. 5280.0) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Foot, Meter) -> (Meter, val_ *. 0.3048) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | (Meter, Mile) -> (to_unit, val_ /. 0.3048 /. 5280.0) | (Mile, Meter) -> (to_unit, val_ *. 5280. *. 0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = notimplemented () ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = notimplemented () ;; let rec passes_da_vinci t = let rec sum_square_tree s = match s with | Leaf -> 0. | Branch ( nb , branches ) -> (nb *. nb) +. List.fold_left (fun x y -> (x *.x) +. (y *. y)) 0. ( List.map (sum_square_tree) branches) in let rec sum_square_trees trees = List.fold_left (fun x y -> (x *.x) +. (y *. y)) 0. ( List.map (sum_square_tree) trees) in match t with | Leaf -> true | Branch(nb, branches) -> ((nb *. nb >= (sum_square_trees branches)) && (List.fold_left ( &&) true (List.map passes_da_vinci branches))) ;; |
let mode (l: 'a list) : 'a = let rec aux index l (curr_el,curr_num) (max_el, max_num) = if index + 1 < List.length l then if compare (List.nth l index) (curr_el) == 0 then if ((curr_num + 1 > max_num) ) then aux (index+1) l (curr_el, curr_num +1) (curr_el, curr_num +1) else aux (index+1) l (curr_el, curr_num +1) (max_el, max_num) else aux (index+1) l (List.nth l index, 1) (max_el, max_num) else if index < List.length l && compare (List.nth l index) (curr_el) == 0 && ((curr_num + 1 > max_num) ) then curr_el else max_el in let sortedList = List.sort compare l in if ((List.length sortedList) < 1) then failwith "List too small" else aux 1 sortedList (List.nth sortedList 0, 1) (List.nth sortedList 0,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec makePairs oldList newList = match oldList with | [] -> newList | h :: t -> if List.length t > 0 then makePairs t (List.append newList [[h; List.nth t 0]]) else makePairs t (List.append newList []) in let ans = if (List.length l < 2) then failwith "List too small" else mode (makePairs l []) in (List.nth ans 0, List.nth ans 1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with Hour -> (match to_unit with Hour -> (Hour, val_) | Second -> (Second, val_ *. 3600.)) | Second -> (match to_unit with Hour -> (Hour, val_ /. 3600.) | Second -> (Second, val_ )) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with Foot -> (match to_unit with Mile -> (Mile, val_ /. 5280.) | Meter -> (Meter, val_ *. 0.3048) | Foot -> (Foot, val_) ) | Meter -> (match to_unit with Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) ) | Mile -> (match to_unit with Mile -> (Mile, val_) | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_distance, from_time) = from_unit in let (to_distance,to_time) = to_unit in let (new_time, new_time_val) = convert_time(to_time, val_ ) from_time in let (new_dist, new_dist_val) = convert_dist(from_distance, new_time_val) to_distance in ((new_dist, new_time), new_dist_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_speed_unit, a_val) = a in let (new_unit, new_a_val) = convert_speed(a_speed_unit, a_val) b_unit in new_unit, new_a_val +. b_val ;; |
let passes_da_vinci t :bool = let rec gucciBranch t branchNumber = match t with | Leaf -> true | Branch (weight, branches) -> if List.length branches == 0 then true else let rec sumOfBranchWeights (list : tree list) : float = match list with | [] -> 0.0 | h :: t -> match h with | Leaf -> 0. +. sumOfBranchWeights t | Branch (branchWeight, branchBranches) -> (branchWeight *. branchWeight ) +. sumOfBranchWeights t in if (weight *. weight) >= sumOfBranchWeights(branches) then if gucciBranch (List.nth branches branchNumber) 0 then if branchNumber + 1 < List.length branches then gucciBranch t (branchNumber + 1) else true else false else false in gucciBranch t 0 ;; |
let mode (l: 'a list) : 'a = let rec aux index l (curr_el,curr_num) (max_el, max_num) = if index + 1 < List.length l then if compare (List.nth l index) (curr_el) == 0 then if ((curr_num + 1 > max_num) ) then aux (index+1) l (curr_el, curr_num +1) (curr_el, curr_num +1) else aux (index+1) l (curr_el, curr_num +1) (max_el, max_num) else aux (index+1) l (List.nth l index, 1) (max_el, max_num) else if index < List.length l && compare (List.nth l index) (curr_el) == 0 && ((curr_num + 1 > max_num) ) then curr_el else max_el in let sortedList = List.sort compare l in if ((List.length sortedList) < 1) then failwith "List too small" else aux 1 sortedList (List.nth sortedList 0, 1) (List.nth sortedList 0,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec makePairs oldList newList = match oldList with | [] -> newList | h :: t -> if List.length t > 0 then makePairs t (List.append newList [[h; List.nth t 0]]) else makePairs t (List.append newList []) in let ans = if (List.length l < 2) then failwith "List too small" else mode (makePairs l []) in (List.nth ans 0, List.nth ans 1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with Hour -> (match to_unit with Hour -> (Hour, val_) | Second -> (Second, val_ *. 3600.)) | Second -> (match to_unit with Hour -> (Hour, val_ /. 3600.) | Second -> (Second, val_ )) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with Foot -> (match to_unit with Mile -> (Mile, val_ /. 5280.) | Meter -> (Meter, val_ *. 0.3048) | Foot -> (Foot, val_) ) | Meter -> (match to_unit with Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) ) | Mile -> (match to_unit with Mile -> (Mile, val_) | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_distance, from_time) = from_unit in let (to_distance,to_time) = to_unit in let (new_time, new_time_val) = convert_time(to_time, val_ ) from_time in let (new_dist, new_dist_val) = convert_dist(from_distance, new_time_val) to_distance in ((new_dist, new_time), new_dist_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_speed_unit, a_val) = a in let (new_unit, new_a_val) = convert_speed(a_speed_unit, a_val) b_unit in new_unit, new_a_val +. b_val ;; |
let passes_da_vinci t :bool = let rec gucciBranch t branchNumber = match t with | Leaf -> true | Branch (weight, branches) -> if List.length branches == 0 then true else let rec sumOfBranchWeights (list : tree list) : float = match list with | [] -> 0.0 | h :: t -> match h with | Leaf -> 0. +. sumOfBranchWeights t | Branch (branchWeight, branchBranches) -> (branchWeight *. branchWeight ) +. sumOfBranchWeights t in if (weight *. weight) >= sumOfBranchWeights(branches) then if gucciBranch (List.nth branches branchNumber) 0 then if branchNumber + 1 < List.length branches then gucciBranch t (branchNumber + 1) else true else false else false in gucciBranch t 0 ;; |
let mode (l: 'a list) : 'a = let rec aux index l (curr_el,curr_num) (max_el, max_num) = if index + 1 < List.length l then if compare (List.nth l index) (curr_el) == 0 then if ((curr_num + 1 > max_num) ) then aux (index+1) l (curr_el, curr_num +1) (curr_el, curr_num +1) else aux (index+1) l (curr_el, curr_num +1) (max_el, max_num) else aux (index+1) l (List.nth l index, 1) (max_el, max_num) else if index < List.length l && compare (List.nth l index) (curr_el) == 0 && ((curr_num + 1 > max_num) ) then curr_el else max_el in let sortedList = List.sort compare l in if ((List.length sortedList) < 1) then failwith "List too small" else aux 1 sortedList (List.nth sortedList 0, 1) (List.nth sortedList 0,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec makePairs oldList newList = match oldList with | [] -> newList | h :: t -> if List.length t > 0 then makePairs t (List.append newList [[h; List.nth t 0]]) else makePairs t (List.append newList []) in let ans = if (List.length l < 2) then failwith "List too small" else mode (makePairs l []) in (List.nth ans 0, List.nth ans 1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with Hour -> (match to_unit with Hour -> (Hour, val_) | Second -> (Second, val_ *. 3600.)) | Second -> (match to_unit with Hour -> (Hour, val_ /. 3600.) | Second -> (Second, val_ )) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with Foot -> (match to_unit with Mile -> (Mile, val_ /. 5280.) | Meter -> (Meter, val_ *. 0.3048) | Foot -> (Foot, val_) ) | Meter -> (match to_unit with Mile -> (Mile, val_ /. 0.3048 /. 5280.) | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) ) | Mile -> (match to_unit with Mile -> (Mile, val_) | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 5280. *. 0.3048) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (from_distance, from_time) = from_unit in let (to_distance,to_time) = to_unit in let (new_time, new_time_val) = convert_time(to_time, val_ ) from_time in let (new_dist, new_dist_val) = convert_dist(from_distance, new_time_val) to_distance in ((new_dist, new_time), new_dist_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (a_speed_unit, a_val) = a in let (new_unit, new_a_val) = convert_speed(a_speed_unit, a_val) b_unit in new_unit, new_a_val +. b_val ;; |
let passes_da_vinci t :bool = let rec gucciBranch t branchNumber = match t with | Leaf -> true | Branch (weight, branches) -> if List.length branches == 0 then true else let rec sumOfBranchWeights (list : tree list) : float = match list with | [] -> 0.0 | h :: t -> match h with | Leaf -> 0. +. sumOfBranchWeights t | Branch (branchWeight, branchBranches) -> (branchWeight *. branchWeight ) +. sumOfBranchWeights t in if (weight *. weight) >= sumOfBranchWeights(branches) then if gucciBranch (List.nth branches branchNumber) 0 then if branchNumber + 1 < List.length branches then gucciBranch t (branchNumber + 1) else true else false else false in gucciBranch t 0 ;; |
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 && cur_num + 1 > max_num) then aux tl (hd, cur_num +1) (cur_el, cur_num +1) else if hd = cur_el then aux tl (hd, cur_num +1) (max_el, max_num) else if max_num = 0 then aux tl (hd, 1) (hd, 1) else aux tl (hd, 1) (max_el, max_num) in match l with | [] -> failwith "The input list must not be empty" | hd :: tl -> aux (List.sort compare l) (hd, 0) (hd, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pair ((l, el): 'a list * 'a) (acc: 'b list) : 'b list = match l with | [] -> acc | el1 :: tl -> pair (tl, el1) ((el,el1) :: acc) in match l with | [] -> failwith "The input list must not be empty" | el :: [] -> failwith "The input list must contain more than 1 element" | el1 :: el2 :: tl -> mode (pair (tl,el2) [(el1, el2)]) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match to_unit with | Second -> if from_unit = Second then (Second, val_) else (Second, val_ *. 3600.) | Hour -> if from_unit = Hour then (Hour, val_) else (Hour, val_ /. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match to_unit with | Foot -> if from_unit = Foot then (Foot, val_) else if from_unit = Meter then (Foot, val_ /. 0.3048) else (Foot, val_ *. 5280.) | Meter -> if from_unit = Meter then (Meter, val_) else if from_unit = Foot then (Meter, val_ *. 0.3048) else (Meter, val_ *. 5280. *. 0.3048) | Mile -> if from_unit = Mile then (Mile, val_) else if from_unit = Foot then (Mile, val_ /. 5280.) else (Mile, val_ /. 0.3048 /. 5280.) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist, time) = to_unit in (to_unit, snd(convert_dist(fst(from_unit), val_) dist) /. snd(convert_time(snd(from_unit), 1.) time)) ;; |
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 | [] -> 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 "List too small." | hd :: tl -> aux tl (hd, 1) (hd, -1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List too small." else begin let l1 = List.tl l in let l2 = List.rev (List.tl (List.rev l)) in let l3 = List.combine l2 l1 in mode l3 end ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with | Second -> (match to_unit with | Second -> (to_unit, val_) | Hour -> (to_unit, val_ /. 3600.) ) | Hour -> (match to_unit with | Second -> (to_unit, val_ *. 3600.) | Hour -> (to_unit, 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 -> (to_unit, val_) | Meter -> (to_unit, val_ *. 0.3048) | Mile -> (to_unit, val_ /. 5280.) ) | Meter -> (match to_unit with | Foot -> (to_unit, val_ /. 0.3048) | Meter -> (to_unit, val_) | Mile -> (to_unit, val_ /. 0.3048 /. 5280.) ) | Mile -> (match to_unit with | Foot -> (to_unit, val_ *. 5280.) | Meter -> (to_unit, val_ *. 5280. *. 0.3048) | Mile -> (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 (_, new_dist_val) = convert_dist (from_dist, 1.) (to_dist) in let (_, new_time_val) = convert_time (from_time, 1.) (to_time) in ((to_dist, to_time), val_ *. new_dist_val /. new_time_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let (from_speed, from_val) = a in let (_, new_val) = convert_speed (from_speed, from_val) (b_unit) in (b_unit, b_val +. new_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (width, _) :: tl -> sum_ tl (width *. width +. acc) 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 domain () = failwith "REMINDER: You should not be writing tests for undefined values.";; |
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 if (cur_num + 1 > max_num) then aux t (h, cur_num + 1) (h, cur_num + 1) else aux t (h, cur_num + 1) (max_el, max_num) else aux t (h, 1) (max_el, max_num) in match (List.sort compare l) with | [] -> domain () | h :: t -> aux (h :: t) (h, 0) (h, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec build_pair_list l (pair_acc: ('a * 'a) list) = match l with | [] -> domain () | h1 :: t1 -> match t1 with | [] -> pair_acc | h2 :: t2 -> build_pair_list t1 (pair_acc @ [(h1, h2)]) in mode (List.sort compare (build_pair_list 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.0) | Hour, Second -> (Second, 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 -> (Meter, val_ *. 0.3048) | Foot, Mile -> (Mile, val_ /. 5280.0) | Meter, Foot -> (Foot, val_ /. 0.3048) | Meter, Mile -> (Mile, (val_ /. 0.3048) /.5280.0) | Mile, Foot -> (Foot, val_ *. 5280.0) | Mile, Meter -> (Meter, (val_ *. 5280.0) *. 0.3048) | _, _ -> (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 (dist_unit, dist_val), (time_unit, time_val) = (convert_dist (from_dist, 1.0) to_dist), (convert_time (from_time, 1.0) to_time) in (to_unit, (val_ *. dist_val) /. time_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (dist_unit, time_unit) = speed_unit in let (_, time_val) = convert_time time time_unit in (dist_unit , speed_val *. time_val) ;; let rec sum_squares ( t_list : tree list) acc = match t_list with | [] -> acc | tree1 :: rest -> match tree1 with | Leaf -> sum_squares rest acc | Branch (width, _) -> sum_squares rest (acc +. (width *. width)) let rec passes_da_vinci t = let rec check_childreen (tree_list: tree list) (acc: bool) = match tree_list with | [] -> acc | head :: tail -> check_childreen tail (acc && (passes_da_vinci head)) in match t with | Leaf | Branch (_, []) -> true | Branch (width, childreen) -> if width *. width < sum_squares childreen 0. then false else check_childreen childreen true ;; |
let cur_num=1+cur_num |_ -> failwith"invalid input." in List.sort compare (l); aux l (0,List.nth(l) 0)(0, List.nth(l) 0); ;; let rec modehelper(l: 'a list)(n: int)(count: int)(final:int)(finala:'a): 'a= if n=(List.length(l) -1) then finala else if (List.nth(l) n= List.nth(l) n-1) then if count>final then (finala -> (List.nth(l) n); final -> count) else modehelper((l)(n+1)(count+1)(final)(finala)) else modehelper(l)(n+1)(1)(final)(finala) ;; *);; |
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit=Hour then (to_unit,(val_/.3600.0)) else (to_unit,val_) |Hour-> if to_unit=Second 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 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.0) |Meter-> if to_unit=Meter then (to_unit,val_) else if to_unit=Foot then (to_unit,val_/.0.3048) else (to_unit,val_/.(5280.0*.0.3048)) |Mile-> if to_unit=Mile then (to_unit,val_) else if to_unit=Meter then (to_unit,val_*.0.3048*.5280.0) else (to_unit,val_*.5280.0) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = match from_unit with |(Foot,Second)-> if to_unit=(Foot,Hour) then (to_unit,val_*.3600.0) else if to_unit=(Foot,Second) then (to_unit,val_) else if to_unit = (Meter, Second) then (to_unit,val_*.0.3048) else if to_unit=(Meter,Hour) then (to_unit,val_*.0.3048*.3600.0) else if to_unit=(Mile,Second) then (to_unit,val_/.5280.0) else (to_unit,val_*.3600.0/.5280.0) |(Foot,Hour)-> if to_unit=(Foot,Second) then (to_unit,val_/.3600.0) else if to_unit=(Foot,Hour) then (to_unit,val_) else if to_unit = (Meter, Second) then (to_unit,val_*.0.3048/.3600.0) else if to_unit=(Meter,Hour) then (to_unit,val_*.0.3048) else if to_unit=(Mile,Second) then (to_unit,val_/.(5280.0*.3600.0)) else (to_unit,val_/.5280.0) |(Meter,Second)-> if to_unit=(Meter,Hour) then (to_unit,val_*.3600.0) else if to_unit=(Meter,Second) then (to_unit,val_) else if to_unit = (Foot, Second) then (to_unit,val_/.0.3048) else if to_unit=(Foot,Hour) then (to_unit,val_*.3600.0/.0.3048) else if to_unit=(Mile,Second) then (to_unit,val_/.(0.3048*.5280.0)) else (to_unit,val_*.3600.0/.(0.3048*.5280.0)) |(Meter,Hour)-> if to_unit=(Meter,Second) then (to_unit,val_/.3600.0) else if to_unit=(Meter,Hour) then (to_unit,val_) else if to_unit = (Foot, Second) then (to_unit,val_/.(0.3048*.3600.0)) else if to_unit=(Foot,Hour) then (to_unit,val_/.0.3048) else if to_unit=(Mile,Second) then (to_unit,val_/.(0.3048*.3600.0*.5280.0)) else (to_unit,val_/.(0.3048*.5280.0)) |(Mile,Second)-> if to_unit=(Mile,Hour) then (to_unit,val_*.3600.0) else if to_unit=(Mile,Second) then (to_unit,val_) else if to_unit = (Foot, Second) then (to_unit,val_*.5280.0) else if to_unit=(Foot,Hour) then (to_unit,val_*.3600.0*.5280.0) else if to_unit=(Meter,Second) then (to_unit,val_*.0.3048*.5280.0) else (to_unit,val_*.0.3048*.5280.0*.3600.0) |(Mile,Hour)-> if to_unit=(Mile,Second) then (to_unit,val_/.3600.0) else if to_unit=(Mile,Hour) then (to_unit,val_) else if to_unit = (Foot, Second) then (to_unit,val_*.5280.0/.3600.0) else if to_unit=(Foot,Hour) then (to_unit,val_*.5280.0) else if to_unit=(Meter,Second) then (to_unit,val_*.0.3048*.5280.0/.3600.0) else (to_unit,val_*.0.3048*.5280.0) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (t,val1)=time in let (d,val2)=speed_unit in let g=convert_time (t,val1) (val2) in let (a,b)=g in let (val2_)=speed_val in (d, b*.speed_val) ;; 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 with |[]-> if cur_num<max_num then max_el else cur_el |[h::t]-> if cur_el=h then let cur_num=1+cur_num else if(cur_num>max_num) then let max_num=cur_num else |_->failwith"invalid input." in List.sort compare (l); aux l (0,List.nth(l) 0)(0, List.nth(l) 0); ;; let rec modehelper(l: 'a list)(n: int)(count: int)(final:int)(finala:'a): 'a= if n=(List.length(l) -1) then finala else if (List.nth(l) n= List.nth(l) n-1) then if count>final then (finala -> (List.nth(l) n); final -> count) else modehelper((l)(n+1)(count+1)(final)(finala)) else modehelper(l)(n+1)(1)(final)(finala) ;; |
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = notimplemented () ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = notimplemented () ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = notimplemented () ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = notimplemented () ;; 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) : 'a = match l with |[] -> if cur_num > max_num then cur_el else max_el |x::xs -> if cur_el = x then aux xs (x, cur_num+1) (max_el,max_num) else if cur_num>max_num then aux xs (x, 1) (cur_el, cur_num) else aux xs (x, 1) (max_el, max_num) in match (List.sort compare (l)) with |[] -> failwith "error message" |x::xs -> aux xs (x, 1) (x,1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let h_list = List.rev (List.tl (List.rev l)) in let c = List.combine h_list (List.tl l) in mode c ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = to_unit then from_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 = if from_unit = to_unit then from_unit, val_ else match from_unit with |Foot ->(match to_unit with |Mile -> (Mile, val_ /. 5280.) |Meter-> (Meter, val_ *. 0.3048) |Foot -> (Foot, val_)) |Mile -> (match to_unit with |Meter -> (Meter, val_*. 1609.344) |Foot -> (Foot, val_ *. 5280.) |Mile -> (Mile, val_)) |Meter->(match to_unit with |Mile -> (Mile, val_ /. 1609.344) |Foot -> (Foot, val_ /. 0.3048) |Meter -> (Meter, 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 let (_, inv_) = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, d/.inv_) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let final_a = convert_speed a b_unit in (b_unit, snd final_a +. b_val) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Branch (width, _) :: tl -> sum_ tl (acc +. width *. width) | Leaf :: tl -> sum_ tl acc in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, trees) :: tl -> if sum_ trees 0. <= width *. width && check trees 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 (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_sorted = List.sort compare l in match l_sorted with | x::xs -> aux xs (x, 1) (x, 0) | [] -> failwith "The list is empty!" ;; |
let pair_mode (l: 'a list) : 'a * 'a = if (l = []) then failwith "The list is empty!" else let l1 = l in let l2 = l in let l1 = List.rev ( List.tl ( List.rev ( l1 ))) in let l2 = List.tl l2 in let combined_list = List.combine l1 l2 in mode combined_list ;; |
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 -> (Second, val_) | _ -> failwith "Not a valid input unit!" ) | Hour -> (match to_unit with | Second -> (Second, val_ *. 3600.) | Hour -> (Hour, val_) | _ -> failwith "Not a valid input unit!" ) ;; |
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.) | _ -> failwith "Not a valid input unit!" ) | Meter -> (match to_unit with | Foot -> (Foot, val_ /. 0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_ /. 1609.344) | _ -> failwith "Not a valid input unit!" ) | Mile -> (match to_unit with | Foot -> (Foot, val_ *. 5280.) | Meter -> (Meter, val_ *. 1609.344) | Mile -> (Mile, val_) | _ -> failwith "Not a valid input unit!" ) ;; let first_element (pair : 'a * 'b) : 'a = let (first, _) = pair in first let second_element ((a, b) : 'a * 'b) : 'b = b;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let converted_dist = convert_dist ((first_element from_unit), val_) (first_element to_unit) in let time_multiplier = convert_time ((second_element from_unit), 1.0) (second_element to_unit) in ((to_unit), (second_element converted_dist) /. (second_element time_multiplier)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let converted_time = convert_time time (second_element speed_unit) in ((first_element speed_unit), ((second_element converted_time) *. speed_val)) ;; let rec sum_of_squares (l: tree list) (acc: float) = match l with | [] -> acc | Leaf :: tl -> sum_of_squares tl acc | Branch (width, subtree) :: tl -> sum_of_squares tl (acc +. (width *. width)) ;; let rec passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> if (not((check subtree) && (sum_of_squares subtree 0. <= (width *. width)))) then false else check tl in check [t] ;; |
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 | [] -> if cur_num > max_num then cur_el else max_el | hd :: tl when hd = cur_el -> aux tl (cur_el, cur_num + 1) (max_el, max_num) | hd :: tl when cur_num > max_num -> aux tl (hd, 1) (cur_el, cur_num) | hd :: tl -> 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 "Undefined input." | _ :: tl when tl = [] -> failwith "Undefined input." | _ -> let b_list = List.tl l in let a_list = List.rev (List.tl (List.rev l)) in let pairs = List.combine a_list b_list in mode pairs ;; |
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.0) else 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 from_unit = Foot then match to_unit with | Foot -> (Foot, val_) | Meter -> (Meter, val_ *. 0.3048) | Mile -> (Mile, val_/.5280.0) else if from_unit = Meter then match to_unit with | Foot -> (Foot, val_/.0.3048) | Meter -> (Meter, val_) | Mile -> (Mile, val_/. (5280.0 *. 0.3048)) else 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 (d1, t1) = from_unit in let (d2, t2) = to_unit in let (_, d_val) = convert_dist (d1, val_) d2 in let (_, s_val) = convert_time (t2, d_val) t1 in ((d2, t2), s_val) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (d, t) = speed_unit in let (_, t_val) = convert_time time t in (d, speed_val *. t_val) ;; let rec subtree_sum t acc = match t with | [] -> acc | Leaf :: tl -> subtree_sum tl acc | Branch (width, _) :: tl -> subtree_sum tl (acc +. width *. width) let rec passes_da_vinci t = match t with | Leaf -> true | Branch (width, subtree) -> if width *. width >= subtree_sum subtree 0.0 then List.for_all passes_da_vinci subtree else false ;; |
let mode (l: 'a list) : 'a = if l = [] then failwith "Input list cannot be 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 mod_list = List.sort compare l in aux mod_list (List.hd mod_list, 0) (List.hd mod_list, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l = [] || List.length l = 1 then failwith "Invalid input. List must have at least two elements." else let rec pair_adder (main_list:'a list) (pair_list: ('a * 'a) list) (index: int): ('a * 'a) list = if index = (List.length main_list) - 1 then (pair_list @ [(List.nth main_list (index - 1), List.nth main_list index)]) else pair_adder main_list (pair_list @ [(List.nth main_list (index - 1), List.nth main_list index)]) (index + 1) in mode (pair_adder l [] 1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | _ -> (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) -> (Meter, val_ *. 0.3048) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Mile, Foot) -> (Foot, val_ *. 5280. ) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (Meter, (val_ *. 5280.) *. 0.3048) | _ -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (from_unit, val_) else let dist_val = convert_dist (fst from_unit, val_) (fst to_unit) and time_val = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, snd dist_val /. snd time_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let first_speed = convert_speed a b_unit in (b_unit, snd first_speed +. b_val) ;; |
let passes_da_vinci t = let rec sum_ (t_list: tree list) (acc: float) : float = match t_list with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch(width, _) :: tl -> sum_ tl (acc +. (width ** 2.)) in let rec check (l: tree list) : bool = match l with | [] -> true | [Leaf] -> 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 mode (l: 'a list) : 'a = if l = [] then failwith "Input list cannot be 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 mod_list = List.sort compare l in aux mod_list (List.hd mod_list, 0) (List.hd mod_list, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l = [] || List.length l = 1 then failwith "Invalid input. List must have at least two elements." else let rec pair_adder (main_list:'a list) (pair_list: ('a * 'a) list) (index: int): ('a * 'a) list = if index = (List.length main_list) - 1 then (pair_list @ [(List.nth main_list (index - 1), List.nth main_list index)]) else pair_adder main_list (pair_list @ [(List.nth main_list (index - 1), List.nth main_list index)]) (index + 1) in mode (pair_adder l [] 1) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match (from_unit, to_unit) with | (Second, Hour) -> (Hour, val_ /. 3600.) | (Hour, Second) -> (Second, val_ *. 3600.) | _ -> (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) -> (Meter, val_ *. 0.3048) | (Meter, Foot) -> (Foot, val_ /. 0.3048) | (Foot, Mile) -> (Mile, val_ /. 5280.) | (Mile, Foot) -> (Foot, val_ *. 5280. ) | (Meter, Mile) -> (Mile, (val_ /. 0.3048) /. 5280.) | (Mile, Meter) -> (Meter, (val_ *. 5280.) *. 0.3048) | _ -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = to_unit then (from_unit, val_) else let dist_val = convert_dist (fst from_unit, val_) (fst to_unit) and time_val = convert_time (snd from_unit, 1.) (snd to_unit) in (to_unit, snd dist_val /. snd time_val) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let first_speed = convert_speed a b_unit in (b_unit, snd first_speed +. b_val) ;; |
let passes_da_vinci t = let rec sum_ (t_list: tree list) (acc: float) : float = match t_list with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch(width, _) :: tl -> sum_ tl (acc +. (width ** 2.)) in let rec check (l: tree list) : bool = 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 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 -> let cur_num = if hd = cur_el then cur_num + 1 else 1 in let cur_el = if hd = cur_el then cur_el else hd in let max_el = if cur_num > max_num then cur_el else max_el in let max_num = if cur_num > max_num then cur_num else max_num in aux tl (cur_el, cur_num) (max_el, max_num) in match l with | [] -> failwith "Invalid input." | _ -> let l = List.sort compare l in let e = List.nth l 0 in aux l (e, 0) (e, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Invalid input." | [_] -> failwith "Invalid input." | _ -> let (_ :: nohead1) = l in let l1 = nohead1 in let (_ :: nohead2) = List.rev l in let l2 = List.rev nohead2 in let l3 = List.combine l2 l1 in mode l3 ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit, to_unit with | Hour, Hour -> Hour, val_ | Hour, Second -> Second, (val_ *. 3600.) | Second, Second -> Second, val_ | Second, Hour -> Hour, (val_ /. 3600.) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit, to_unit with | Foot, Foot -> Foot, val_ | Foot, Meter -> Meter, (val_ *. 0.3048) | Foot, Mile -> Mile, (val_ /. 5280.) | Meter, Foot -> Meter, (val_ /. 0.3048) | Meter, Meter -> Meter, val_ | Meter, Mile -> Mile, (val_ /. (0.3048 *. 5280.)) | Mile, Foot -> Foot, (val_ *. 5280.) | Mile, Meter -> Meter, (val_ *. (0.3048 *. 5280.)) | Mile, Mile -> Mile, val_ ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let old_dist = fst(from_unit) in let new_dist = fst(to_unit) in let old_time = snd(from_unit) in let new_time = snd(to_unit) in let dist_conv = snd(convert_dist(old_dist, val_) new_dist) in let time_conv = snd(convert_time(new_time, dist_conv) old_time) in (new_dist, new_time), time_conv ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let speed_conv = snd(convert_speed a b_unit) in (b_unit, speed_conv +. 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 -> let acc = acc +. (width ** 2.) in sum_ tl acc 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 mode (l: 'a list) : 'a = match l with |[] -> failwith "Input is invalid" |_ -> 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 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 = if (List.length l) < 2 then failwith "Invalid input" else let l1_hd :: l1_tl = l in let l2_hd :: l2_tl = List.rev l in let l2_tl = List.rev l2_tl in let bi_gram_l = List.combine l2_tl l1_tl in mode bi_gram_l ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.