text
stringlengths
0
601k
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec total_weight (result : ('a * weight) list): int = match result with | [] -> 0 | x::xs -> snd x + total_weight xs in let rec nodes_to_list (result : ('a * weight) list): 'a list = match result with | [] -> [] |(a,b)::xs -> a::nodes_to_list xs in let rec process_node (node: 'a * weight) (visited : ('a * weight) list) fc: ('a list * weight)= if List.mem node visited then fc () else if fst node = b then (List.rev (nodes_to_list (node::visited)), total_weight (node::visited)) else let n = neighbours g (fst node) in if List.for_all (fun x -> not(List.mem x visited)) n then process_neighbours n (node::visited) (fun () -> raise Fail) else fc () and process_neighbours (nodes: ('a * weight) list) (visited: ('a * weight) list) fc: ('a list * weight) = match nodes with |[] -> fc () |x::xs -> process_node x visited (fun () -> process_neighbours xs visited (fun () -> raise Fail)) in process_node (a,0) [] (fun () -> raise Fail);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let x = find_all_paths g a b in let rec max_length current_max current_path tuples_list = match tuples_list with [] -> Some (current_path, current_max) |(s, i)::tl -> if i > current_max then max_length i s tl else max_length current_max current_path tl in if x = [] then None else max_length 0 [] x;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let no_tries = ref 0 in let pwd = ref initial_pass in let checker funct arg pass = if (!pwd = pass) then ( no_tries := 0 ; funct arg) else ( no_tries := !no_tries + 1 ; raise wrong_pass) in let op_check (funct : 'a -> 'b) amt = if (!no_tries <= 5) then ( if (amt >= 0) then funct amt else raise negative_amount) else raise too_many_failures in let upd = ( :=) pwd in let shw () = if (!no_tries <= 5) then !balance else raise too_many_failures in let ret amt = if (!balance >= amt) then (balance := !balance - amt ;) else raise not_enough_balance in let dep amt = balance := !balance + amt; in { update_pass = (fun oldpwd newpwd -> checker upd newpwd oldpwd) ; retrieve = (fun password amount -> checker (op_check ret) amount password) ; deposit = (fun password amount -> checker (op_check dep) amount password) ; show_balance = checker shw () } ;; let upd newpwd = z := 0 ; x := newpwd ; ;; let op_check (funct : 'a -> 'b) amt = if (!z <= 5) then ( if (amt >= 0) then funct amt else raise negative_amount ) else raise too_many_failures ;; let show () = if (!z <= 5) then !y else raise too_many_failures ;; let retrieve amt = if (amt <= !y) then (y := !y - amt ;) else raise not_enough_balance ;; let deposit amt = y := !y + amt ; ;; let checker funct arg pwd = if (!x = pwd) then ( funct arg) else ( z := !z + 1; raise wrong_pass) ;; *) let ngb node (list : ('a * weight) list) ((i, o, w) : 'a * 'a * weight) = if (i = node) then (o, w) :: list else list ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (ngb vertex) [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, n_weight): 'a * weight) (visited : 'a list) w (path : 'a list) : ('a list * weight) = if (node = b) then (List.rev (node :: path), (w + n_weight)) else if (List.exists ((=) node ) visited) then raise Fail else ( aux_list (neighbours g node) (node :: visited) (w + n_weight) (node :: path) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) w path : ('a list * weight) = match nodes with |[] -> raise Fail | x::xs -> try aux_node x visited w path with Fail -> aux_list xs visited w path in aux_list (neighbours g a) [a] 0 [a] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, n_weight): '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 = let rec longest paths ((max_path, max_weight) : 'a list * weight) = match paths with | [] -> Some (max_path, max_weight) | (path, wgt) :: xs -> if (wgt > max_weight) then longest xs (path, wgt) else longest xs (max_path, wgt) in let all_paths = find_all_paths g a b in if (List.length all_paths = 0) then None else (longest all_paths ([], 0)) ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let no_tries = ref 0 in let pwd = ref initial_pass in let checker funct arg pass = if (!pwd = pass) then ( no_tries := 0 ; funct arg) else ( no_tries := !no_tries + 1 ; raise wrong_pass) in let op_check (funct : 'a -> 'b) amt = if (!no_tries <= 5) then ( if (amt >= 0) then funct amt else raise negative_amount) else raise too_many_failures in let upd = ( :=) pwd in let shw () = if (!no_tries <= 5) then !balance else raise too_many_failures in let ret amt = if (!balance >= amt) then (balance := !balance - amt ;) else raise not_enough_balance in let dep amt = balance := !balance + amt; in { update_pass = (fun oldpwd newpwd -> checker upd newpwd oldpwd) ; retrieve = (fun password amount -> checker (op_check ret) amount password) ; deposit = (fun password amount -> checker (op_check dep) amount password) ; show_balance = checker shw () } ;; let upd newpwd = z := 0 ; x := newpwd ; ;; let op_check (funct : 'a -> 'b) amt = if (!z <= 5) then ( if (amt >= 0) then funct amt else raise negative_amount ) else raise too_many_failures ;; let show () = if (!z <= 5) then !y else raise too_many_failures ;; let retrieve amt = if (amt <= !y) then (y := !y - amt ;) else raise not_enough_balance ;; let deposit amt = y := !y + amt ; ;; let checker funct arg pwd = if (!x = pwd) then ( funct arg) else ( z := !z + 1; raise wrong_pass) ;; *) let ngb node (list : ('a * weight) list) ((i, o, w) : 'a * 'a * weight) = if (i = node) then (o, w) :: list else list ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (ngb vertex) [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, n_weight): 'a * weight) (visited : 'a list) w (path : 'a list) : ('a list * weight) = if (node = b) then (List.rev (node :: path), (w + n_weight)) else if (List.exists ((=) node ) visited) then raise Fail else ( aux_list (neighbours g node) (node :: visited) (w + n_weight) (node :: path) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) w path : ('a list * weight) = match nodes with |[] -> raise Fail | x::xs -> try aux_node x visited w path with Fail -> aux_list xs visited w path in aux_list (neighbours g a) [a] 0 [a] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((node, n_weight): '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 = let rec longest paths ((max_path, max_weight) : 'a list * weight) = match paths with | [] -> Some (max_path, max_weight) | (path, wgt) :: xs -> if (wgt > max_weight) then longest xs (path, wgt) else longest xs (max_path, wgt) in let all_paths = find_all_paths g a b in if (List.length all_paths = 0) then None else (longest all_paths ([], 0)) ;;
let open_account (initial_pass: passwd) : bank_account = let amount = ref 0 in let failures = ref 0 in let cur_passwd = ref (initial_pass) in { update_pass = (fun (old_pw: passwd) (new_pw: passwd) -> if old_pw = !cur_passwd then (failures:=0; cur_passwd := new_pw) else (failures:=!failures+1;raise wrong_pass) ; ); retrieve : passwd -> int -> unit = (fun (pw: passwd) (n: int) -> if !failures >= 5 then raise too_many_failures; if pw = !cur_passwd then (failures:=0; if n<0 then raise negative_amount else (if !amount-n<0 then raise not_enough_balance else amount:=!amount-n)) else (failures:= !failures+1;raise wrong_pass)); deposit : passwd -> int -> unit = (fun (pw: passwd) (n: int)-> if !failures >= 5 then raise too_many_failures; if pw = !cur_passwd then (failures:=0; if n<0 then raise negative_amount else amount:=!amount+n) else (failures:= !failures+1;raise wrong_pass) ); show_balance : (passwd -> int) = (fun pw -> if !failures >= 5 then raise too_many_failures; if pw = !cur_passwd then (failures:=0;!amount;) else (failures:=!failures+1;raise wrong_pass) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let list = (let f1 x = match x with |(a,_,_) when a = vertex ->true |_->false in List.filter f1 g.edges) in let f2 x = let (_,b,c) = x in (b, c) in List.map f2 list;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list*weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (suc: weight) : ('a list*weight) = match node with |(x,y) when x = b -> (List.rev (x::visited), suc+y) |(x,y)-> if List.mem x visited then raise Fail else let list = neighbours g x in aux_list list (x::visited) (suc+y) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (suc: weight) :('a list*weight) = match nodes with |[]-> raise Fail |x::xs ->try aux_node x visited suc with Fail -> aux_list xs visited suc 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)= match node with |(x,y) when x = b -> sc ([x],y) |(x,y) -> if List.mem x visited then fc () else aux_list (neighbours g x) (x::visited) fc (fun (v,m)->sc (x::v,y+m)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |x::xs ->aux_node x visited (fun()->aux_list xs visited fc sc) sc in aux_node (a,0) [] (fun ()-> raise Fail) (fun (x,y)-> (x,y));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest_helper (l: ('a list * weight) list) (node: ('a list * weight)) (len: weight) = match l with | [] -> if len=0 then None else Some (node) |x::xs-> let (_,w)=x in if w>len then longest_helper xs x w else longest_helper xs node len in longest_helper (find_all_paths g a b) ([],0) 0;;
let makeCounter () = let localCount = ref 0 in { increase = (fun () -> incr localCount; !localCount) ; reset = (fun () -> localCount := 0) ; num = (fun () -> !localCount >= 5) } ;;
let open_account (initial_pass: passwd) : bank_account = let lc = makeCounter () in let password = ref initial_pass in let money = ref 0 in { update_pass = ( fun old_passwd new_passwd -> if old_passwd = !password then let _ = lc.reset () in password := new_passwd else let _ = lc.increase () in raise wrong_pass ) ; retrieve = ( fun pswd amount -> if lc.num () then raise too_many_failures else if pswd = !password then if amount < 0 then raise negative_amount else if !money < amount then raise not_enough_balance else let _ = lc.reset () in money := (!money - amount) else let _ = lc.increase ()in raise wrong_pass ) ; deposit = ( fun pswd amount -> if lc.num () then raise too_many_failures else if pswd = !password then if amount < 0 then raise negative_amount else let _ = lc.reset () in money := (!money + amount) else let _ = lc.increase ()in raise wrong_pass ) ; show_balance = ( fun pswd -> if lc.num () then raise too_many_failures else if pswd = !password then let _ = lc.reset () in !money else let _ = lc.increase ()in raise wrong_pass ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in List.fold_right (fun (edge: ('a * 'a * weight)) (lst: ('a * weight) list) -> let v1, v2, w = edge in ( if v1 = vertex then (v2, w) :: lst else lst ) ) edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nodes_list = g.nodes in let edges = g.edges in if List.exists (fun node -> node = a) nodes_list && List.exists (fun node -> node = b) nodes_list then let rec check_for_path (edges: ('a * 'a * weight) list) (a: 'a) (b: 'a) (node_lst: 'a list * weight) (index: int): ('a list * weight) = if a = b then let alist, cost = node_lst in (alist@[b], cost) else if index = List.length edges then raise Fail else try let v1, v2, w = List.nth edges index in if v1 = a then let alist, cost = node_lst in if List.exists (fun node -> node = v2) alist then check_for_path edges a b node_lst (index+1) else check_for_path edges v2 b (alist@[v1], cost+w) 0 else check_for_path edges a b node_lst (index+1) with Fail -> check_for_path edges a b node_lst (index + 1) in check_for_path edges a b ([], 0) 0 else raise Fail ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nodes_list = g.nodes in let edges = g.edges in if List.exists (fun node -> node = a) nodes_list && List.exists (fun node -> node = b) nodes_list then let rec check_for_path (edges: ('a * 'a * weight) list) (a: 'a) (b: 'a) node_lst_fxn (index: int) node_lst = if a = b then node_lst_fxn ([b], 0) else if index = List.length edges then raise Fail else try let v1, v2, w = List.nth edges index in if v1 = a then if List.exists (fun node -> node = v2) node_lst then check_for_path edges a b node_lst_fxn (index+1) node_lst else check_for_path edges v2 b (fun r -> node_lst_fxn ( let node, cost = r in (v1::node, cost+w))) 0 (v1::node_lst) else check_for_path edges a b node_lst_fxn (index+1) node_lst with Fail -> check_for_path edges a b node_lst_fxn (index + 1) node_lst in check_for_path edges a b (fun r -> r) 0 [a] else raise Fail ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let ans = find_all_paths g a b in match ans with | [] -> None | _ -> let rec get_long_path (path_list: ('a list * weight) list) (best_ans: ('a list * weight)) : ('a list * weight) = match path_list with | [] -> best_ans | (lst, w)::rest_of_list -> let _, cost = best_ans in if w > cost then get_long_path rest_of_list (lst,w) else get_long_path rest_of_list best_ans in Some( get_long_path ans ([],0) ) ;;
let makeCounter () = let counter = ref 0 in { increase = (fun () -> counter := !counter + 1) ; reset = (fun () -> counter := 0) ; get_if_locked = (fun () -> !counter >= 5) } ;;
let open_account (initial_pass: passwd) : bank_account = let localCounter = makeCounter () in let password = ref initial_pass in let money = ref 0 in { update_pass = ( fun old_passwd new_passwd -> if old_passwd = !password then let _ = localCounter.reset () in password := new_passwd else let _ = localCounter.increase () in raise wrong_pass ) ; retrieve = ( fun pswd amount -> if localCounter.get_if_locked () then raise too_many_failures else if pswd = !password then if amount < 0 then raise negative_amount else if !money < amount then raise not_enough_balance else let _ = localCounter.reset () in money := (!money - amount) else let _ = localCounter.increase ()in raise wrong_pass ) ; deposit = ( fun pswd amount -> if localCounter.get_if_locked () then raise too_many_failures else if pswd = !password then if amount < 0 then raise negative_amount else let _ = localCounter.reset () in money := (!money + amount) else let _ = localCounter.increase ()in raise wrong_pass ) ; show_balance = ( fun pswd -> if localCounter.get_if_locked () then raise too_many_failures else if pswd = !password then let _ = localCounter.reset () in !money else let _ = localCounter.increase ()in raise wrong_pass ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in List.fold_right (fun (edge: ('a * 'a * weight)) (lst: ('a * weight) list) -> let v1, v2, w = edge in ( if v1 = vertex then (v2, w) :: lst else lst ) ) edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nodes_list = g.nodes in let edges = g.edges in if List.exists (fun node -> node = a) nodes_list && List.exists (fun node -> node = b) nodes_list then let rec check_for_path (edges: ('a * 'a * weight) list) (a: 'a) (b: 'a) (node_lst: 'a list * weight) (index: int): ('a list * weight) = if a = b then let alist, cost = node_lst in (alist@[b], cost) else if index = List.length edges then raise Fail else try let v1, v2, w = List.nth edges index in if v1 = a then let alist, cost = node_lst in if List.exists (fun node -> node = v2) alist then check_for_path edges a b node_lst (index+1) else check_for_path edges v2 b (alist@[v1], cost+w) 0 else check_for_path edges a b node_lst (index+1) with Fail -> check_for_path edges a b node_lst (index + 1) in check_for_path edges a b ([], 0) 0 else raise Fail ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nodes_list = g.nodes in let edges = g.edges in if List.exists (fun node -> node = a) nodes_list && List.exists (fun node -> node = b) nodes_list then let rec check_for_path (edges: ('a * 'a * weight) list) (a: 'a) (b: 'a) node_lst_fxn (index: int) node_lst = if a = b then node_lst_fxn ([b], 0) else if index = List.length edges then raise Fail else try let v1, v2, w = List.nth edges index in if v1 = a then if List.exists (fun node -> node = v2) node_lst then check_for_path edges a b node_lst_fxn (index+1) node_lst else check_for_path edges v2 b (fun r -> node_lst_fxn ( let node, cost = r in (v1::node, cost+w))) 0 (v1::node_lst) else check_for_path edges a b node_lst_fxn (index+1) node_lst with Fail -> check_for_path edges a b node_lst_fxn (index + 1) node_lst in check_for_path edges a b (fun r -> r) 0 [a] else raise Fail ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let ans = find_all_paths g a b in match ans with | [] -> None | _ -> let rec get_long_path (path_list: ('a list * weight) list) (best_ans: ('a list * weight)) : ('a list * weight) = match path_list with | [] -> best_ans | (lst, w)::rest_of_list -> let _, cost = best_ans in if w > cost then get_long_path rest_of_list (lst,w) else get_long_path rest_of_list best_ans in Some( get_long_path ans ([],0) ) ;;
let makeCounter () = let counter = ref 0 in { increase = (fun () -> counter := !counter + 1) ; reset = (fun () -> counter := 0) ; get_if_locked = (fun () -> !counter >= 5) } ;;
let open_account (initial_pass: passwd) : bank_account = let localCounter = makeCounter () in let password = ref initial_pass in let money = ref 0 in { update_pass = ( fun old_passwd new_passwd -> if old_passwd = !password then let _ = localCounter.reset () in password := new_passwd else let _ = localCounter.increase () in raise wrong_pass ) ; retrieve = ( fun pswd amount -> if localCounter.get_if_locked () then raise too_many_failures else if pswd = !password then if amount < 0 then raise negative_amount else if !money < amount then raise not_enough_balance else let _ = localCounter.reset () in money := (!money - amount) else let _ = localCounter.increase ()in raise wrong_pass ) ; deposit = ( fun pswd amount -> if localCounter.get_if_locked () then raise too_many_failures else if pswd = !password then if amount < 0 then raise negative_amount else let _ = localCounter.reset () in money := (!money + amount) else let _ = localCounter.increase ()in raise wrong_pass ) ; show_balance = ( fun pswd -> if localCounter.get_if_locked () then raise too_many_failures else if pswd = !password then let _ = localCounter.reset () in !money else let _ = localCounter.increase ()in raise wrong_pass ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in List.fold_right (fun (edge: ('a * 'a * weight)) (lst: ('a * weight) list) -> let v1, v2, w = edge in ( if v1 = vertex then (v2, w) :: lst else lst ) ) edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nodes_list = g.nodes in let edges = g.edges in if List.exists (fun node -> node = a) nodes_list && List.exists (fun node -> node = b) nodes_list then let rec check_for_path (edges: ('a * 'a * weight) list) (a: 'a) (b: 'a) (node_lst: 'a list * weight) (index: int): ('a list * weight) = if a = b then let alist, cost = node_lst in (alist@[b], cost) else if index = List.length edges then raise Fail else try let v1, v2, w = List.nth edges index in if v1 = a then let alist, cost = node_lst in if List.exists (fun node -> node = v2) alist then check_for_path edges a b node_lst (index+1) else check_for_path edges v2 b (alist@[v1], cost+w) 0 else check_for_path edges a b node_lst (index+1) with Fail -> check_for_path edges a b node_lst (index + 1) in check_for_path edges a b ([], 0) 0 else raise Fail ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nodes_list = g.nodes in let edges = g.edges in if List.exists (fun node -> node = a) nodes_list && List.exists (fun node -> node = b) nodes_list then let rec check_for_path (edges: ('a * 'a * weight) list) (a: 'a) (b: 'a) node_lst_fxn (index: int) node_lst = if a = b then node_lst_fxn ([b], 0) else if index = List.length edges then raise Fail else try let v1, v2, w = List.nth edges index in if v1 = a then if List.exists (fun node -> node = v2) node_lst then check_for_path edges a b node_lst_fxn (index+1) node_lst else check_for_path edges v2 b (fun r -> node_lst_fxn ( let node, cost = r in (v1::node, cost+w))) 0 (v1::node_lst) else check_for_path edges a b node_lst_fxn (index+1) node_lst with Fail -> check_for_path edges a b node_lst_fxn (index + 1) node_lst in check_for_path edges a b (fun r -> r) 0 [a] else raise Fail ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let ans = find_all_paths g a b in match ans with | [] -> None | _ -> let rec get_long_path (path_list: ('a list * weight) list) (best_ans: ('a list * weight)) : ('a list * weight) = match path_list with | [] -> best_ans | (lst, w)::rest_of_list -> let _, cost = best_ans in if w > cost then get_long_path rest_of_list (lst,w) else get_long_path rest_of_list best_ans in Some( get_long_path ans ([],0) ) ;;
let open_account (initial_pass: passwd) : bank_account = let money = ref 0 in let currentPassword = ref initial_pass in let failures = ref 0 in { update_pass = (fun password newPassword -> if not(String.equal password !currentPassword) then (failures := !failures + 1; raise wrong_pass) else failures := 0; currentPassword := newPassword ); retrieve = (fun password moneyToRetrieve -> if (!failures = 5) then raise too_many_failures else if not(String.equal password !currentPassword) then (failures := !failures + 1; raise wrong_pass) else match moneyToRetrieve < 0 with | true -> (failures := 0; raise negative_amount) | false -> match moneyToRetrieve > !money with | true -> (failures := 0; raise not_enough_balance) | false -> failures := 0; money := !money - moneyToRetrieve ); deposit = (fun password moneyToAdd -> if (!failures = 5) then raise too_many_failures else if not(String.equal password !currentPassword) then (failures := !failures + 1; raise wrong_pass) else if (moneyToAdd < 0) then (failures := 0; raise negative_amount) else failures := 0; money := !money + moneyToAdd ); show_balance = (fun (password: passwd) -> if (!failures = 5) then raise too_many_failures else if not(String.equal password !currentPassword) then (failures := !failures + 1; raise wrong_pass) else failures := 0; !money ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun (l: ('a * weight) list) y -> let (p,n,w) = y in if (p = vertex) then (n,w) :: l else l ) [] 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) (distance : int) : ('a list * weight) = let (n,w) = node in if (n = b) then (visited @ [b], distance + w) else if (List.exists (fun x -> x = n) visited) then raise Fail else aux_list (neighbours g n) (visited @ [n]) (distance + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (distance : int) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, w) :: xs -> if (n = b) then (visited @ [n], distance + w) else try aux_node (n,w) visited distance with Fail -> aux_list xs visited distance in let visited = [a] in let distance = 0 in let closest = neighbours g a in aux_list closest visited distance;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (distance : int) fc sc : ('a list * weight)= let (n,w) = node in if (n = b) then (visited @ [b], distance + w) else if (List.exists (fun x -> x = n) visited) then fc () else let sc2 = fun n w -> (n :: visited, distance + w) in aux_list (neighbours g n) (visited @ [n]) (distance + w) fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) (distance : int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (n,w) :: xs -> let sc2 = fun n w -> (n :: visited, distance + w) in let fc2 = fun () -> aux_list xs visited distance fc sc in aux_node (n,w) visited distance fc2 sc2 in let visited = [a] in let distance = 0 in let closest = neighbours g a in let fc = fun () -> raise Fail in let sc = fun n w -> (visited @ [n], distance + w) in match closest with | [] -> fc () | (p,w) :: xs -> if p = b then sc p w else let sc2 = sc in let fc2 = fun () -> aux_list xs visited distance fc sc in aux_list (neighbours g p) (visited @ [p]) (distance + w) fc2 sc2;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec path_helper (g: 'a graph) (a: 'a) (b: 'a) (allpaths: ('a list * weight) list) (max : 'a list * weight) : ('a list * weight) option = match allpaths with | [] -> None | (l,w) :: xs -> if (xs = []) then if (w > snd (max)) then Some (l,w) else Some max else if (w > snd (max)) then path_helper g a b xs (l,w) else path_helper g a b xs max in let allthepaths = find_all_paths g a b in path_helper g a b allthepaths ([], 0) ;;
let open_account (initial_pass: passwd) : bank_account = let failure_count = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun input1 input2 -> if input1 = !password then let _ = (failure_count := 0) in password := input2 else if !failure_count > 5 then raise too_many_failures else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass ); retrieve = (if !failure_count > 5 then raise too_many_failures else fun input1 input2 -> if input1 = !password then if input2 > 0 then if !balance - input2 > 0 then balance := !balance - input2 else raise not_enough_balance else raise negative_amount else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass ); deposit = (if !failure_count > 5 then raise too_many_failures else fun input1 input2 -> if input1 = !password then if input2 < 0 then raise negative_amount else balance := !balance + input2 else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass ); show_balance = (if !failure_count > 5 then raise too_many_failures else fun input1 -> if input1 = !password then !balance else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass) };;
let neighbours (g: 'a graph) (vertex: 'a): ('a * weight) list = let _ = g in List.fold_right (fun x y -> let (a,b,c) = x in if a = vertex then (b,c)::y else y) g.edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neib = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (cur_w:weight) : ('a list * weight) = if not(List.mem (fst(node)) visited) then let visited = fst(node)::visited in let cur_w = cur_w + snd(node) in if (fst(node) = b) then (List.rev visited,cur_w) else let new_neib = neighbours g (fst(node)) in aux_list new_neib visited cur_w else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cur_w:weight) : ('a list * weight) = match nodes with | []-> raise Fail | (a,b)::tail -> try aux_node (a,b) visited cur_w with Fail -> aux_list tail visited cur_w in aux_list neib [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neib = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = if not(List.mem (fst(node)) visited) then let visited = fst(node)::visited in if (fst(node) = b) then (List.rev visited,sc (snd(node))) else let new_neib = neighbours g (fst(node)) in aux_list new_neib visited fc (fun a -> sc(a + snd(node))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | []-> fc () | (a,b)::tail -> aux_node (a,b) visited fc sc in aux_list neib [a] (fun() -> raise Fail) (fun r -> r) ;;
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 failure_count = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun input1 input2 -> if input1 = !password then let _ = (failure_count := 0) in password := input2 else if !failure_count > 5 then raise too_many_failures else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass ); retrieve = (if !failure_count > 5 then raise too_many_failures else fun input1 input2 -> if input1 = !password then if input2 > 0 then if !balance - input2 > 0 then balance := !balance - input2 else raise not_enough_balance else raise negative_amount else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass ); deposit = (if !failure_count > 5 then raise too_many_failures else fun input1 input2 -> if input1 = !password then if input2 < 0 then raise negative_amount else balance := !balance + input2 else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass ); show_balance = (if !failure_count > 5 then raise too_many_failures else fun input1 -> if input1 = !password then !balance else let _ = (failure_count := !failure_count + 1 ) in raise wrong_pass) };;
let neighbours (g: 'a graph) (vertex: 'a): ('a * weight) list = let _ = g in List.fold_right (fun x y -> let (a,b,c) = x in if a = vertex then (b,c)::y else y) g.edges [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neib = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (cur_w:weight) : ('a list * weight) = if not(List.mem (fst(node)) visited) then let visited = fst(node)::visited in let cur_w = cur_w + snd(node) in if (fst(node) = b) then (List.rev visited,cur_w) else let new_neib = neighbours g (fst(node)) in aux_list new_neib visited cur_w else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) (cur_w:weight) : ('a list * weight) = match nodes with | []-> raise Fail | (a,b)::tail -> try aux_node (a,b) visited cur_w with Fail -> aux_list tail visited cur_w in aux_list neib [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neib = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = if not(List.mem (fst(node)) visited) then let visited = fst(node)::visited in if (fst(node) = b) then (List.rev visited,sc (snd(node))) else let new_neib = neighbours g (fst(node)) in aux_list new_neib visited fc (fun a -> sc(a + snd(node))) else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | []-> fc () | (a,b)::tail -> aux_node (a,b) visited fc sc in aux_list neib [a] (fun() -> raise Fail) (fun r -> r) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (find_path g a b) with Fail -> None;;
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass in let balance = ref 0 in let attempts = ref 1 in { update_pass = (fun pass new_pass -> if pass = !curr_pass then (attempts := 1; curr_pass := new_pass) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun pass amt -> if !attempts > 5 then raise too_many_failures else if pass = !curr_pass then (attempts := 1; if amt < 0 then raise negative_amount else if amt > !balance then raise not_enough_balance else balance := !balance - amt) else (attempts := !attempts + 1; raise wrong_pass)); deposit = (fun pass amt -> if !attempts > 5 then raise too_many_failures else if pass = !curr_pass then (attempts := 1; if amt < 0 then raise negative_amount else balance := !balance + amt) else (attempts := !attempts + 1; raise wrong_pass)); show_balance = (fun pass -> if !attempts > 5 then raise too_many_failures else if pass = !curr_pass then (attempts := 1; !balance) else (attempts := !attempts + 1; raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec get_neighbours (nl: ('a * weight) list) (edge: ('a * 'a * weight)) : ('a * weight) list = let (fn, tn, w) = edge in if vertex = fn then match nl with | [] -> (tn, w) :: [] | hd :: tl -> hd :: get_neighbours tl edge else nl in List.fold_left get_neighbours [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let tw = ref 0 in let nl = ref (a :: []) in let v = ref (a :: []) in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n, w) = node in match visited with | [] -> (v := (n :: !v); nl := n :: !nl; tw := !tw + w; if n = b then (List.rev !nl, !tw) else (try aux_list (neighbours g n) !v with Fail -> (nl := List.tl !nl; tw := !tw - w; raise Fail))) | hd :: tl -> if n = hd then raise Fail else aux_node node tl and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd :: tl -> (v := visited; try aux_node hd visited with Fail -> aux_list tl visited) in aux_list (neighbours g a) (a :: []) ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let tw = ref 0 in let nl = ref (a :: []) in let v = ref (a :: []) in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = let (n, w) = node in match visited with | [] -> (v := (n :: !v); nl := n :: !nl; tw := !tw + w; if n = b then sc (List.rev !nl, !tw) else (let fc' = fun () -> (nl := List.tl !nl; tw := !tw - w; fc ()) in aux_list (neighbours g n) !v fc' sc)) | hd :: tl -> if n = hd then fc () else aux_node node tl fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd :: tl -> (v := visited; let fc' = fun () -> aux_list tl visited fc sc in aux_node hd visited fc' sc) in let fc' = (fun () -> if !tw = 0 then raise Fail else (!nl, !tw)) in aux_list (neighbours g a) (a :: []) fc' (fun x -> x) ;;
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 | hd :: tl -> if tl = [] then Some hd else let max = (fun x path -> let (p, w) = x in let (p', w') = path in if w' > w then (p', w') else (p, w)) in Some (List.fold_left max hd tl) ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let attempts = ref 0 in let password = ref initial_pass in { update_pass = (fun oldPwd newPwd -> if oldPwd = !password then ( attempts := 0; password := newPwd ) else ( attempts := (!attempts+1); raise wrong_pass ) ) ; retrieve = (fun pwd amount -> if (!attempts = 5) then raise too_many_failures else ( if (pwd = !password) then ( attempts := 0; if (amount >= 0) then ( if (amount <= !balance) then balance := (!balance-amount) else raise not_enough_balance ) else raise negative_amount ) else ( attempts := (!attempts+1); raise wrong_pass ) ) ) ; deposit = (fun pwd amount -> if (!attempts = 5) then raise too_many_failures else ( if (pwd = !password) then ( attempts := 0; if (amount >= 0) then balance := (!balance+amount) else raise negative_amount ) else ( attempts := (!attempts+1); raise wrong_pass ) ) ) ; show_balance = (fun pwd -> if (!attempts = 5) then raise too_many_failures else ( if (pwd = !password) then ( attempts := 0; !balance ) else ( attempts := (!attempts+1); raise wrong_pass ) ) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left ( fun arr (v1, v2, w) -> if (v1 = vertex) then (v2, w)::arr else arr ) [] 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) weight : ('a list * weight) = let (n, w) = node in if (n = b) then (visited @ [n], weight+w) else ( (if (List.mem n visited) then raise Fail else aux_list (neighbours g n) (visited @ [n]) (weight+w)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) weight : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> ( try aux_node x visited weight with Fail -> aux_list xs 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 (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight) = let (n, w) = node in if (n = b) then sc [n] w else ( (if (List.mem n visited) then fc () else ( aux_list (neighbours g n) (visited @ [n]) (fun () -> fc ()) (fun scN scW -> sc (n :: scN) (w + scW)) ) ) ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> aux_node x visited (fun () -> aux_list xs visited fc sc) sc in aux_list (neighbours g a) [a] (fun () -> raise Fail) (fun n w -> (a :: n, w));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allPaths = find_all_paths g a b in if (allPaths = []) then None else ( Some (List.fold_right (fun (p1, w1) (p2, w2) -> if w1 > w2 then (p1, w1) else (p2, w2)) allPaths ([], 0)) );;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun x y -> if x = !pass then (pass := y; attempts := 0) else (attempts := !attempts + 1; raise wrong_pass) ); retrieve = (fun x y -> if !attempts >= 5 then raise too_many_failures else if x = !pass then (if y >= 0 then (if y <= !balance then (balance := !balance - y;attempts := 0) else raise not_enough_balance) else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass) ); deposit = (fun x y -> if !attempts >= 5 then raise too_many_failures else if x = !pass then (if y >= 0 then (balance := !balance + y;attempts := 0) else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass) ); show_balance = (fun x -> if !attempts >= 5 then raise too_many_failures else if x = !pass then (attempts := 0;!balance) else (attempts := !attempts + 1; raise wrong_pass) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (x, y, w) acc -> if x = vertex then (y,w) :: acc else acc) 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 (x,y) = node in if x = b then ([b],y) else if List.mem x visited then raise Fail else let (path,w) = (aux_list (neighbours g x) ([x]@visited)) in (x::path, w+y) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |hd::tl -> try aux_node hd visited with Fail -> aux_list tl visited in aux_list [(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 (x,y) = node in if x = b then sc ([x],y) else if List.mem x visited then fc () else let sc2 = fun (w,z)-> sc ([x]@w, z+y) in aux_list (neighbours g x) ([x]@visited) fc (sc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |hd::tl -> let (x,y) = hd in let f = fun () -> aux_list tl visited fc sc in aux_node hd visited f sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (w,z)-> (w, z));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max (lc:('a list * weight) list) (m:'a list * weight)= match (lc,m) with |((x,y)::[], (w,v)) -> if (y>=v) then Some (x,y) else Some (w,v) |((x,y)::xs, (w,v)) -> if (y>=v) then max xs (x,y) else max xs (w,v) |_->None in max (find_all_paths g a b) ([],0);;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun x y -> if x = !pass then (pass := y; attempts := 0) else (attempts := !attempts + 1; raise wrong_pass) ); retrieve = (fun x y -> if !attempts >= 5 then raise too_many_failures else if x = !pass then (if y >= 0 then (if y <= !balance then (balance := !balance - y;attempts := 0) else raise not_enough_balance) else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass) ); deposit = (fun x y -> if !attempts >= 5 then raise too_many_failures else if x = !pass then (if y >= 0 then (balance := !balance + y;attempts := 0) else raise negative_amount) else (attempts := !attempts + 1; raise wrong_pass) ); show_balance = (fun x -> if !attempts >= 5 then raise too_many_failures else if x = !pass then (attempts := 0;!balance) else (attempts := !attempts + 1; raise wrong_pass) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (x, y, w) acc -> if x = vertex then (y,w) :: acc else acc) 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 (x,y) = node in if x = b then ([b],y) else if List.mem x visited then raise Fail else let (path,w) = (aux_list (neighbours g x) ([x]@visited)) in (x::path, w+y) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |hd::tl -> try aux_node hd visited with Fail -> aux_list tl visited in aux_list [(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 (x,y) = node in if x = b then sc ([x],y) else if List.mem x visited then fc () else let sc2 = fun (w,z)-> sc ([x]@w, z+y) in aux_list (neighbours g x) ([x]@visited) fc (sc2) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |hd::tl -> let (x,y) = hd in let f = fun () -> aux_list tl visited fc sc in aux_node hd visited f sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (w,z)-> (w, z)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max (lc:('a list * weight) list) (m:'a list * weight)= match (lc,m) with |((x,y)::[], (w,v)) -> if (y>=v) then Some (x,y) else Some (w,v) |((x,y)::xs, (w,v)) -> if (y>=v) then max xs (x,y) else max xs (w,v) |_->None in max (find_all_paths g a b) ([],0) ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let attempts = ref 0 in let password = ref initial_pass in let (a:bank_account) = { update_pass = (fun (a:passwd) (b:passwd) -> ( if (a = !password) then (attempts:=0; password:=b;) else ((attempts := !attempts + 1); raise (wrong_pass); ) )); retrieve = (fun (a:passwd) (b:int) -> ( if(!attempts > 4) then (attempts:= !attempts + 1;raise (too_many_failures)) else if(a = !password) then ( (attempts := 0); if (b<0) then raise (negative_amount) else if(b <= !balance) then (balance := !balance - b) else raise (not_enough_balance) ) else ((attempts := !attempts + 1); raise ((wrong_pass));) )); deposit = (fun (a:passwd) (b:int) -> ( if(!attempts > 4) then (attempts:= !attempts + 1;raise (too_many_failures)) else if(a = !password) then ( (attempts := 0); if (b<0) then raise (negative_amount) else balance := !balance + b ) else ((attempts := !attempts + 1); raise ((wrong_pass));) )); show_balance = (fun (a:passwd) -> if(!attempts > 4) then (attempts:= !attempts + 1;raise (too_many_failures)) else if(a = !password) then ( (attempts := 0); !balance; ) else ((attempts := !attempts + 1); raise ((wrong_pass));) ); } in a ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let nodes = g.nodes in let edges = g.edges in List.fold_left ( (fun a e -> let (l,r,w) = e in if(l=vertex) then (r,w):: a else a ) ) [] 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_node, wgt) = node in let new_visited = visited in let nb = (neighbours g curr_node) in let notvisited = List.filter ( fun v -> let (n,w) = v in not (List.exists (fun element -> element = n ) new_visited) ) nb in let new_visited = visited@[curr_node] in if (notvisited = []) then raise Fail else let (auxls,auxw) = try (aux_list nb new_visited) with Fail -> ([],0) in if(auxls <> []) then (auxls,(auxw + wgt)) else let rec iter ls = match ls with | (p,www)::rest -> ( try aux_node (p,(wgt+www)) new_visited with Fail -> (iter rest) ) | [] -> raise Fail in iter nb and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let notvisited = List.filter ( fun v -> let (n,w) = v in not (List.exists (fun element -> element = n ) visited) ) nodes in let exts = List.exists (fun bb -> let (n,w) = bb in n = b) notvisited in if(notvisited = []) then raise Fail else if (exts) then let (n,w) = (List.find (fun (m,z) -> m = b ) notvisited ) in (visited@[n],w) else 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 (curr_node, wgt) = node in let new_visited = visited in let nb = (neighbours g curr_node) in let notvisited = List.filter ( fun v -> let (n,w) = v in not (List.exists (fun element -> element = n ) new_visited) ) nb in let new_visited = visited@[curr_node] in if (notvisited = []) then (fc nb) else let (auxls,auxw) = (aux_list nb new_visited (fun (): ('a list * weight) -> ([],0)) ( fun visited notvisited -> let (n,w) = List.find (fun (m,z) -> m = b ) notvisited in (visited@[n],w)) ) in if(auxls <> []) then sc (auxls, auxw) wgt else let fail3 = fun nb -> match nb with | ([]) -> fc [] | ((ppp,www)::_) -> aux_node (ppp,wgt+www) (new_visited@[ppp]) fc sc in match nb with | ([]) -> fc [] | ((ppp,www)::_) -> aux_node (ppp,wgt+www) (new_visited) fail3 sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let notvisited = List.filter ( fun v -> let (n,w) = v in not (List.exists (fun element -> element = n ) visited) ) nodes in let exts = List.exists (fun bb -> let (n,w) = bb in n = b) notvisited in if(notvisited = []) then fc () else if (exts) then sc visited notvisited else fc () in match aux_node (a,0) [] (fun l -> ([],0)) (fun (auxls,auxw) wgt -> (auxls,(auxw + wgt))) with | ([],0) -> raise Fail | r -> r ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let ls = find_all_paths g a b in let ls = List.sort (fun (la,a) (lb,b) -> if a<b then -1 else if a>b then 1 else 0) ls in if(ls=[]) then None else Some (let x::_ = ls in x);;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let wrong_count = ref 0 in { update_pass = (fun old_pass new_pass -> (if old_pass = !pass then (pass := new_pass; wrong_count := 0) else (wrong_count := !wrong_count + 1; raise wrong_pass))); retrieve = (fun password amount -> (if !wrong_count < 5 then (if password <> !pass then (wrong_count := !wrong_count + 1; raise wrong_pass) else (wrong_count := 0; if amount >= 0 then (if amount <= !balance then balance := !balance - amount else raise not_enough_balance) else raise negative_amount)) else raise too_many_failures)); deposit = (fun password amount -> (if !wrong_count < 5 then (if password <> !pass then (wrong_count := !wrong_count + 1; raise wrong_pass) else (wrong_count := 0; if amount >= 0 then balance := !balance + amount else raise negative_amount)) else raise too_many_failures)); show_balance = (fun password -> (if !wrong_count < 5 then (if password <> !pass then (wrong_count := !wrong_count + 1; raise wrong_pass) else (wrong_count := 0; !balance)) else raise too_many_failures)) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec aux_nei (e: ('a * 'a * weight) list) (node: 'a) (result: ('a * weight) list): ('a * weight) list = match e with | [] -> result | (n1,n2,w)::tl -> (if n1 = node then aux_nei tl node (List.append result [(n2,w)]) else aux_nei tl node result) in aux_nei g.edges vertex [] ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([a],0) else let rec helper (neigh: ('a * weight) list) (visited: 'a list) (path: 'a list) (tot_weight: weight): ('a list * weight)= match neigh with | [] -> raise Fail | (node,weight)::t -> (if node = b then (List.append path [node], tot_weight + weight) else (if List.mem node visited then helper t visited path tot_weight else try helper (neighbours g node) (List.append visited [node]) (List.append path [node]) (tot_weight + weight) with Fail -> helper t visited path tot_weight)) in helper (neighbours g a) [a] [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec helper (neigh: ('a * weight) list) (visited: 'a list) (path: 'a list) (tot_weight: weight) fc sc: ('a list * weight)= match neigh with | [] -> fc () | (node,weight)::t -> (if node = b then sc (node,weight) (path,tot_weight) else (if List.mem node visited then helper t visited path tot_weight fc sc else let sc1 = sc in let fc1 = fun () -> helper (neighbours g node) (List.append visited [node]) (List.append path [node]) (tot_weight + weight) fc sc in helper t visited path tot_weight fc1 sc1)) in helper (neighbours g a) [a] [a] 0 (fun () -> raise Fail) (fun (n,w) (p,w1) -> (List.append p [n], w1+w)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper (paths: ('a list * weight) list) (high_path: 'a list) (high_cost: weight): ('a list * weight) option = match paths with | [] -> (if high_path = [] then None else Some (high_path, high_cost)) | (p,w)::t -> (if w > high_cost then helper t p w else helper t high_path high_cost) in helper (find_all_paths g a b) [] 0 ;;
let open_account (initial_pass : passwd) : bank_account = let password = ref initial_pass in let auth_failures = ref 0 in let balance = ref 0 in let auth_blocked = ref false in let update_pass (cur_pass : passwd) (new_pass : passwd) : unit = if cur_pass = !password then if !auth_blocked = true then ( auth_blocked := false; password := new_pass) else password := new_pass else ( incr auth_failures; raise wrong_pass) in let retrieve (pass : passwd) (amount : int) : unit = if !auth_blocked = true then raise too_many_failures else if pass <> !password then ( if !auth_failures > 5 then ( auth_blocked := true; raise too_many_failures) else incr auth_failures; raise wrong_pass) else if amount < 0 then raise negative_amount else if !balance - amount < 0 then raise not_enough_balance else balance := !balance - amount in let deposit (pass : passwd) (amount : int) : unit = if !auth_blocked = true then raise too_many_failures else if pass <> !password then ( if !auth_failures > 5 then ( auth_blocked := true; raise too_many_failures) else incr auth_failures; raise wrong_pass) else if amount < 0 then raise negative_amount else balance := !balance + amount in let show_balance (pass : passwd) : int = if !auth_blocked = true then raise too_many_failures else if pass <> !password then ( if !auth_failures > 5 then ( auth_blocked := true; raise too_many_failures) else incr auth_failures; raise wrong_pass) else !balance in { update_pass; retrieve; deposit; show_balance } ;;
let neighbours (g : 'a graph) (vertex : 'a) : ('a * weight) list = let f init (b : 'a * 'a * weight) = match b with | n1, n2, w -> if n1 = vertex then (n2, w) :: init else init in List.fold_left f [] g.edges ;; let rec sum lst acc : weight = match lst with | [] -> acc | h :: t -> sum t (acc + h) ;;
let find_path (g : 'a graph) (a : 'a) (b : 'a) : 'a list * weight = let rec aux u v path visited weights moves = if u = v then List.rev (u :: path), sum weights 0 else ( match moves with | (node, w) :: t -> if List.mem node visited then aux u v path visited weights t else aux node v (u :: path) (u :: visited) (w :: weights) (neighbours g node) | [] -> if List.length path = 0 then raise Fail else aux (List.hd path) v (List.tl path) (u :: visited) (List.tl weights) (neighbours g (List.hd path))) in aux a b [] [] [] (neighbours g a) ;;
let find_path' (g : 'a graph) (a : 'a) (b : 'a) : 'a list * weight = let fail_continuation f = f in let success_continuation end_path end_weights = List.rev end_path, sum end_weights 0 in let rec aux u v path visited weights moves fc sc = if u = v then sc (u :: path) weights else ( match moves with | (node, w) :: t -> if List.mem node visited then aux u v path visited weights t fc sc else aux node v (u :: path) (u :: visited) (w :: weights) (neighbours g node) fc sc | [] -> if List.length path = 0 then raise Fail else fc (aux (List.hd path) v (List.tl path) (u :: visited) (List.tl weights) (neighbours g (List.hd path)) fc sc)) in aux a b [] [] [] (neighbours g a) fail_continuation success_continuation ;;
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 pass = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun input1 input2 -> let old = !pass in match String.equal input1 old with | true -> pass := input2 | _ -> raise wrong_pass) ; retrieve = (fun input1 input2 -> let pw = !pass in let bl = !balance in match String.equal input1 pw with | true -> if input2 > bl then raise not_enough_balance else if input2 >= 0 then balance := bl - input2 else raise negative_amount | _ -> (count := !count + 1 ; if !count > 5 then raise too_many_failures ; raise wrong_pass)) ; deposit = (fun input1 input2 -> let pw = !pass in let bl = !balance in match String.equal input1 pw with | true -> if input2 < 0 then raise negative_amount else balance := bl + input2 | _ -> (count := !count + 1 ; if !count > 5 then raise too_many_failures ; raise wrong_pass)) ; show_balance = (fun input -> let pw = !pass in let bl = !balance in match String.equal input pw with | true -> bl | _ -> (count := !count + 1 ; if !count > 5 then raise too_many_failures ; raise wrong_pass)) ; } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper (e: ('a * 'a * weight) list) (vertex: 'a) : ('a * weight) list = match e with | [] -> [] | h::t -> let (s, _, w) = h in if s = vertex then (s, w)::helper t vertex else helper t vertex in helper 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 ();;
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 password = ref initial_pass in let failedAttempts = ref 0 in let balance = ref 0 in { update_pass = (fun (oldPasswd: passwd) (newPasswd: passwd) -> if !password = oldPasswd then let () = failedAttempts := 0 in password := newPasswd else let () = (failedAttempts := !failedAttempts + 1) in raise wrong_pass); retrieve = (fun (pwd: passwd) (amount: int) -> if !failedAttempts < 5 then if !password = pwd then if amount <= !balance then if amount >= 0 then let () = failedAttempts := 0 in balance := !balance - amount else raise negative_amount else raise not_enough_balance else let () = (failedAttempts := !failedAttempts + 1) in raise wrong_pass else raise too_many_failures); deposit = (fun (pwd: passwd) (amount: int) -> if !failedAttempts < 5 then if !password = pwd then if amount >= 0 then let () = failedAttempts := 0 in balance := !balance + amount else raise negative_amount else let () = (failedAttempts := !failedAttempts + 1) in raise wrong_pass else raise too_many_failures); show_balance = (fun (pwd: passwd) -> if !failedAttempts < 5 then if !password = pwd then let () = failedAttempts := 0 in !balance else let () = (failedAttempts := !failedAttempts + 1) in raise wrong_pass else raise too_many_failures) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (a: 'a * 'a * weight) (l: ('a * weight) list) = let (x,y,z) = a in if x = vertex then (y,z)::l else l in List.fold_right f 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 (x,wt) = node in if x = b then ([x], wt) else if List.mem x visited then raise Fail else let (l,lt) = aux_list (neighbours g x) (x::visited) in ((x::l),(wt + lt)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try aux_node x visited with Fail -> aux_list xs 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 (x,wt) = node in if x = b then sc [x] wt else if List.mem x visited then fc () else let sc2 = fun (y: 'a list) (lwt: weight) -> sc (x::y) (wt + lwt) in aux_list (neighbours g x) (x::visited) fc sc2 and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let fc2 = fun () -> aux_list xs visited fc sc in aux_node x visited fc2 sc in aux_node (a,0) [] (fun () -> raise Fail) (fun (xs: 'a list) (wt: weight) -> (xs,wt));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (x: ('a list * weight)) (y: ('a list * weight)) = let (_,xw) = x in let (_,yw) = y in if xw > yw then x else y in let l = find_all_paths g a b in match l with | [] -> None | _ -> Some (List.fold_left f ([], -1) l);;
let open_account (initial_pass: passwd) : bank_account = let cur_pwd = ref initial_pass in let balance = ref 0 in let failure_count = ref 0 in { update_pass = (fun attempted_pwd new_pwd -> if attempted_pwd = !cur_pwd then (failure_count := 0; cur_pwd := new_pwd) else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) ); retrieve = ( fun attempted_pwd amount -> if !failure_count > 4 then raise too_many_failures else if attempted_pwd = !cur_pwd then ( failure_count := 0; if amount <0 then raise negative_amount else if amount <= !balance then balance := !balance - amount else raise not_enough_balance ) else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) ); deposit = ( fun attempted_pwd amount -> if !failure_count > 4 then raise too_many_failures else if attempted_pwd = !cur_pwd then ( failure_count := 0; if amount < 0 then raise negative_amount else balance := !balance + amount ) else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) ); show_balance = ( fun attempted_pwd -> if !failure_count > 4 then raise too_many_failures else if attempted_pwd <> !cur_pwd then (failure_count := !failure_count + 1; raise wrong_pass) else ( failure_count := 0; !balance ) ); };;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes; edges = useful} = g in begin let rec helper useful vertex list = match useful with | [] -> list | [(v1, v2, w)] -> if v1 = vertex then helper [] vertex ((v2, w) :: list) else helper [] vertex list | [(v1, v2, w); rest] -> if v1 = vertex then helper [rest] vertex ((v2, w) :: list) else helper [rest] vertex list | (v1, v2, w)::rest -> if v1 = vertex then helper rest vertex ((v2, w) :: list) else helper rest vertex list in helper useful vertex [] end ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (total_weight: weight) : ('a list * weight) = let (a,weight) = node in if a = b then (List.rev (b :: visited), (total_weight + weight)) else if List.mem a visited then raise Fail else aux_list (neighbours g a) (a :: visited) (total_weight + weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight: weight) : ('a list * weight) = match nodes with | node :: rest -> (try aux_node node visited total_weight with Fail -> aux_list rest visited total_weight) | [] -> raise Fail 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) (total_weight: weight) fc sc : ('a list * weight)= let (a,weight) = node in if a = b then (List.rev (b :: visited), (total_weight + weight)) else if List.mem a visited then fc () else aux_list (neighbours g a) (a :: visited) (total_weight + weight) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_weight: weight) fc sc : ('a list * weight) = match nodes with | node :: rest -> aux_node node visited total_weight (fun () -> aux_list rest visited total_weight fc sc) sc | [] -> fc () in let sc = fun x -> x in let fc () = raise Fail in aux_list (neighbours g a) [a] 0 fc sc;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper paths_list big_list big_weight = match paths_list with | (list, weight):: rest -> if weight > big_weight then helper rest list weight else if weight = big_weight then if List.length list > List.length big_list then helper rest list weight else helper rest list weight else helper rest big_list big_weight | [] -> (big_list, big_weight) in if find_all_paths g a b = [] then None else Some (helper (find_all_paths g a b) [] 0) ;;