text
stringlengths 0
601k
|
---|
let mode (l: 'a list) : 'a = let m = List.sort compare l in let rec aux m ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match m with | [] -> if cur_num > max_num then cur_el else max_el |x::rest -> if cur_el = x then aux rest (cur_el, cur_num+1) (max_el, max_num) else if cur_num > max_num then aux rest (x, 1) (cur_el, cur_num) else aux rest (x, 1) (max_el, max_num) in match m with | [] -> failwith "none" |first::next -> aux next (first, 1) (first, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l and l2 = List.rev (List.tl (List.rev l)) in let toenter = List.combine l2 l1 in mode toenter ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if not (val_ >= 0.) then failwith ("wrong input") else match from_unit with | Second -> if to_unit = Hour then ( Hour, val_/.3600.) else (from_unit, val_) | Hour -> if to_unit = Second then ( Second, val_*.3600.) else (from_unit, val_) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if not (val_ >= 0.) then failwith ("wrong input") else match from_unit with | Foot -> if to_unit = Meter then ( Meter, val_*.0.3048) else if to_unit = Mile then (Mile, val_/.5280.) else (from_unit, val_) | Meter -> if to_unit = Foot then ( Meter, val_/.0.3048) else if to_unit = Mile then (Mile, val_/. 1609.344) else (from_unit, val_) | Mile -> if to_unit = Foot then ( Meter, val_*.5280.) else if to_unit = Meter then (Mile, val_*. 1609.344) else (from_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if not (val_ >= 0.) then failwith ("wrong input") else let from_unit_dist = fst(from_unit) and from_unit_time = snd(from_unit) and to_unit_dist = fst(to_unit) and to_unit_time = snd(to_unit) in let next_value = snd(convert_dist (from_unit_dist, val_) to_unit_dist) in let number = snd(convert_time (from_unit_time, 1.0) to_unit_time) in (to_unit, next_value/.number) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let converted = convert_speed (fst (a) , snd(a)) b_unit in let added = snd(converted) +. b_val in (b_unit, added) ;; |
let passes_da_vinci t = let rec sum_ l acc = match l with | [] -> acc | Leaf :: tl -> sum_ tl acc | Branch (w, _) :: tl -> sum_ tl (acc +. w *. w) in let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (w, trees) :: tl -> if sum_ trees 0. <= w *. w && 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 | x :: remainder -> if cur_el = x then aux remainder (cur_el, cur_num + 1) (max_el, max_num) else if cur_num > max_num then aux remainder (x, 1) (cur_el, cur_num) else aux remainder (x, 1) (max_el,max_num) | [] -> if cur_num > max_num then cur_el else max_el in match List.sort compare l with | x::remainder -> aux remainder (x, 1) (x, 1) | [] -> failwith "error message" ;; |
let pair_mode (l: 'a list) : 'a * 'a = let rec my_pair (l: 'a list):('a*'a) list = match l with | [] -> [] |[pp1;pp2] -> [(pp1,pp2)] |[_] -> [] |pp1::pp2::tail -> (pp1,pp2)::(my_pair(pp2::tail)) in let l = l in mode (my_pair l) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if to_unit = Hour && from_unit = Second then (from_unit,val_ /. 3600.) else if to_unit = Second && from_unit = Hour then (from_unit,val_ *. 3600.) else from_unit, val_ ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if to_unit = Meter && from_unit = Foot then (from_unit,val_ *. 0.3048) else if to_unit = Foot && from_unit = Meter then (from_unit,val_ /. 0.3048) else if to_unit = Mile && from_unit = Meter then (from_unit,val_ /. 1609.344) else if to_unit = Meter && from_unit = Mile then (from_unit,val_ *. 1609.344) else if to_unit = Foot && from_unit = Mile then (from_unit,val_ *. 5280.) else if to_unit = Mile && from_unit = Foot then (from_unit,val_ /. 5280.) else from_unit, val_ ;; else if from_unit = (Meter, Hour) && to_unit = (Meter, Second) then (from_unit, val_ /. 3600.) else if from_unit = (Meter, Hour) && to_unit = (Foot, Second) then (from_unit, val_ *. 11811.023622) else if from_unit = (Meter, Hour) && to_unit = (Foot, Hour) then (from_unit, val_ /. 0.3048) else if from_unit = (Meter, Hour) && to_unit = (Mile, Second) then (from_unit, val_ /. 1609.344) else if from_unit = (Meter, Hour) && to_unit = (Mile, Hour) then (from_unit, val_ /. 5793638.4) else from_unit, val_ ;;*);; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (_,from_time) = from_unit in let (from_dist,_) = from_unit in let (_,to_time) = to_unit in let (to_dist,_) = to_unit in let (new_time, time) = convert_time (from_time, 1.) to_time in let (new_dist, dist) = convert_dist ((from_dist), val_) (to_dist) in ((new_dist, new_time), dist/.time) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (time_x, time) = time in let (speed_distance, _) = speed_unit in let ((final_dist, _), last_val) = convert_speed (speed_unit, speed_val) (speed_distance, time_x) in (final_dist, last_val *. time) ;; let treeA = Branch (5., [ Branch (3., [Leaf; Leaf; Leaf]); Leaf; Branch (4., [Branch (2., [])]) ]) let treeB = Leaf let treeC = Branch (1., [ Branch (3., [Leaf]) ]) 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) = notimplemented () in notimplemented () ;; |
let pair_mode (l: 'a list) : 'a * 'a = notimplemented () ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if to_unit = Hour && from_unit = Second then (from_unit,val_ /. 3600.) else if to_unit = Second && from_unit = Hour then (from_unit,val_ *. 3600.) else from_unit, val_ ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = if to_unit = Meter && from_unit = Foot then (from_unit,val_ *. 0.3048) else if to_unit = Foot && from_unit = Meter then (from_unit,val_ /. 0.3048) else if to_unit = Mile && from_unit = Meter then (from_unit,val_ /. 1609.344) else if to_unit = Meter && from_unit = Mile then (from_unit,val_ *. 1609.344) else if to_unit = Foot && from_unit = Mile then (from_unit,val_ *. 5280.) else if to_unit = Mile && from_unit = Foot then (from_unit,val_ /. 5280.) else from_unit, val_ ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if from_unit = (Meter, Second) && to_unit = (Meter, Hour) then (from_unit, val_ *. 3600.) else if from_unit = (Meter, Second) && to_unit = (Foot, Second) then (from_unit, val_ /. 0.3048) else if from_unit = (Meter, Second) && to_unit = (Foot, Hour) then (from_unit, val_ /. 0.000084666666666) else if from_unit = (Meter, Second) && to_unit = (Mile, Second) then (from_unit, val_ /. 1609.344) else if from_unit = (Meter, Second) && to_unit = (Mile, Hour) then (from_unit, val_ /. 5793638.4) else if from_unit = (Meter, Hour) && to_unit = (Meter, Second) then (from_unit, val_ /. 3600.) else if from_unit = (Meter, Hour) && to_unit = (Foot, Second) then (from_unit, val_ *. 11811.023622) else if from_unit = (Meter, Hour) && to_unit = (Foot, Hour) then (from_unit, val_ /. 0.3048) else if from_unit = (Meter, Hour) && to_unit = (Mile, Second) then (from_unit, val_ /. 1609.344) else if from_unit = (Meter, Hour) && to_unit = (Mile, Hour) then (from_unit, val_ /. 5793638.4) else from_unit, val_ ;; |
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 sorted_list = List.sort compare l in match l with | [] -> failwith "List too small." | _ -> 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 (if cur_el < max_el then cur_el else max_el) else (if cur_num < max_num then max_el else cur_el) | hd :: tl -> if hd = cur_el then (if max_num < (cur_num + 1) 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 sorted_list (List.hd sorted_list, 0) (List.hd sorted_list, 0) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if List.length l < 2 then failwith "List is too small." else (let x = (List.tl l)@[(List.hd l)] in mode (List.tl(List.rev(List.combine l x)))) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if fst(from_unit, val_) = Second then (if to_unit = Hour then (Hour, (val_ /. 3600.0)) else (Second, val_)) else (if to_unit = Second then (Second, (val_ *. 3600.0)) else (Hour, val_)) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let unit1 = fst(from_unit, val_) in match unit1 with | Foot -> if to_unit = Foot then (Foot, val_) else (if to_unit = Meter then (Meter, (val_ *. 0.3048)) else (Mile, (val_ /. 5280.0))) | Meter -> if to_unit = Meter then (Meter, val_) else (if to_unit = Foot then (Foot, (val_ /. 0.3048)) else (Mile, (val_ /. 1609.344))) | Mile -> if to_unit = Mile then (Mile, val_) else (if to_unit = Meter then (Meter, val_ *. 1609.344) else (Foot, val_ *. 5280.0)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let unit1 = fst(from_unit) in let unit2 = fst(to_unit) in let time1 = snd(from_unit) in let time2 = snd(to_unit) in ((unit2, time2), (snd(convert_dist (unit1, val_) unit2) /. snd(convert_time (time1, 1.0) time2))) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = if snd(speed_unit) = fst(time) then ((fst(speed_unit)), (speed_val *. snd(time))) else ((fst(speed_unit)), (speed_val *. (snd(convert_time ((fst(time)), (snd(time))) (snd(speed_unit)))))) ;; let rec sum l (acc: float) = match l with | [] -> acc | Leaf :: tl -> sum tl acc | Branch (width, subtree) :: tl -> sum tl (acc +. (width *. width)) ;; let rec passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if (not(check subtree && ((width *. width) >= sum subtree 0.0))) 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 cur_num in match tl with |[] -> if cur_num > max_num then cur_el else max_el |hd2::tl2 -> let new_head = hd2 in if cur_num > max_num then let max_el = cur_el in let max_num = cur_num in let cur_num = if new_head <> cur_el then 0 else cur_num in aux tl (new_head, cur_num)(max_el,max_num) else let cur_num = if new_head <> cur_el then 0 else cur_num in aux tl (new_head, cur_num)(max_el,max_num) in let (sorted:'a list) = List.sort compare l in match sorted with |[] -> failwith "[] is an invalid input" |hd1::tl1 -> aux sorted (hd1, 0) (hd1, -10) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match List.length l with |0 -> failwith "invalid input: the length is 0" |1 -> failwith "invalid input: the length is 1" |_ -> let l1 = l in let l2 = l in let rev_l2 = List.rev l2 in match l1 with |[] -> failwith "invalid input" |hd1::tl1 -> let l1_removed = tl1 in match rev_l2 with |[] -> failwith "invalid input" |hd2::tl2 -> let l2_removed = List.rev tl2 in let li_of_tuples = List.combine l2_removed l1_removed in mode li_of_tuples ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let given_time_unit = fst(from_unit, val_) in let given_value = snd(from_unit, val_) in if given_value < 0. then failwith "invalid input" else if given_time_unit = to_unit then (given_time_unit, given_value) else if given_time_unit = Second then let return_time_unit:time_unit = Hour in let return_value = given_value /. 3600. in (return_time_unit, return_value) else let return_time_unit:time_unit = Second in let return_value = given_value *. 3600. in (return_time_unit, return_value) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let given_dist_unit:dist_unit = fst(from_unit, val_) in let given_value = snd(from_unit, val_) in match (given_value < 0.) with | true -> failwith "invalid input" | _ -> if given_dist_unit = to_unit then (given_dist_unit, given_value) else if given_dist_unit = Foot then match to_unit with |Meter -> (Meter, given_value *. 0.3048) |_ -> (Mile, given_value /. 5280.) else if given_dist_unit = Meter then match to_unit with |Foot -> (Foot, given_value /. 0.3048) |_ -> (Mile, given_value /. 0.3048 /. 5280.) else match to_unit with |Foot -> (Foot, given_value *. 5280.) |_ -> (Meter, given_value *. 5280. *. 0.3048) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = if val_ < 0. then failwith "invalid input" else let given_dist_unit = fst(fst(from_unit, val_)) in let given_time_unit = snd(fst(from_unit, val_)) in let to_dist_unit = fst(to_unit) in let to_time_unit = snd(to_unit) in let dist_to_multiply = snd(convert_dist (given_dist_unit, 1.) to_dist_unit) in let time_to_multiply = snd(convert_time (given_time_unit, 1.) to_time_unit) in let updated_value = val_ *. dist_to_multiply /. time_to_multiply in ((to_dist_unit, to_time_unit), updated_value) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let value_a = snd a in let value_b = snd(b_unit, b_val) in if value_a < 0. || value_b < 0. then failwith "invalid input" else let common_speed_unit = fst(b_unit, b_val) in let converted_speed = convert_speed a common_speed_unit in let added_value = snd(b_unit, b_val) +. snd(converted_speed) in (common_speed_unit, added_value) ;; |
let passes_da_vinci t = let rec sum_ l acc= match l with |[] -> acc |Leaf :: t1 -> sum_(t1)(acc +. 0.0) |Branch (width, subtree) :: t1 -> let squared_width = width ** 2. in sum_(t1)(acc+.squared_width) in let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> let squared_branchs_width = width ** 2. in let squared_sum_of_child = sum_ subtree 0. in let sum_check = squared_sum_of_child <= squared_branchs_width in if not(check subtree && sum_check) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = match l with | [] -> failwith "Undefined input" | _ -> let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | x :: xs -> if cur_el = x && cur_num > max_num then aux xs (cur_el, cur_num + 1) (cur_el, cur_num) else if cur_el = x then aux xs (cur_el, cur_num+1) (max_el, max_num) else aux xs (x, 1) (max_el, max_num) in let sortedList = List.sort compare l in match sortedList with | x :: xs -> aux sortedList (x,0) (x,0) | [] -> failwith "Undefined input";; |
let pair_mode (l: 'a list) : 'a * 'a = match l with |[]-> failwith "Undefined input - empty list" |[_]-> failwith "Undefined input - only one element in list" |_ -> let list_first_removed = List.tl l in let list_reversed = List.rev l in let list_first_removed_rev = List.tl list_reversed in let list_last_removed = List.rev list_first_removed_rev in let list_combined_pair = List.combine list_last_removed list_first_removed in mode (list_combined_pair);; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit = Hour then (Hour, val_ /. 3600.) else (Second, val_ ) | Hour -> if to_unit = Second then (Second, val_ *. 3600.) else (Hour, val_);; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with |Meter -> if to_unit = Foot then (Foot, val_ /. 0.3048) else if to_unit = Mile then (Mile, (val_ /. 0.3048) /. 5280. ) else (Meter, val_) |Mile -> if to_unit = Foot then (Foot, val_ *. 5280.) else if to_unit = Meter then (Meter, (val_ *. 5280.) *. 0.3048 ) else (Mile, val_) |Foot -> if to_unit = Mile then (Mile, val_ /. 5280.) else if to_unit = Meter then (Meter, val_ *. 0.3048) else (Foot, val_);; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let distance_initial = fst from_unit in let time_initial = snd from_unit in let distance_final = fst to_unit in let time_final = snd to_unit in let distance_converted = snd (convert_dist (distance_initial, val_) distance_final) in let time_converted = snd (convert_time (time_initial, 1.) time_final) in ((distance_final, time_final), distance_converted /. time_converted);; |
let add_speed ((a_unit, a_val) : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_converted = snd (convert_speed (a_unit, a_val) (b_unit)) in let added_speed = a_converted +. b_val in ((b_unit), added_speed);; |
let passes_da_vinci t = match t with |Leaf -> true |_ -> (passes_da_vinci_two 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 -> let cur_el, cur_num = if hd = cur_el then cur_el, cur_num + 1 else hd, 1 in aux tl (cur_el, cur_num) (if cur_num > max_num then cur_el, cur_num else max_el, max_num) in match List.sort compare l with | [] -> raise (Invalid_argument "list cannot be empty") | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> raise (Invalid_argument "list needs to contain at least two elements") | l -> mode (List.combine (l |> List.rev |> List.tl |> List.rev) (List.tl l)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let scalar = match from_unit, to_unit with | Hour, Second -> 3600. | Second, Hour -> 1. /. 3600. | Hour, Hour | Second, Second -> 1. in to_unit, val_ *. scalar ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let rec scalar from_unit to_unit = match from_unit, to_unit with | Mile, Foot -> 5280. | Foot, Meter -> 0.3048 | Mile, Meter -> (scalar Mile Foot) *. (scalar Foot Meter) | Foot, Mile | Meter, Foot | Meter, Mile -> 1. /. (scalar to_unit from_unit) | Foot, Foot | Meter, Meter | Mile, Mile -> 1. in to_unit, val_ *. (scalar from_unit to_unit) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let _, dist_scalar = convert_dist ((fst from_unit), 1.) (fst to_unit) in let _, time_scalar = convert_time ((snd from_unit), 1.) (snd to_unit) in to_unit, val_ *. dist_scalar /. time_scalar ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let _, time = convert_time time (snd speed_unit) in fst speed_unit, speed_val *. time ;; let rec passes_da_vinci t = let sum_of_squares ts = ts |> List.rev_map (fun t -> match t with | Branch(w, _) -> w | Leaf -> 0.) |> List.rev_map (fun w -> w *. w) |> List.fold_left (+.) 0. in match t with | Leaf -> true | Branch(w, ts) -> if sum_of_squares ts <= w *. w then List.for_all passes_da_vinci ts 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 | [] -> max_el | hd :: tl -> let cur_el, cur_num = if hd = cur_el then cur_el, cur_num + 1 else hd, 1 in aux tl (cur_el, cur_num) (if cur_num > max_num then cur_el, cur_num else max_el, max_num) in match List.sort compare l with | [] -> raise (failwith "list cannot be empty") | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> raise (failwith "list needs to contain at least two elements") | l -> mode (List.combine (l |> List.rev |> List.tl |> List.rev) (List.tl l)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let scalar = match from_unit, to_unit with | Hour, Second -> 3600. | Second, Hour -> 1. /. 3600. | Hour, Hour | Second, Second -> 1. in to_unit, val_ *. scalar ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let rec scalar from_unit to_unit = match from_unit, to_unit with | Mile, Foot -> 5280. | Foot, Meter -> 0.3048 | Mile, Meter -> (scalar Mile Foot) *. (scalar Foot Meter) | Foot, Mile | Meter, Foot | Meter, Mile -> 1. /. (scalar to_unit from_unit) | Foot, Foot | Meter, Meter | Mile, Mile -> 1. in to_unit, val_ *. (scalar from_unit to_unit) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let _, dist_scalar = convert_dist ((fst from_unit), 1.) (fst to_unit) in let _, time_scalar = convert_time ((snd from_unit), 1.) (snd to_unit) in to_unit, val_ *. dist_scalar /. time_scalar ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let _, time = convert_time time (snd speed_unit) in fst speed_unit, speed_val *. time ;; let rec passes_da_vinci t = let sum_of_squares ts = ts |> List.rev_map (fun t -> match t with | Branch(w, _) -> w | Leaf -> 0.) |> List.rev_map (fun w -> w ** 2.) |> List.fold_left (+.) 0. in match t with | Leaf -> true | Branch(w, ts) -> sum_of_squares ts <= w *. w && List.for_all passes_da_vinci ts ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd :: tl -> let cur_el, cur_num = if hd = cur_el then cur_el, cur_num + 1 else hd, 1 in aux tl (cur_el, cur_num) (if cur_num > max_num then cur_el, cur_num else max_el, max_num) in match List.sort compare l with | [] -> raise (failwith "list cannot be empty") | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] | [_] -> raise (failwith "list needs to contain at least two elements") | l -> mode (List.combine (l |> List.rev |> List.tl |> List.rev) (List.tl l)) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let scalar = match from_unit, to_unit with | Hour, Second -> 3600. | Second, Hour -> 1. /. 3600. | Hour, Hour | Second, Second -> 1. in to_unit, val_ *. scalar ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let rec scalar from_unit to_unit = match from_unit, to_unit with | Mile, Foot -> 5280. | Foot, Meter -> 0.3048 | Mile, Meter -> (scalar Mile Foot) *. (scalar Foot Meter) | Foot, Mile | Meter, Foot | Meter, Mile -> 1. /. (scalar to_unit from_unit) | Foot, Foot | Meter, Meter | Mile, Mile -> 1. in to_unit, val_ *. (scalar from_unit to_unit) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let _, dist_scalar = convert_dist ((fst from_unit), 1.) (fst to_unit) in let _, time_scalar = convert_time ((snd from_unit), 1.) (snd to_unit) in to_unit, val_ *. dist_scalar /. time_scalar ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let _, time = convert_time time (snd speed_unit) in fst speed_unit, speed_val *. time ;; let rec passes_da_vinci t = let sum_of_squares ts = ts |> List.rev_map (fun t -> match t with | Branch(w, _) -> w | Leaf -> 0.) |> List.rev_map (fun w -> w ** 2.) |> List.fold_left (+.) 0. in match t with | Leaf -> true | Branch(w, ts) -> sum_of_squares ts <= w *. w && List.for_all passes_da_vinci ts ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | hd :: tl -> let (cur_el, cur_num) = if hd = cur_el then (cur_el, cur_num + 1) else (hd, 1) in aux tl (cur_el, cur_num) (if cur_num > max_num then (cur_el, cur_num) else (max_el, max_num)) in let l = List.sort compare l in match l with | [] -> raise Not_found | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> raise Not_found | [_] -> raise Not_found | _ :: tl -> let zip a b = let rec aux a b acc = match (a, b) with | _, [] -> acc | [], _ -> acc | x :: a, y :: b -> aux a b ((x, y) :: acc) in List.rev (aux a b []) in mode (zip l tl) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let scalar = match from_unit, to_unit with | Hour, Second -> 3600. | Second, Hour -> 1. /. 3600. | _ -> 1. in (to_unit, val_ *. scalar) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let rec scalar from_unit to_unit = match from_unit, to_unit with | Mile, Foot -> 5280. | Foot, Meter -> 0.3048 | Mile, Meter -> (scalar Mile Foot) *. (scalar Foot Meter) | Foot, Mile | Meter, Foot | Meter, Mile -> 1. /. (scalar to_unit from_unit) | _ -> 1. in (to_unit, val_ *. (scalar from_unit to_unit)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = (to_unit, val_ *. (snd (convert_dist ((fst from_unit), 1.) (fst to_unit))) /. (snd (convert_time ((snd from_unit), 1.) (snd to_unit)))) ;; |
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 rec passes_da_vinci t = let sum_of_squares l = l |> List.map (fun t -> match t with | Branch(f, _) -> f *. f | Leaf -> 0.) |> List.fold_left (+.) 0. in match t with | Leaf -> true | Branch(_, []) -> true | Branch(f, l) -> if (sum_of_squares l) <= f *. f then List.fold_left (fun a b -> a && (passes_da_vinci b)) true l 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 | [] -> max_el | hd :: tl -> let (cur_el, cur_num) = if hd = cur_el then (cur_el, cur_num + 1) else (hd, 1) in aux tl (cur_el, cur_num) (if cur_num > max_num then (cur_el, cur_num) else (max_el, max_num)) in let l = List.sort compare l in match l with | [] -> raise Not_found | hd :: tl -> aux tl (hd, 1) (hd, 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> raise Not_found | [_] -> raise Not_found | _ :: tl -> let zip a b = let rec aux a b acc = match (a, b) with | _, [] -> acc | [], _ -> acc | x :: a, y :: b -> aux a b ((x, y) :: acc) in List.rev (aux a b []) in mode (zip l tl) ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = let scalar = match from_unit, to_unit with | Hour, Second -> 3600. | Second, Hour -> 1. /. 3600. | _ -> 1. in (to_unit, val_ *. scalar) ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = let rec scalar from_unit to_unit = match from_unit, to_unit with | Mile, Foot -> 5280. | Foot, Meter -> 0.3048 | Mile, Meter -> (scalar Mile Foot) *. (scalar Foot Meter) | Foot, Mile | Meter, Foot | Meter, Mile -> 1. /. (scalar to_unit from_unit) | _ -> 1. in (to_unit, val_ *. (scalar from_unit to_unit)) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let _, dist_scalar = convert_dist ((fst from_unit), 1.) (fst to_unit) in let _, time_scalar = convert_time ((snd from_unit), 1.) (snd to_unit) in (to_unit, val_ *. dist_scalar /. time_scalar) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let _, time = convert_time time (snd speed_unit) in (fst speed_unit, speed_val *. time) ;; let rec passes_da_vinci t = let sum_of_squares l = l |> List.rev_map (fun t -> match t with | Branch(f, _) -> f *. f | Leaf -> 0.) |> List.fold_left (+.) 0. in match t with | Leaf -> true | Branch(_, []) -> true | Branch(f, l) -> if (sum_of_squares l) <= f *. f then l |> List.rev_map passes_da_vinci |> List.for_all (fun b -> b) 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 | 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 | [] -> notimplemented() | x::xs-> aux xs (x,1) (x,1) ;; let combine l1 l2 = let combined_list = List.rev(List.combine l1 l2) in match combined_list with | [x] -> combined_list | x::xs-> xs | [] -> notimplemented();; |
let pair_mode (l: 'a list) : 'a * 'a = let rec pair n l2 = match (n: int) with | n when n > 0 -> pair (n-1) ((List.nth l n)::l2) | 0 -> combine l l2 | _ -> notimplemented() in let new_list = List.rev((pair ((List.length l)-1) (List.nth l 0 :: []))) in mode(new_list) ;; |
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 |Mile -> (to_unit, (val_/.5280.)) |Meter -> (to_unit, (val_*.0.3048)) |Foot -> (to_unit, val_) ) |Mile -> (match to_unit with |Mile -> (to_unit, val_) |Meter -> (to_unit, ((val_*.5280.)*.0.3048)) |Foot -> (to_unit, (val_*.5280.)) ) |Meter -> (match to_unit with |Meter -> (to_unit, val_) |Mile -> (to_unit, ((val_/.0.3048)/.5280.)) |Foot -> (to_unit, (val_/.0.3048)) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let time_value = convert_time ((snd from_unit), val_) (snd to_unit) in let dist_value = convert_dist ((fst from_unit), val_) (fst to_unit) in (to_unit,(val_ *. (snd dist_value /. snd time_value))) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let time_value = convert_time time (snd speed_unit) in ((fst speed_unit), ((snd time_value) *. speed_val)) ;; let rec passes_da_vinci t = let rec sum_ l acc = match l with |Branch(height, subtree)::tl -> if(passes_da_vinci (Branch(height, subtree)))then sum_ tl (acc-.(height*.height)) else false |Leaf::tl -> sum_ tl acc |[] -> (acc>=0.) in match t with | Branch(height,subtree)->sum_ subtree (height *. height) | 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 if l = [] then failwith "Not a large enough list" else let list = List.sort compare l in aux (List.tl list) ((List.hd list), 1) ((List.hd list), 1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Not a large enough list" | _::tl -> let listA = List.rev (List.tl (List.rev l)) in let listB = tl in let list = List.combine listA listB in mode list ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = if from_unit = Hour then if to_unit = Hour then (Hour, val_) else (Second, val_ *. 3600.) else if to_unit = Second then (Second, val_) else (Hour, val_ /. 3600.) ;; |
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, val_ /. 5280.) else if from_unit = Meter then match to_unit with |Foot -> (Foot, val_ /. 0.3048) |Meter -> (Meter, val_) |_ -> (Mile, (val_ /. 0.3048) /. 5280.) else match to_unit with |Foot -> (Foot, val_ *. 5280.) |Meter -> (Meter, (val_ *. 5280.) *. 0.3048) |_ -> (Mile, val_ ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let val2 = convert_dist (fst from_unit, val_) (fst to_unit) in let final = convert_time ((snd to_unit), snd val2) (snd from_unit) in (to_unit, snd final) ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let v1 = convert_speed ( fst a, snd a) b_unit in (b_unit, (b_val +. (snd v1))) ;; |
let passes_da_vinci t = let rec check l = match l with | [] -> true | Leaf :: tl -> true | Branch (width, subtree) :: tl -> if not ((check subtree) && ((sum_ subtree 0.) <= (width *. width))) then false else check tl in check [t] ;; |
let mode (l: 'a list) : 'a = let rec aux l ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l with | [] -> max_el | a ::( _ as r) -> if a = cur_el then if cur_num+1 > max_num then aux r (cur_el, cur_num+1) (cur_el, cur_num+1) else aux r (cur_el, cur_num+1) (max_el, max_num) else aux r ( a, 1) (max_el, max_num) in match List.sort compare (l) with | [] -> failwith "Undefined input." | [x] -> x | y:: _ -> aux (List.sort compare (l)) (y,0) (y,-1) ;; |
let pair_mode (l: 'a list) : 'a * 'a = match l with | [] -> failwith "Undefined input." | [x] -> failwith "Undefined input." | _ -> 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 = match from_unit with | Second -> ( match to_unit with | Second -> ( to_unit , 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 = 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_ /. 0.3048 /. 5280.0 ) ) | Mile -> ( match to_unit with |Foot -> ( Foot, val_ *. 5280.0) | Meter -> (Meter , val_ *. 5280.0 *. 0.3048 ) | Mile -> (Mile ,val_ ) ) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let (dist1, time1) = from_unit in let (dist2, time2) = to_unit in (to_unit, snd(convert_time ( time2, snd(convert_dist (dist1, val_) dist2)) time1)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let t : time_unit =snd(speed_unit) in let time_converted :time_unit value = convert_time time t in (fst(speed_unit), speed_val *. snd( time_converted ) ) ;; let rec passes_da_vinci t = let rec sum_ l acc : float= match l with | [] -> acc | Leaf ::( _ as r) -> sum_ r acc | Branch (a, _) ::( _ as r) -> sum_ r (acc +. (a *. a)) in let rec check l = match l with | [] -> true |Leaf :: tl -> check tl | Branch (width, subtree) :: tl -> if sum_ subtree 0. > (width *. width) then false else check (subtree ) && check (tl) in check [t] ;; |
let mode (l: 'a list) : 'a = let aux elem = (List.length (List.filter (fun x->x = elem) l), elem) in let pairs = List.sort (fun (a,_) (c,_) -> compare c a) (List.map aux l) in if l == [] then failwith "Empty list." else snd (List.hd (List.sort (fun (_,a) (_,c) -> compare a c) (List.filter (fun (x,_) -> x = fst (List.hd pairs)) pairs))) ;; |
let pair_mode (l: 'a list) : 'a * 'a = if l == [] then failwith "Empty list." else 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 = match (from_unit, to_unit) with | (Second, Hour) -> (to_unit, val_ /. 3600.) | (Hour,Second) -> (to_unit, val_*. 3600.) | (_,_) -> (to_unit, val_) ;; |
let convert_dist ((from_unit, val_):dist_unit value) to_unit : dist_unit value = match (from_unit, to_unit) with | (Foot, Meter) -> (to_unit, val_ *. 0.3048) | (Foot, Mile) -> (to_unit, val_ /. 5280.) | (Meter, Foot) -> (to_unit, val_ /. 0.3048) | (Meter, Mile) -> (to_unit, (val_ /. 0.3048) /. 5280.) | (Mile, Foot) -> (to_unit, val_ *. 5280.) | (Mile, Meter) -> (to_unit, (val_ *. 5280.) *. 0.3048) | (_,_) -> (to_unit, val_) ;; |
let convert_speed ((from_unit, val_) : speed_unit value) to_unit : speed_unit value = let ((a,b),(c,d)) = (from_unit, to_unit) in ((fst (convert_dist (a, val_) c), fst (convert_time (b, val_) d)), val_ *. snd (convert_dist (a, val_) c) /. snd (convert_time (b, val_) d)) ;; |
let dist_traveled time ((speed_unit, speed_val) : speed_unit value) : dist_unit value = let (d_u, _) = speed_unit in let (time_u, time_val) = time in (d_u, snd (convert_speed (speed_unit, speed_val) (d_u, time_u)) *. time_val) ;; let rec passes_da_vinci t = let get_val = function | Branch (a,_) -> a | _ -> 0. in let rec sum_squares = function | [] -> 0. | h::t -> ((get_val h) *. (get_val h)) +. (sum_squares t) in match t with | Leaf -> true | Branch (_,[]) -> true | Branch (a,b) -> (a *. a) >= sum_squares b && (List.for_all (fun x->x) (List.map passes_da_vinci b)); ;; |
let mode (l: 'a list) : 'a = let l1 = List.sort compare l in let rec aux l1 ((cur_el, cur_num) : 'a * int) ((max_el, max_num) : 'a * int) = match l1 with [] -> if (cur_num > max_num) then cur_el else 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 aux l1 (List.hd l1, 0) (List.hd l1, 0) ;; let invalidinput ()= failwith "This input is invalid" ;; |
let pair_mode (l: 'a list) : 'a * 'a = let l1 = List.tl l in let l2 = List.rev (List.tl (List.rev l)) in let lc = List.combine l2 l1 in mode lc ;; |
let convert_time ((from_unit, val_) : time_unit value) to_unit : time_unit value = match from_unit with |Second -> if to_unit = Hour then Hour, (val_ /. 3600.) else Second, val_ |Hour -> if to_unit = Second then Second, (val_ *. 3600.) else Hour, val_ ;; |
let convert_dist ((from_unit, val_) : dist_unit value) to_unit : dist_unit value = match from_unit with | Foot -> if to_unit = Meter then Meter, (val_ *. 0.3048) else if to_unit = Mile then Mile, (val_ /. 5280.) else Foot, val_ | Meter -> if to_unit = Foot then Foot, (val_ /. 0.3048) else if to_unit = Mile then Mile, (val_ /. 0.3048 /. 5280.) else Meter, val_ | Mile -> if to_unit = Foot then Foot, (val_ *. 5280.) else if to_unit = Meter then Meter, (val_ *. 5280. *. 0.3048) else 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 to_dist = fst(to_unit) in let to_time = snd(to_unit) in let r1 = convert_dist (from_dist, val_) to_dist in let val1 = snd(r1) in let val2 = convert_time (to_time, val1) from_time in let val3 = snd(val2) in to_unit, val3 ;; |
let add_speed (a : speed_unit value) ((b_unit, b_val) : speed_unit value) : speed_unit value = let a_unit = fst(a) in let a_val = snd(a) in let a_convert = snd (convert_speed (a_unit, a_val) b_unit ) in let val_ = a_convert +. b_val in b_unit, val_ ;; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.