text
stringlengths
0
601k
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let checkNode list node = let (n1, n2, cost) = node in if n1 = vertex then (n2,cost) :: list else list in List.fold_left checkNode [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (curr,weight) = node in if curr = b then ( (List.rev (curr :: visited), weight) ) else ( if List.mem curr visited then raise Fail else let neighbourList = neighbours g curr in if (List.length neighbourList) = 0 then raise Fail else ( let final = List.map (fun (d,w)-> (d, (weight+w))) neighbourList in aux_list final (curr :: visited) ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try (let (z,v) = h in aux_node h visited) with Fail -> aux_list t visited in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (curr,weight) = node in if curr = b then (fc [curr], sc weight) else ( if List.mem curr visited then raise Fail else let neighbourList = neighbours g curr in let final = List.map (fun (d,w)-> (d, (weight+w))) neighbourList in aux_list final (curr :: visited) (fun u -> fc (curr :: u)) (fun u -> u) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited fc sc with Fail -> aux_list t visited fc sc in aux_node (a,0) [] (fun u -> u) (fun v -> v);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all = find_all_paths g a b in let max = ref (-1) in let longest_path = ref ([],-1) in let rec find_max paths = match paths with | [] -> !longest_path | (p, weight)::t -> if weight >= !max then (max := weight; longest_path := (p, weight) ; find_max t ) else find_max t in match all with | [] -> None | h::t -> Some (find_max all);;
let open_account (initial_pass: passwd) : bank_account = ( let user_pass = ref initial_pass in let money = ref 0 in let num_tries = ref 0 in { update_pass = (fun old_pass new_pass -> ( if old_pass = !user_pass then (user_pass := new_pass; num_tries := 0) else (num_tries := !num_tries + 1; raise wrong_pass); () )); retrieve = (fun pass withdraw_val -> ( num_tries := !num_tries + 1; if (pass = !user_pass && !num_tries < 6) then (if withdraw_val >= 0 then (if withdraw_val <= !money then (money := !money - withdraw_val; num_tries := 0) else raise not_enough_balance) else raise negative_amount) else ( if !num_tries < 6 then raise wrong_pass else raise too_many_failures); () )); deposit = (fun pass deposit_val -> ( num_tries := !num_tries + 1; if (pass = !user_pass && !num_tries < 6) then (if deposit_val >= 0 then (money := !money + deposit_val; num_tries := 0) else raise negative_amount) else ( if !num_tries < 6 then raise wrong_pass else raise too_many_failures); () )); show_balance = (fun pass -> ( num_tries := !num_tries + 1; if (pass = !user_pass && !num_tries < 6) then (num_tries := 0; !money) else ( if !num_tries < 6 then raise wrong_pass else raise too_many_failures); )); }) ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun init edge -> match edge with | (point1, point2, weight) -> if point1 = vertex then (point2, weight) :: init else init | _ -> failwith "Not an edge") [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((name, edge_weight): 'a * weight) (visited : 'a list) : ('a list * weight) = let (p_list, p_weight) = aux_list (neighbours g name) (name :: visited) in (name::p_list, edge_weight + p_weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = let non_visited_places = List.filter (fun (next_stop, _) -> List.for_all (fun visited_node -> next_stop != visited_node) visited ) nodes in match non_visited_places with | x :: xs -> ( let (x_name, x_weight) = x in if x_name = b then ([x_name], x_weight) else try aux_node x visited with Fail -> aux_list xs (x_name::visited) ) | _ -> raise Fail in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((name, edge_weight): 'a * weight) (visited : 'a list) fc sc: ('a list * weight) = aux_list (neighbours g name) (name :: visited) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight) = let non_visited_places = List.filter (fun (next_stop, _) -> List.for_all (fun visited_node -> next_stop != visited_node) visited ) nodes in match non_visited_places with | [] -> (let (p_list, p_weight) = fc [] 0 in match p_list with | [] -> raise Fail | _ -> (p_list, p_weight)) | (x_name, x_weight) :: xs -> if x_name = b then (sc [b] x_weight) else let fail_case = fun a_list a_weight -> aux_list xs (x_name::visited) fc sc in aux_node (x_name, x_weight) visited fail_case (fun a_list a_weight -> sc (x_name::a_list) (x_weight + a_weight)) in aux_node (a,0) [] (fun path_list total_weight -> path_list, total_weight) (fun path_list total_weight -> a::path_list, total_weight);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | _ -> Some (List.fold_left (fun (cur_list, cur_weight) (path_list, path_weight) -> if path_weight > cur_weight then (path_list, path_weight) else (cur_list, cur_weight)) ([], 0) all_paths);;
let open_account (initial_pass: passwd) : bank_account = let p = ref initial_pass in let balance= ref 0 in let failures = ref 0 in { update_pass = (fun oldP newP -> if !p = oldP then (p := newP; failures := 0) else (failures := !failures + 1; raise wrong_pass)); deposit = (fun pass amt -> if !failures >= 5 then raise too_many_failures else if !p = pass then (failures := 0; if amt < 0 then raise negative_amount else balance := !balance + amt) else (failures := !failures + 1; raise wrong_pass)); retrieve = (fun pass amt -> if !failures >= 5 then raise too_many_failures else if !p = pass then (failures := 0; if amt > !balance then raise not_enough_balance else if amt < 0 then raise negative_amount else balance := !balance - amt) else (failures := !failures + 1; raise wrong_pass)); show_balance = (fun pass -> if !failures >= 5 then raise too_many_failures else if !p = pass then (failures := 0; !balance) else (failures := !failures + 1; raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper el acc = let (edge1, edge2, w) = el in if edge1 = vertex then (edge2, w)::acc else acc in List.fold_right helper g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec get_solution (node: 'a) (visited : 'a list) : ('a list * weight) = if node = b then ([b], 0) else let remaining = List.filter(fun el -> let (v1, v2, _) = el in (not (List.mem v2 visited)) && v1 = node) g.edges in match remaining with | [] -> raise Fail | h::_ -> let (v1, v2, w) = h in try let (l, w1) = get_solution v2 (v1::visited) in (v1::l, w+w1) with | Fail -> get_solution node (v2::visited) in get_solution a [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec helper (node : 'a) (visited : 'a list) fail (suc : ('a list * weight) -> ('a list * weight)) = if node = b then suc ([b], 0) else let remaining = List.filter(fun el -> let (v1, v2, w) = el in (not (List.mem v2 visited)) && v1 = node) g.edges in match remaining with | [] -> fail () | h::t -> let (v1, v2, w) = h in let suc2 = (fun r -> let (l, w1) = r in suc (v1::l, w + w1)) in let fail2 = (fun () -> helper node (v2::visited) fail suc) in helper v2 (v1::visited) fail2 suc2 in helper a [] (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in match l with | [] -> None | _ -> let l' = List.sort (fun x y -> let (_, w1) = x in let (_, w2) = y in w2 - w1) l in match l' with | [] -> None | h::_-> Some h;;
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let balance = ref 0 in let x : int ref = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> if not (String.equal (!pw)(old_pass)) then (x := !x + 1; raise wrong_pass;) else ( x := 0; pw := new_pass; )); deposit = (fun (mypass: passwd) (amount: int) -> if !x = 5 then (raise too_many_failures) else ( if not (String.equal (!pw)(mypass)) then (x := !x + 1; raise wrong_pass;) else ( x := 0; if amount < 0 then (raise negative_amount) else ( balance := !balance + amount; )))); retrieve = (fun (mypass: passwd) (amount: int) -> if !x = 5 then raise too_many_failures else ( if not (String.equal (!pw)(mypass)) then (x := !x + 1; raise wrong_pass;) else ( x := 0; if amount < 0 then raise negative_amount else ( if !balance - amount < 0 then raise not_enough_balance else balance:= !balance - amount )))); show_balance = (fun (mypass: passwd) -> if !x = 5 then raise too_many_failures else ( if not (String.equal (!pw)(mypass)) then (x := !x + 1; raise wrong_pass;) else ( x := 0; !balance ))) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let my_func = fun (v1, v2, w) r -> if v1 = vertex then (v2,w)::r else r in List.fold_right (my_func) (g.edges) ([]);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let count = ref 0 in let money = ref 0 in let check_pw pw1 = if !count >=5 then raise too_many_failures else let true_pw = !pw in if true_pw = pw1 then (count :=0) else (count := !count +1; raise wrong_pass) in let update_pass old_pw new_pw = if old_pw <> !pw then (count := !count +1; raise wrong_pass) else begin count := 0; pw := new_pw; end in let deposit pw num = check_pw pw ; if num >=0 then (money := !money + num) else raise negative_amount in let retrieve pw num = check_pw pw; if num < 0 then raise negative_amount else if num > !money then raise not_enough_balance else (money := !money - num) in let show_balance pw = check_pw pw; !money in {update_pass; retrieve; deposit; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edge = g.edges in let rec find edge vertex acc = match edge with |[] -> acc |(v1,v2,w) :: t -> if v1 = vertex then find t vertex [(v2,w)]@acc else find t vertex acc in find edge vertex [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v,w) = node in if (List.mem v visited) then raise Fail else if v = b then ([v],w) else let (l,w_total) = aux_list (neighbours g v) (v ::visited) in(v::l, w + w_total) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(v,w) :: t -> try aux_node (v,w) visited with Fail -> aux_list t visited in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v,w) = node in if (List.mem v visited) then fc () else if v = b then sc ([v],w) else (aux_list (neighbours g v) (v ::visited) fc (fun (l,w_total) -> sc (v::l, w + w_total))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(v,w) :: t -> aux_node (v,w) visited (fun() -> aux_list t visited fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec long l = match l with | [] -> None | [(l,w)] -> Some (l,w) | (l1,w1)::(l2,w2) ::t -> if w1>=w2 then long ((l1,w1)::t) else long ((l2,w2)::t) in long(find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let failures = ref 0 in { update_pass = ( fun input_pass new_pass : unit -> if input_pass = !pass then (pass := new_pass; failures := 0) else (failures := !failures + 1; raise wrong_pass)); retrieve = ( fun input_pass ret_ : unit -> if !failures >= 5 then raise too_many_failures else begin if input_pass = !pass then if ret_ < 0 then raise negative_amount else if (!balance - ret_ < 0) then raise not_enough_balance else (balance := !balance - ret_; failures := 0) else (failures := !failures + 1; raise wrong_pass) end); deposit = ( fun input_pass dep_ : unit -> if !failures >= 5 then raise too_many_failures else begin if input_pass = !pass then if dep_ < 0 then raise negative_amount else (balance := !balance + dep_; failures := 0) else (failures := !failures + 1; raise wrong_pass) end ); show_balance = (fun input_pass : int -> if !failures >= 5 then raise too_many_failures else begin if input_pass = !pass then () else (failures := !failures + 1; raise wrong_pass); (failures := 0; !balance) end); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f vertex edge init = let (x, y, w) = edge in if x = vertex then (y, w) :: init else init in List.fold_right (f vertex) g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) : (('a * weight) list) = match nodes with | [] -> raise Fail | nodes_hd :: nodes_tl -> if List.mem nodes_hd visited then raise Fail else let (hd_node, _) = nodes_hd in if hd_node = b then [nodes_hd] else let neigh_hd_node = neighbours g hd_node in try nodes_hd :: (aux_list neigh_hd_node (nodes_hd :: visited)) with Fail -> aux_list nodes_tl (nodes_hd :: visited @ neigh_hd_node) in let path = (a, 0) :: aux_list (neighbours g a) [] in let rec clean_path path acc = match path with | [] -> acc | hd :: tl -> let (hd_node, hd_w) = hd in let (acc_list, acc_w) = acc in clean_path tl ((acc_list @ [hd_node]), (acc_w + hd_w)) in clean_path path ([], 0);;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_list (nodes: ('a * weight) list) (visited: ('a * weight) list) fc sc = match nodes with | [] -> fc () | nodes_hd :: nodes_tl -> if List.mem nodes_hd visited then fc () else let (hd_node, hd_weight) = nodes_hd in if hd_node = b then sc ([hd_node], hd_weight) else let neigh_hd_node = neighbours g hd_node in let sc2 (node, weight) = sc (hd_node :: node, weight + hd_weight) in let fc2 = fun () -> aux_list nodes_tl (nodes_hd :: visited @ neigh_hd_node) fc sc in aux_list neigh_hd_node (nodes_hd :: visited) fc2 sc2 in let path = aux_list (neighbours g a) [] (fun () -> raise Fail) (fun x -> x) in let (list, weight) = path in (a :: list, weight);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented();;
let open_account (initial_pass: passwd) : bank_account = let cur_pass = ref initial_pass in let cur_balance = ref 0 in let attempts = ref 0 in { update_pass = (fun old_pass new_pass -> if (!cur_pass = old_pass) then (cur_pass := new_pass; attempts := 0;) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun pass amount -> if (!attempts = 5) then (raise wrong_pass) else if (!attempts > 5) then (raise too_many_failures) else if (!cur_pass <> pass) then (attempts := !attempts + 1; raise wrong_pass) else ( attempts := 0; if (amount < 0) then raise negative_amount else if (!cur_balance > amount) then (cur_balance := !cur_balance - amount; attempts := 0) else raise not_enough_balance)); deposit = (fun pass amount -> if (!attempts > 5) then (raise too_many_failures) else if (amount < 0) then raise negative_amount else if (!cur_pass <> pass) then (attempts := !attempts + 1; raise wrong_pass) else (cur_balance := !cur_balance + amount; attempts := 0)); show_balance = (fun pass -> if (!attempts > 5) then raise too_many_failures else if (!cur_pass = pass) then (attempts := 0; !cur_balance) else (attempts := 1 + !attempts; raise wrong_pass)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbours edge -> (let (current, dest, wt) = edge in if current = vertex then (dest, wt) :: neighbours else neighbours)) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v,w) = node in if List.mem v visited then raise Fail else if v = b then ([v],w) else let (nextA,nextWeight) = aux_list (neighbours g v) (v::visited) in (v::nextA,w+nextWeight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,w)::tail -> try aux_node (v,w) visited with Fail -> aux_list tail (v::visited) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v,w) = node in if List.mem v visited then fc () else if v = b then sc ([v],w) else aux_list (neighbours g v) (v::visited) fc (fun (nextA,nextWeight) -> sc (v::nextA, w+nextWeight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (v,w)::tail -> aux_node (v,w) visited (fun () -> aux_list tail (v::visited) fc sc) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match List.sort (fun (_,wa) (_,wb) -> if wa > wb then +1 else if wa < wb then -1 else 0) (find_all_paths g a b) with | [] -> None | (v,w)::_ -> Some(v,w);;
let open_account (initial_pass: passwd) : bank_account = let current_balance = ref 0 in let current_password = ref initial_pass in let num_failures = ref 0 in { update_pass = (fun old_pass new_pass -> if (old_pass = !current_password) then (current_password := new_pass ; num_failures := 0;) else (num_failures := !num_failures + 1 ; raise wrong_pass) ); retrieve = (fun pass amount -> if (pass = !current_password) then ( if (!num_failures >= 5) then raise too_many_failures else ( num_failures := 0; if (amount < 0) then (raise negative_amount) else( if (!current_balance - amount >= 0) then (current_balance := !current_balance - amount ;) else (raise not_enough_balance)))) else ( if (!num_failures >= 5) then (raise too_many_failures) else (num_failures := !num_failures + 1 ; raise wrong_pass)) ); deposit = (fun pass amount -> if (pass = !current_password) then (if (!num_failures >= 5) then raise too_many_failures else ( num_failures := 0; if (amount < 0) then (raise negative_amount) else (current_balance := !current_balance + amount ;))) else ( if (!num_failures >= 5) then (raise too_many_failures) else (num_failures := !num_failures + 1 ; raise wrong_pass)) ); show_balance = (fun pass -> if (pass = !current_password) then ( if (!num_failures >= 5) then raise too_many_failures else (num_failures := 0; !current_balance) ) else ( if (!num_failures >= 5) then (raise too_many_failures) else (num_failures := !num_failures + 1 ; raise wrong_pass)) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f a (n1, n2, w) = if (n1 = vertex) then (n2, w) :: a else a in List.fold_left f [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((cur, w): 'a * weight) (visited : 'a list) final_node weight : ('a list * weight) = if (cur = final_node) then (List.rev (final_node :: visited), weight + w) else (if (not (List.mem cur visited)) then aux_list (neighbours g cur) (cur :: visited) (weight + w) else raise Fail) and aux_list (nodes: ('a * weight) list) (visited: 'a list) weight : ('a list * weight) = match nodes with | [] -> raise Fail | (cur, w) :: t -> try aux_node (cur, w) visited b weight with Fail -> aux_list t visited weight in aux_list (neighbours g a) [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (cur, w) visited fc sc final_node weight : ('a list * weight)= if (cur = final_node) then (sc final_node visited weight w) else (if (not (List.mem cur visited)) then aux_list (neighbours g cur) (cur :: visited) fc sc (weight + w) else fc ()) and aux_list (nodes: ('a * weight) list) visited fc sc weight : ('a list * weight) = match nodes with | [] -> fc () | (cur, w) :: t -> let sc2 = sc in let fc2 = fun () -> (aux_node (cur, w) visited fc sc b weight) in aux_list t visited fc2 sc2 weight in aux_list (neighbours g a) [a] (fun () -> (raise Fail)) (fun final_node visited weight w -> (List.rev (final_node :: visited), weight + w)) 0 ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let max x y = if x >= y then x else y in let rec aux lst accW accL = match lst with | [] -> if accL = [] then None else Some (accL, accW) | (p, w) :: t -> let new_W = max accW w in if (new_W = accW) then aux t new_W accL else aux t new_W p in aux (find_all_paths g a b) 0 [] ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let tries = ref 0 in let password = ref initial_pass in { deposit = ( fun p f -> if (!tries < 5) then if (p = !password) then let () = tries := 0 in match f with | f when f < 0 -> raise negative_amount | _ -> balance := (!balance + f); else begin tries := (!tries + 1); raise wrong_pass end else raise too_many_failures ); update_pass = ( fun p np -> if p = !password then begin tries := 0; password := np end else begin tries := (!tries + 1); raise wrong_pass; end ); retrieve = ( fun p f -> if (!tries < 5) then if p = !password then let () = (tries := 0) in if !balance >= f then if f < 0 then raise negative_amount else begin balance := !balance - f; end else raise not_enough_balance else begin tries := (!tries + 1); raise wrong_pass; end else raise too_many_failures ); show_balance = ( fun p -> if (!tries < 5) then if p = !password then begin print_int !tries; tries := 0; !balance end else begin tries := (!tries + 1); raise wrong_pass end else raise (too_many_failures) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edges l = match edges with | [] -> l | (u, v, weight) :: xs when u = vertex -> helper xs (l @ [(v, weight)]) | _ :: xs -> helper xs l in helper g.edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc: int) : ('a list * weight) = let (u, weight) = node in match (u, weight) with | (v1, some) when v1 = b -> (visited @ [b]), acc + some | (prev, _) when List.mem prev visited -> raise Fail | (bruh, moment) -> aux_list (neighbours g bruh) (visited @ [bruh]) (acc + moment) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc: int) : ('a list * weight) = match nodes with | [] -> raise Fail | hd :: tl -> try aux_node hd visited acc with Fail -> aux_list tl (visited) acc in aux_list (neighbours g a) [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (u, weight) = node in match (u, weight) with | (v1, some) when v1 = b -> (visited @ [b], sc some) | (prev, _) when List.mem prev visited -> fc () | (bruh, moment) -> aux_list (neighbours g bruh) (visited @ [bruh]) fc (fun zucc -> (sc zucc) + moment) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> aux_node hd visited (fun () -> (aux_list tl visited fc sc)) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun moment -> moment) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> let rec find_max l (max, maxw) = match l with | [] -> (max, maxw) | (l, weight) :: tl -> if weight > maxw then find_max tl (l, weight) else find_max tl (max, maxw) in Some (find_max paths ([], -1)) ;;
let open_account (initial_pass: passwd) : bank_account = let local_password = ref initial_pass in let local_bal = ref 0 in let attempts = ref 0 in { update_pass = (fun (old_password:passwd) (new_password: passwd) -> if old_password = !(local_password) then (attempts := 0 ; local_password := new_password ) else (incr attempts; raise wrong_pass) ); retrieve = (fun (password:passwd) (amount: int) -> if !(attempts) >= 5 then raise too_many_failures else if !(local_password) = password then (attempts := 0 ; if amount < 0 then raise negative_amount else if amount > !(local_bal) then raise not_enough_balance else local_bal := !(local_bal) - amount ) else (incr attempts; raise wrong_pass) ); deposit = (fun (password:passwd) (amount: int) -> if !(attempts) >= 5 then raise too_many_failures else if !(local_password) = password then (attempts := 0 ; if amount < 0 then raise negative_amount else local_bal:= !(local_bal) + amount ) else (incr attempts; raise wrong_pass) ); show_balance = (fun (password:passwd) -> if !(attempts) >= 5 then raise too_many_failures else if !(local_password) = password then (attempts := 0 ; !(local_bal) ) else (incr attempts; raise wrong_pass) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let folder (x: 'a*'b*'c) (y: 'd) = let (v,n,w) = x in if v = vertex then (n,w)::y else y in List.fold_right folder g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v,w) = node in let node_neighbours = (neighbours g v) in let path = if v = b then ([],0) else if not (List.mem v visited) then aux_list node_neighbours (v::visited) else raise Fail in let (p,wt) = path in (v::p,wt+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |(v,w)::tl-> (try aux_node (v,w) visited with Fail-> aux_list tl visited) |_-> ( raise Fail ) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v,w) = node in if v = b then sc ([v],w) else if not (List.mem v visited) then aux_list (neighbours g v) (v::visited) fc (fun (p,wt) -> sc (v::p, (w + wt))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |(v,w)::tl-> let succC = sc in let failC = (fun () -> aux_list tl visited fc sc) in aux_node (v,w) visited (failC) (succC) |_-> fc () in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (initial_pass: passwd) : bank_account = let local_password = ref initial_pass in let local_bal = ref 0 in let attempts = ref 0 in { update_pass = (fun (old_password:passwd) (new_password: passwd) -> if old_password = !(local_password) then (attempts := 0 ; local_password := new_password ) else (incr attempts; raise wrong_pass) ); retrieve = (fun (password:passwd) (amount: int) -> if !(attempts) >= 5 then raise too_many_failures else if !(local_password) = password then (attempts := 0 ; if amount < 0 then raise negative_amount else if amount > !(local_bal) then raise not_enough_balance else local_bal := !(local_bal) - amount ) else (incr attempts; raise wrong_pass) ); deposit = (fun (password:passwd) (amount: int) -> if !(attempts) >= 5 then raise too_many_failures else if !(local_password) = password then (attempts := 0 ; if amount < 0 then raise negative_amount else local_bal:= !(local_bal) + amount ) else (incr attempts; raise wrong_pass) ); show_balance = (fun (password:passwd) -> if !(attempts) >= 5 then raise too_many_failures else if !(local_password) = password then (attempts := 0 ; !(local_bal) ) else (incr attempts; raise wrong_pass) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let folder (x: 'a*'b*'c) (y: 'd) = let (v,n,w) = x in if v = vertex then (n,w)::y else y in List.fold_right folder g.edges [];;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (v,w) = node in let node_neighbours = (neighbours g v) in let path = if v = b then ([],0) else if not (List.mem v visited) then aux_list node_neighbours (v::visited) else raise Fail in let (p,wt) = path in (v::p,wt+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |(v,w)::tl-> (try aux_node (v,w) visited with Fail-> aux_list tl visited) |_-> ( raise Fail ) in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v,w) = node in if v = b then sc ([v],w) else if not (List.mem v visited) then aux_list (neighbours g v) (v::visited) fc (fun (p,wt) -> sc (v::p, (w + wt))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |(v,w)::tl-> let succC = sc in let failC = (fun () -> aux_list tl visited fc sc) in aux_node (v,w) visited (failC) (succC) |_-> fc () in aux_node (a,0) [] (fun () -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max paths maxPath = match paths with |[]-> maxPath |path::tl -> let (p,w) = path in let (pM,wM) = maxPath in if w > wM then max tl path else max tl maxPath in let paths = (find_all_paths g a b) in match paths with |[]-> None |_-> Some (max paths ([],0));;
let open_account (initial_pass: passwd) : bank_account = let wpasscount = ref 0 in let balance = ref 0 in let cur_pass = ref initial_pass in { update_pass = (fun old_pass new_pass -> if !cur_pass = old_pass then (cur_pass := new_pass; wpasscount := 0) else (wpasscount := !wpasscount + 1;raise wrong_pass) ) ; deposit = ( fun pass money -> if !wpasscount > 4 then raise too_many_failures else ( if !cur_pass = pass then (if money >= 0 then ( balance := !balance + money; wpasscount := 0) else (wpasscount := 0; raise negative_amount) ) else( wpasscount := !wpasscount + 1; raise wrong_pass) ) ) ; retrieve = ( fun pass money -> if !wpasscount > 4 then raise too_many_failures else ( if !cur_pass = pass then( if money >= 0 then (if !balance - money >= 0 then ( balance := !balance - money; wpasscount := 0) else (wpasscount := 0; raise not_enough_balance )) else (wpasscount := 0; raise negative_amount) ) else (wpasscount := !wpasscount + 1; raise wrong_pass) ) ) ; show_balance = fun pass -> ( if !wpasscount > 4 then raise too_many_failures else ( if !cur_pass = pass then ( wpasscount := 0; !balance) else (wpasscount := !wpasscount + 1; raise wrong_pass) ) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let choice (t: ('a * weight) list) (tuple: 'a * 'a * weight) : ('a * weight) list = let (a,b,c) = tuple in if a = vertex then List.append t [(b,c)] else t in List.fold_left choice [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (str,w) = node in if List.mem str visited then raise Fail else( let visited1 = List.append visited [str] in if str = b then (visited1, w) else( let adding (tup : 'a * weight) : ('a * weight) = ( let (frst, snd) = tup in (frst, snd + w) ) in let neigh = neighbours g str in let new_neigh = List.map adding neigh in aux_list new_neigh visited1 ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | _ -> ( try aux_node (List.hd nodes) visited with Fail -> aux_list (List.tl nodes) visited ) in if (List.mem a g.nodes && List.mem b g.nodes) then aux_node (a,0) [] else raise Fail;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (str, w) = node in if List.mem str visited then fc () else( let visited1 = List.append visited [str] in if(str = b) then (visited1, sc w) else( let neigh = neighbours g str in aux_list neigh visited1 fc (fun r -> sc(r + w))) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | _ -> let head = List.hd nodes in let next = List.tl nodes in let fc2 = fun () -> aux_list next visited fc sc in aux_node head visited fc2 sc in if (List.mem a g.nodes && List.mem b g.nodes) then aux_node (a,0) [] (fun () -> raise Fail) (fun r -> r) else raise Fail;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> ( let compare (c,d) (a,b) = if(b >= d) then (a,b) else (c,d) in let answer = List.fold_left compare (List.hd paths) (List.tl paths) in Some(answer) );;
let open_account (initial_pass: passwd) : bank_account = let stored_pass = ref initial_pass in let balance = ref 0 in let fail_cont = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !stored_pass then (stored_pass := new_pass; fail_cont := 0;) else (fail_cont := !fail_cont + 1 ; raise wrong_pass)) ; deposit = (fun pass depot -> if !fail_cont >= 5 then raise too_many_failures else if pass = !stored_pass then (if depot < 0 then raise negative_amount else balance := !balance + depot; fail_cont := 0) else (fail_cont := !fail_cont + 1; if !fail_cont >=6 then raise too_many_failures else raise wrong_pass)); retrieve = (fun pass retr -> if !fail_cont >= 5 then raise too_many_failures else if pass = !stored_pass then (if retr < 0 then raise negative_amount else if (!balance - retr) < 0 then raise not_enough_balance else balance := !balance - retr; fail_cont := 0) else (fail_cont := !fail_cont + 1; if !fail_cont >=6 then raise too_many_failures else raise wrong_pass)); show_balance= (fun pass -> if !fail_cont >= 5 then raise too_many_failures else if pass = !stored_pass then (fail_cont := 0; !balance) else (fail_cont := !fail_cont + 1; if !fail_cont >=6 then raise too_many_failures else raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let neighbour_check node result= (let (checked,going_to,w) = node in if checked = vertex then ((going_to,w):: result) else result) in List.fold_right neighbour_check g.edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (name,weight)=node in if List.mem name visited then raise Fail else if name=b then ([name],weight) else let (path,final_weight) = (aux_list (neighbours g name) (name::visited)) in (name::path,final_weight+weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xl -> try (aux_node x visited) with Fail -> aux_list xl visited in let (results,f_weight) = aux_list (neighbours g a) [a] in (a::results,f_weight) ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (name,weight)=node in if List.mem name visited then fc () else if name=b then sc ([name],weight) else aux_list (neighbours g name ) (name::visited) fc (fun (path,nweight) -> sc (name::path,nweight +weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xl -> let sc2 = sc in let fc2= fun() -> (aux_list xl visited fc sc) in aux_node x visited fc2 sc2 in let (results,f_weight) = aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun x -> x) in (a::results,f_weight) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in let rec find_highest_num paths (high:int):int = match paths with | [] -> high | x::xl -> let (_,num)=x in if num > high then find_highest_num xl num else find_highest_num xl high in let rec find_path paths target= match paths with | [] -> None | x::xl -> let (_,num) = x in if num = target then Some x else find_path xl target in find_path all_paths (find_highest_num all_paths 0) ;;
let open_account initpass = let passwd = ref initpass in let numfailed = ref 0 in let balance = ref 0 in { update_pass = (fun entered newpass -> if entered = !passwd then begin passwd:= newpass; numfailed := 0 end else begin numfailed := !numfailed + 1; raise wrong_pass end ) ; retrieve = (fun entered wd -> if !numfailed >= 5 then raise too_many_failures else if entered = !passwd then begin numfailed := 0; if wd < 0 then raise negative_amount else if !balance >= wd then balance := !balance - wd else raise not_enough_balance end else begin numfailed := !numfailed + 1; raise wrong_pass end ) ; deposit = (fun entered dep -> if !numfailed >= 5 then raise too_many_failures else if entered = !passwd then if dep < 0 then raise negative_amount else begin numfailed := 0; balance := !balance + dep end else begin numfailed := !numfailed + 1; raise wrong_pass end) ; show_balance = (fun entered -> if !numfailed >= 5 then raise too_many_failures else if entered = !passwd then begin numfailed := 0; !balance end else begin numfailed := !numfailed + 1; raise wrong_pass end ) ; };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun x -> match x with (_,v2,w) -> (v2,w)) (List.filter (fun node -> match node with | (v1,_,_) -> v1 = vertex ) g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (neighbors: 'b list) (visited : 'a list) (weight: int): ('a list * weight) = match neighbors with | [] -> raise Fail | head::tl -> try match head with | (dest, w) -> if not (List.exists (fun a -> a=dest) visited) then if dest = b then (visited @ [b], weight+w) else aux_node (neighbours g dest) (visited @ [dest]) (weight + w) else raise Fail with Fail -> aux_node tl visited weight in aux_node (neighbours g a) [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (neighbors: 'b list) (visited : 'a list) fc sc : ('a list * weight)= match neighbors with | [] -> fc () | head::tl -> match head with | (dest, w) -> if not (List.exists (fun h -> h=dest) visited) then if dest = b then (visited @ [b], w + sc 0) else aux_node (neighbours g dest) (visited @ [dest]) (fun () -> aux_node tl visited fc sc) (fun h -> (sc w + h)) else raise Fail in aux_node (neighbours g a) [a] (fun () -> raise Fail) (fun m -> m);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account initpass = let passwd = ref initpass in let numfailed = ref 0 in let balance = ref 0 in { update_pass = (fun entered newpass -> if entered = !passwd then begin passwd:= newpass; numfailed := 0 end else begin numfailed := !numfailed + 1; raise wrong_pass end ) ; retrieve = (fun entered wd -> if !numfailed >= 5 then raise too_many_failures else if entered = !passwd then begin numfailed := 0; if wd < 0 then raise negative_amount else if !balance >= wd then balance := !balance - wd else raise not_enough_balance end else begin numfailed := !numfailed + 1; raise wrong_pass end ) ; deposit = (fun entered dep -> if !numfailed >= 5 then raise too_many_failures else if entered = !passwd then if dep < 0 then raise negative_amount else begin numfailed := 0; balance := !balance + dep end else begin numfailed := !numfailed + 1; raise wrong_pass end) ; show_balance = (fun entered -> if !numfailed >= 5 then raise too_many_failures else if entered = !passwd then begin numfailed := 0; !balance end else begin numfailed := !numfailed + 1; raise wrong_pass end ) ; };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun x -> let (_,v2,w) = x in (v2, w)) (List.filter (fun node -> let (v1,_,_) = node in v1 = vertex ) g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (neighbors: 'b list) (visited : 'a list) (weight: int): ('a list * weight) = match neighbors with | [] -> raise Fail | head::tl -> try let (dest,w) = head in if not (List.exists (fun a -> a=dest) visited) then if dest = b then (visited @ [b], weight+w) else aux_node (neighbours g dest) (visited @ [dest]) (weight + w) else raise Fail with Fail -> aux_node tl visited weight in aux_node (neighbours g a) [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = let (dest, w) = node in if not (List.exists (fun h -> h=dest) visited) then if dest = b then (visited @ [b], w + sc 0) else aux_list (neighbours g dest) (visited @ [dest]) fc ((+) (sc w)) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with head::tl -> aux_node head visited (fun () -> aux_list tl visited fc sc) sc | [] -> fc () in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun m -> m);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in let rec aux ls maxcost route = match ls with | head::tl -> let (l, c) = head in if c > maxcost then aux tl c l else aux tl maxcost route | [] -> match route with | [] -> None | _ -> Some (route, maxcost) in aux paths 0 [];;
let open_account (initial_pass : passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let num_failures = ref 0 in let update_pass (old_pass : passwd) (new_pass : passwd) = if !password = old_pass then ( num_failures := 0; password := new_pass ) else ( num_failures := !num_failures + 1; raise wrong_pass ) in let retrieve (cur_pass : passwd) (to_retrieve : int) = if !num_failures < 5 then if !password = cur_pass then ( num_failures := 0; if to_retrieve >= 0 then if to_retrieve <= !balance then balance := !balance - to_retrieve else raise not_enough_balance else raise negative_amount ) else ( num_failures := !num_failures + 1; raise wrong_pass ) else raise too_many_failures in let deposit (cur_pass : passwd) (to_deposit : int) = if !num_failures < 5 then if !password = cur_pass then ( num_failures := 0; if to_deposit >= 0 then balance := !balance + to_deposit else raise negative_amount ) else ( num_failures := !num_failures + 1; raise wrong_pass ) else raise too_many_failures in let show_balance (cur_pass : passwd) = if !num_failures < 5 then if !password = cur_pass then ( num_failures := 0; !balance ) else ( num_failures := !num_failures + 1; raise wrong_pass ) else raise too_many_failures in { update_pass = update_pass; retrieve = retrieve; deposit = deposit; show_balance = show_balance } ;;
let neighbours (g : 'a graph) (vertex : 'a) : ('a * weight) list = let f = fun vertex -> fun ls -> fun (edge : ('a * 'a * weight)) -> let (v1, v2, w) = edge in if v1 = vertex then (v2, w)::ls else ls in List.fold_left (f vertex) [] g.edges;;
let find_path (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) = let rec aux_node (node : 'a * weight) (visited : 'a list) (cur_w : weight): ('a list * weight) = let (v, w) = node in if v = b then (visited @ [v], cur_w + w) else if List.exists ((=) v) visited then raise Fail else aux_list (neighbours g v) (visited @ [v]) (cur_w + w) and aux_list (nodes : ('a * weight) list) (visited : 'a list) (cur_w : weight): ('a list * weight) = match nodes with | [] -> if a = b then ([a], 0) else raise Fail | hd::tl -> try aux_node hd visited cur_w with Fail -> aux_list tl visited cur_w in aux_list (neighbours g a) [a] 0 ;;
let find_path' (g : 'a graph) (a : 'a) (b : 'a) : ('a list * weight) = let rec aux_node (node : 'a * weight) (visited : 'a list) fc sc (to_visit : ('a * weight) list): ('a list * weight) = match node with | (v, _) when v = b -> sc (node, visited, fc 0) | (v, _) when List.exists ((=) v) visited && to_visit = [] -> raise Fail | (v, _) when List.exists ((=) v) visited -> let (next, _) = List.hd to_visit in aux_list (neighbours g next) (visited) fc sc (List.tl to_visit) | (v, w) -> aux_list (neighbours g v) (visited @ [v]) (fun x -> (x + w)) sc to_visit and aux_list (nodes : ('a * weight) list) (visited : 'a list) fc sc (to_visit : ('a * weight) list): ('a list * weight) = match nodes with | [] -> if a = b then sc ((a, 0), visited, fc 0) else raise Fail | hd::tl -> aux_node hd visited fc sc (tl @ to_visit) in let fc = fun x -> x in let sc = fun ((v, w), visited, total_w) -> (visited @ [v], total_w + w) in aux_list (neighbours g a) [a] fc sc [] ;;
let find_all_paths (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) list = let rec aux_node (node : 'a * weight) (visited : 'a list) (weights : weight list) (paths : ('a list * weight) list) : ('a list * weight) list = let (v, w) = node in;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in let rec get_max_length paths max_length = match paths with | hd::tl -> let (_, length) = hd in if length > max_length then get_max_length tl length else get_max_length tl max_length | [] -> max_length in let rec get_longest_path paths max_length = match paths with | hd::tl -> let (_, length) = hd in if length = max_length then Some hd else get_longest_path tl max_length | [] -> None in get_longest_path paths (get_max_length paths 0) ;;
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass and curr_balance = ref 0 and caution = ref 0 in { update_pass = (fun pass newPass -> if (not(pass = !curr_pass)) then begin caution := !caution + 1; raise wrong_pass end else begin curr_pass := newPass; caution := 0 end); retrieve = (fun pass num -> if (!caution = 5) then raise too_many_failures else begin if (not(pass = !curr_pass)) then begin caution := !caution + 1; raise wrong_pass end else begin if (num < 0) then raise negative_amount else if (num > !curr_balance) then raise not_enough_balance else curr_balance := !curr_balance - num; caution := 0 end end); deposit = (fun pass num -> if (!caution = 5) then raise too_many_failures else if (not(pass = !curr_pass)) then begin caution := !caution + 1; raise wrong_pass end else if (num < 0) then raise negative_amount else begin curr_balance := !curr_balance + num; caution := 0 end); show_balance = (fun pass -> if (!caution = 5) then raise too_many_failures else if (not (pass = !curr_pass)) then begin caution := !caution + 1; raise wrong_pass end else caution := 0; !curr_balance ); } ;; let rec helper l acc = match l with | [] -> acc | (_, y, w) :: ls -> helper ls ((y, w) :: acc) ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = helper (List.filter (fun (x, _, _) -> vertex = x) g.edges) [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight) : ('a list * weight) = match node with | (nextDoor, w) when nextDoor = b -> (List.rev visited @ [nextDoor], w + acc) | (nextDoor, w) -> if List.mem nextDoor visited then raise Fail else aux_list (neighbours g nextDoor) (nextDoor :: visited) (acc + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: ls -> try aux_node node visited acc with Fail -> aux_list ls visited acc in aux_list (neighbours g a) [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (fc) (sc: ('a list * weight) -> ('a list * weight)) : ('a list * weight) = match node with | (nextDoor, w) when nextDoor = b -> sc ([nextDoor], w) | (nextDoor, w') -> if List.mem nextDoor visited then fc () else aux_list (neighbours g nextDoor) (nextDoor :: visited) fc (fun (n, w) -> sc (nextDoor :: n, w' + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (fc) (sc) : ('a list * weight) = match nodes with | [] -> fc () | node :: ls -> let fail2 = fun () -> aux_list ls visited fc sc in aux_node node visited fail2 sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun (n, w) : ('a list * weight) -> (a :: n, w)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let find l = match l with | [] -> None | x :: _ -> Some x in find (List.rev (List.sort (fun (_, w) (_, w') -> compare w w') (find_all_paths g a b))) ;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let failures = ref 0 in let auth_fail = fun f exc -> f := !f + 1; raise exc in let update = fun f p n_p -> f := 0; p := n_p in let dep_helper = fun f b a -> f := 0; b := !b + a in let ret_helper = fun f b a -> f := 0; b := !b - a in let show_helper = fun f b -> f := 0; !b in let update_pass old_pass new_pass = if !pass = old_pass then update failures pass new_pass else auth_fail failures wrong_pass in let deposit pass_attempt amt = if !failures > 4 then raise too_many_failures else if !pass = pass_attempt then if amt < 0 then raise negative_amount else dep_helper failures balance amt else auth_fail failures wrong_pass in let retrieve pass_attempt amt = if !failures > 4 then raise too_many_failures else if !pass = pass_attempt then if amt < 0 then raise negative_amount else if (!balance - amt) < 0 then raise not_enough_balance else ret_helper failures balance amt else auth_fail failures wrong_pass in let show_balance pass_attempt = if !failures > 4 then raise too_many_failures else if !pass = pass_attempt then show_helper failures balance else auth_fail failures wrong_pass in let acct = { update_pass : passwd -> passwd -> unit; retrieve : passwd -> int -> unit; deposit : passwd -> int -> unit; show_balance : passwd -> int; } in acct ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (initial_pass: passwd) : bank_account = let cur_pass = ref initial_pass in let balance = ref 0 in let failures = ref 0 in let verify pass = if (!failures >= 5) then raise too_many_failures else if (pass <> !cur_pass) then (failures := !failures + 1; raise wrong_pass) else failures := 0 in { update_pass = (fun (old_pass: passwd) -> fun (new_pass: passwd) -> if (old_pass = !cur_pass) then (cur_pass := new_pass; failures := 0) else (failures := !failures + 1; raise wrong_pass) ); retrieve = (fun (pass: passwd) -> fun amount -> verify pass; if (amount < 0) then raise negative_amount else if (amount > !balance) then raise not_enough_balance else balance := !balance - amount ); deposit = (fun (pass: passwd) -> fun amount -> verify pass; if (amount < 0) then raise negative_amount else balance := !balance + amount ); show_balance = (fun (pass: passwd) -> verify pass; !balance) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let append_neighbour neighbour_list (source, dest, w) = if source = vertex then (dest, w) :: neighbour_list else neighbour_list in List.fold_left append_neighbour [] g.edges let neighbours_sum (g: 'a graph) (vertex: 'a) (acc: weight) : ('a * weight) list = let append_neighbour neighbour_list (source, dest, w) = if source = vertex then (dest, w + acc) :: neighbour_list else neighbour_list in List.fold_left append_neighbour [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec visit_node (cur: 'a) (acc: weight) (visited : 'a list): ('a list * weight) = visit_neighbours (neighbours_sum g cur acc) (cur :: visited) and visit_neighbours (neighbours: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match neighbours with | [] -> raise Fail | (next, w) :: tl when next = b -> (List.rev (next :: visited), w) | (next, _) :: tl when List.exists ((=) next) visited -> visit_neighbours tl visited | (next, w) :: tl -> try visit_node next w visited with Fail -> visit_neighbours tl visited in visit_node a 0 [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec visit_node (cur: 'a) (visited : 'a list) fc sc : ('a list * weight)= visit_neighbours (neighbours g cur) (cur :: visited) fc sc and visit_neighbours (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (next, w) :: tl when next = b -> sc ([next], w) | (next, _) :: tl when List.exists ((=) next) visited -> visit_neighbours tl visited fc sc | (next, w) :: tl -> let sc2 (v_l, w2) = sc (next :: v_l, w + w2) in let fc2 () = visit_neighbours tl visited fc sc in visit_node next visited fc2 sc2 in visit_node a [] (fun () -> raise Fail) (fun (v, w) -> (a :: v, w));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> let max_weight (p1, w1) (p2, w2) = if w1 > w2 then (p1, w1) else (p2, w2) in Some (List.fold_left max_weight ([], min_int) paths);;
let security_loop p my_password my_tries = if(!my_tries >= 5) then raise too_many_failures else if(String.equal !my_password p) then my_tries := 0 else let _ = my_tries := !my_tries +1 in raise wrong_pass ;;
let open_account (initial_pass: passwd) : bank_account = let my_password= ref initial_pass in let my_balance= ref 0 in let my_tries= ref 0 in { update_pass= (fun (new_p: passwd) -> if(String.equal !my_password new_p) then (fun new_p -> my_password:= new_p) else raise wrong_pass); deposit = (fun p a-> if(a < 0) then raise negative_amount else if(!my_tries >=5) then raise too_many_failures else let _ = security_loop p my_password my_tries in my_balance:= !my_balance + a); retrieve = (fun p a -> if(a > !my_balance) then raise not_enough_balance else if (a <0) then raise negative_amount else if(!my_tries >= 5) then raise too_many_failures else let _ = security_loop p my_password my_tries in my_balance:= !my_balance - a); show_balance= (fun p -> if(!my_tries >=5) then raise too_many_failures else let _ = security_loop p my_password my_tries in !my_balance); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = if(not (List.find (fun vertex -> vertex) g.nodes)) then raise Fail else let rec match_edges l_edges vertex lacc = match l_edges with | [] -> [] | (v1, v2,w)::t -> match (v1, v2,w) with | (vertex, _, _) -> (v2, w)::lacc | _ -> match_edges t vertex lacc in match_edges g.edges vertex [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();;