text
stringlengths
0
601k
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find graph = match graph.edges with | [] -> [] | (v1,v2,w) :: tl -> if v1 = vertex then (v2 , w ) :: find {nodes = g.nodes; edges = tl } else find { nodes = g.nodes; edges = tl } in find g;;
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 (v2,w) = node in if v2 = b then ([v2],w) else if List.mem v2 visited then raise Fail else let ( path, w2 ) = aux_list (neighbours g v2) ( v2 :: visited ) in ( v2 :: path, w + w2 ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: tl -> try aux_node node visited with Fail -> aux_list tl 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)= 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 c = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun oldpass newpass -> if ( oldpass = !password) then (c := 0; password := newpass) else ( c := !c + 1; raise wrong_pass ) ); retrieve = ( fun pw output -> if ( !c >= 5) then raise too_many_failures else ( if pw = !password then (c := 0; if output < 0 then raise negative_amount else if output > !balance then raise not_enough_balance else balance := !balance - output ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); deposit = ( fun pw input -> if ( !c >= 5 ) then raise too_many_failures else (if ( pw = !password) then ( c := 0; if input < 0 then raise negative_amount else balance := !balance + input ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); show_balance = ( fun pw -> if ( !c >= 5) then raise too_many_failures else if pw = !password then (c := 0; !balance ) else( c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find graph = match graph.edges with | [] -> [] | (v1,v2,w) :: tl -> if v1 = vertex then (v2 , w ) :: find {nodes = g.nodes; edges = tl } else find { nodes = g.nodes; edges = tl } in find g;;
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 (v2,w) = node in if v2 = b then ([v2],w) else if List.mem v2 visited then raise Fail else let ( path, w2 ) = aux_list (neighbours g v2) ( v2 :: visited ) in ( v2 :: path, w + w2 ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: tl -> try aux_node node visited with Fail -> aux_list tl 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 (v2,w) = node in if v2 = b then sc([v2],w) else if List.mem v2 visited then fc () else aux_list (neighbours g v2) ( v2 :: visited) fc (fun ( list, weight) -> sc ( v2 :: list, weight + w )) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node :: tl -> aux_node node visited ( fun () -> aux_list tl 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 max list = match list with | [] -> None | [(v,w)] -> Some (v,w) | (v1,w1) :: (v2,w2) :: tl -> if w1 >= w2 then max ((v2,w2) ::rest) else max ((v1,w1) :: rest) in max (find_all_paths g a b) *);;
let open_account (initial_pass: passwd) : bank_account = let c = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun oldpass newpass -> if ( oldpass = !password) then (c := 0; password := newpass) else ( c := !c + 1; raise wrong_pass ) ); retrieve = ( fun pw output -> if ( !c >= 5) then raise too_many_failures else ( if pw = !password then (c := 0; if output < 0 then raise negative_amount else if output > !balance then raise not_enough_balance else balance := !balance - output ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); deposit = ( fun pw input -> if ( !c >= 5 ) then raise too_many_failures else (if ( pw = !password) then ( c := 0; if input < 0 then raise negative_amount else balance := !balance + input ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); show_balance = ( fun pw -> if ( !c >= 5) then raise too_many_failures else if pw = !password then (c := 0; !balance ) else( c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find graph = match graph.edges with | [] -> [] | (v1,v2,w) :: tl -> if v1 = vertex then (v2 , w ) :: find {nodes = g.nodes; edges = tl } else find { nodes = g.nodes; edges = tl } in find g;;
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 (v2,w) = node in if v2 = b then ([v2],w) else if List.mem v2 visited then raise Fail else let ( path, w2 ) = aux_list (neighbours g v2) ( v2 :: visited ) in ( v2 :: path, w + w2 ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: tl -> try aux_node node visited with Fail -> aux_list tl 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 (v2,w) = node in if v2 = b then sc([v2],w) else if List.mem v2 visited then fc () else aux_list (neighbours g v2) ( v2 :: visited) fc (fun ( list, weight) -> sc ( v2 :: list, weight + w )) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node :: tl -> aux_node node visited ( fun () -> aux_list tl 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 max allpath = match allpath with | [] -> None | [(v,w)] -> Some (v,w) | (v1,w1) :: (v2,w2) :: tl -> if w1 <= w2 then max ((v2,w2) :: tl) else max ((v1,w1) :: tl) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let c = ref 0 in let balance = ref 0 in let password = ref initial_pass in { update_pass = (fun oldpass newpass -> if ( oldpass = !password) then (c := 0; password := newpass) else ( c := !c + 1; raise wrong_pass ) ); retrieve = ( fun pw output -> if ( !c >= 5) then raise too_many_failures else ( if pw = !password then (c := 0; if output < 0 then raise negative_amount else if output > !balance then raise not_enough_balance else balance := !balance - output ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); deposit = ( fun pw input -> if ( !c >= 5 ) then raise too_many_failures else (if ( pw = !password) then ( c := 0; if input < 0 then raise negative_amount else balance := !balance + input ) else (c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ) ); show_balance = ( fun pw -> if ( !c >= 5) then raise too_many_failures else if pw = !password then (c := 0; !balance ) else( c := !c + 1; if !c > 5 then raise too_many_failures else raise wrong_pass) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find graph = match graph.edges with | [] -> [] | (v1,v2,w) :: tl -> if v1 = vertex then (v2 , w ) :: find {nodes = g.nodes; edges = tl } else find { nodes = g.nodes; edges = tl } in find g;;
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 (v2,w) = node in if v2 = b then ([v2],w) else if List.mem v2 visited then raise Fail else let ( path, w2 ) = aux_list (neighbours g v2) ( v2 :: visited ) in ( v2 :: path, w + w2 ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | node :: tl -> try aux_node node visited with Fail -> aux_list tl 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 (v2,w) = node in if v2 = b then sc([v2],w) else if List.mem v2 visited then fc () else aux_list (neighbours g v2) ( v2 :: visited) fc (fun ( list, weight) -> sc ( v2 :: list, weight + w )) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | node :: tl -> aux_node node visited ( fun () -> aux_list tl 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 max allpath = match allpath with | [] -> None | [(v,w)] -> Some (v,w) | (v1,w1) :: (v2,w2) :: tl -> if w1 <= w2 then max ((v2,w2) :: tl) else max ((v1,w1) :: tl) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let amount = ref 0 in let pass = ref initial_pass in let count = ref 0 in { update_pass = (fun old_pass -> fun new_pass -> if !count > 4 then if old_pass <> !pass then (count := !count +1; raise wrong_pass) else (pass := new_pass; count := 0) else if old_pass <> !pass then (count := !count +1; raise wrong_pass) else (pass := new_pass; count := 0)) ; retrieve = ( fun pwd -> fun n -> if !count = 5 then raise too_many_failures; if pwd <> !pass then (count := !count +1; raise wrong_pass); (if n > !amount then raise not_enough_balance else if n < 0 then raise negative_amount else (amount := !amount - n; count := 0))) ; deposit = ( fun pwd -> fun n -> if !count = 5 then raise too_many_failures else if pwd <> !pass then (count := !count +1; raise wrong_pass) else (if n < 0 then raise negative_amount else (amount := !amount + n; count := 0))) ; show_balance = (fun pwd -> if !count = 5 then raise too_many_failures else if pwd <> !pass then (count := !count +1; raise wrong_pass) else (count := 0; !amount)) ; } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun nbs edge -> let (v1,v2,w) = edge in if v1 = vertex then nbs @ [(v2,w)] else nbs) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (vertex: 'a ) (visited : 'a list) (sum : int): ('a list * weight) = match neighbours g vertex with | [] -> raise Fail | [(v,w)] -> (if v = b then (visited @ [v],sum + w) else if List.mem v visited then raise Fail else aux_node v (visited @ [v]) (sum + w)) | _ -> aux_list (neighbours g vertex) visited sum and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum : int): ('a list * weight) = match nodes with | [] -> raise Fail | (v,w)::tl -> (if v = b then (visited @ [v],sum + w) else if List.mem v visited then aux_list tl visited sum else try (aux_node v (visited @ [v]) (sum + w)) with Fail -> aux_list tl visited sum) in aux_node a [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (vertex: 'a ) (visited : 'a list) (sum : int): ('a list * weight) = match neighbours g vertex with | [] -> raise Fail | [(v,w)] -> (if v = b then (visited @ [v],sum + w) else if List.mem v visited then raise Fail else aux_node v (visited @ [v]) (sum + w)) | _ -> aux_list (neighbours g vertex) visited sum and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum : int): ('a list * weight) = match nodes with | [] -> raise Fail | (v,w)::tl -> (if v = b then (visited @ [v],sum + w) else if List.mem v visited then aux_list tl visited sum else try (aux_node v (visited @ [v]) (sum + w)) with Fail -> aux_list tl visited sum) in aux_node a [a] 0 ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | lists -> Some(List.hd (List.sort (fun x y -> let (_,w1) = x and (_,w2) = y in if w1 < w2 then 1 else if w1 > w2 then -1 else 0) lists)) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let current_balance = ref 0 in let wrong_password_trials = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> ( if (old_pass = ! password) then (password := new_pass; wrong_password_trials := 0) else ((wrong_password_trials := ! wrong_password_trials + 1; raise wrong_pass)) ) ); retrieve = (fun pass amount -> ( match ! wrong_password_trials with | 5 -> (raise too_many_failures) | _ -> ( if (pass = ! password) then ( if (amount < 0) then (wrong_password_trials := 0; raise negative_amount) else if (amount > ! current_balance) then (wrong_password_trials := 0; raise not_enough_balance) else (wrong_password_trials := 0; current_balance := ! current_balance - amount)) else (wrong_password_trials := ! wrong_password_trials + 1; raise wrong_pass) ) )); deposit = (fun pass amount -> ( if ! wrong_password_trials = 5 then raise too_many_failures else if (pass = ! password) then ( if amount < 0 then (wrong_password_trials := 0; raise negative_amount) else (wrong_password_trials := 0; current_balance := ! current_balance + amount)) else ( (wrong_password_trials := !wrong_password_trials + 1; raise wrong_pass) ) )); show_balance = (fun pass -> ( if ! wrong_password_trials = 5 then raise too_many_failures else if (pass = ! password) then (wrong_password_trials := 0; !current_balance) else (wrong_password_trials := ! wrong_password_trials + 1; raise wrong_pass) ) ) } ;; let graph1 = { nodes = ["a";"b";"c";"d"]; edges = [("a", "b", 1); ("d", "b", 1); ("a", "d", 2); ("b", "d", 1)] } in let graph2 = { nodes = ["a";"b";"c"]; edges = [("a", "b", 1); ("a", "c", 2); ("c", "a", 2); ("b", "a", 1); ("c", "b", 3); ("b", "c", 3)] } in let graph3 = { nodes = ["ac";"ca";"fk";"kf"]; edges = [("ac", "ca", 2); ("fk", "kf", 1); ("ca", "ac", 4)] } in [ ((graph1, "c"), []); ((graph1, "a"), [("b", 1); ("d", 2)]); ((graph1, "b"), [("d", 1)]); ((graph2, "c"), [("a", 2); ("b", 3)]); ((graph3, "fk"), [("kf", 1)]); ((graph3, "hello"), [])] ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes = nodes_list; edges = edge_list} = g in ( List.fold_left (fun listy -> fun (a, b, c) -> (if vertex = a then List.append listy [(b, c)] else List.append listy []) ) [] edge_list) ;; let rec is_visited (element: 'a) (element_list : 'a list) : bool = match element_list with | x :: xs -> ((x = element) || (is_visited element xs)) | [] -> false ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nei = neighbours g a in let rec aux_node ((arbitrary, weight): 'a * weight) (visited : 'a list) (acc_final) (acc_weight): ('a list * weight) = if arbitrary = b then (acc_final @ [arbitrary] , acc_weight + weight) else ( if (is_visited arbitrary visited) then raise Fail else (aux_list (neighbours g arbitrary) (visited @ [arbitrary]) (acc_final @ [arbitrary]) (weight + acc_weight))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc_final) (acc_weight) : ('a list * weight) = match nodes with | (x1, x2)::rest -> ( if not (is_visited x1 visited) then (try aux_node (x1, x2) visited acc_final acc_weight with Fail -> aux_list rest (visited @ [x1]) acc_final acc_weight) else aux_list rest visited acc_final acc_weight) | [] -> raise Fail in aux_list nei [a] [a] 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let nei = neighbours g a in let rec aux_node ((arbitrary, weight): 'a * weight) (visited : 'a list) (acc_final) (acc_weight) fc sc: ('a list * weight) = if arbitrary = b then sc (acc_final @ [arbitrary] , acc_weight + weight) else ( if (is_visited arbitrary visited) then fc () else sc (aux_list (neighbours g arbitrary) (visited @ [arbitrary]) (acc_final @ [arbitrary]) (weight + acc_weight) fc sc)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc_final) (acc_weight) fc sc: ('a list * weight) = match nodes with | (x1, x2)::rest -> ( if not (is_visited x1 visited) then (let suc2 = sc in let fail2 = fun () -> aux_node (x1, x2) visited acc_final acc_weight fc sc in aux_list rest (visited @ [x1]) acc_final acc_weight fail2 suc2) else aux_list rest visited acc_final acc_weight fc sc ) | [] -> fc () in aux_list nei [a] [a] 0 (fun () -> raise Fail) (fun l -> l) ;; let rec combine (list_of_list_and_weight_tuples : ('a list * weight) list) (acc_tup1:'a list) (acc_weight:weight) acc_final : ('a list * weight) list = match list_of_list_and_weight_tuples with | (list_of_nodes, w) :: rest -> ( if (List.length list_of_nodes) = 1 then (combine rest (list_of_nodes @ acc_tup1) (acc_weight + w) acc_final) else combine rest (acc_tup1) (acc_weight) (acc_final @ [(list_of_nodes, w)] ) ) | _ -> acc_final @ [(acc_tup1, acc_weight)] ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = if (find_all_paths g a b) = [] then None else helper (find_all_paths g a b) None 0 ;;
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass in let num_attempt = ref 0 in let balance = ref 0 in { update_pass = (fun prev_pass new_pass -> if (compare !curr_pass prev_pass == 0) then (num_attempt := 0; curr_pass := new_pass ) else (num_attempt := (!num_attempt +1); raise wrong_pass)); deposit = (fun password amount -> if ((compare !curr_pass password == 0) && amount >= 0 && !num_attempt <= 4) then (balance := (!balance + amount); num_attempt := 0) else if (compare !curr_pass password != 0) && !num_attempt <= 4 then (num_attempt := (!num_attempt + 1); raise wrong_pass) else if amount < 0 then raise negative_amount else raise too_many_failures); retrieve = (fun password amount -> if (compare !curr_pass password == 0) && !balance - amount >= 0 && amount >= 0 && !num_attempt <= 4 then (balance := (!balance - amount); num_attempt := 0) else if (compare !curr_pass password != 0) && !num_attempt <= 4 then (num_attempt := (!num_attempt +1); raise wrong_pass) else if amount < 0 then raise negative_amount else if !balance - amount < 0 then raise not_enough_balance else raise too_many_failures); show_balance = (fun password -> if (compare !curr_pass password == 0) && !num_attempt <= 4 then (num_attempt := 0; !balance) else if (compare !curr_pass password != 0) && !num_attempt <= 4 then (num_attempt := (!num_attempt +1); raise wrong_pass) else raise too_many_failures); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let list_edges = List.filter (fun (a,b,c) -> compare a vertex == 0) (g.edges) in List.fold_right (fun (a,b,c) y -> [(b,c)] @ y) list_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) : ('a list * weight) = if (List.length (List.filter (fun x -> compare x (fst(node)) == 0) visited) == 0) then aux_list (neighbours g (fst(node))) ([fst(node)] @ visited) else aux_list (neighbours g (fst(node))) visited and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h :: t -> if (compare (fst(h)) b != 0) then aux_node h visited else (visited,0) in aux_list (neighbours g a) [];;
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 curr_pass = ref initial_pass and counter = ref 0 and balance = ref 0 in { update_pass = (fun (old_pass: passwd) (new_passwd: passwd) -> if old_pass = !curr_pass then (curr_pass := new_passwd; counter := 0) else (counter := !counter + 1; raise wrong_pass())); deposit = (fun (try_pass: passwd) (dep_amt: int) -> if !counter = 5 then raise too_many_failures() else (if try_pass = !curr_pass then (counter := 0; if dep_amt < 0 then raise negative_amount() else balance := !balance + dep_amt) else (counter := !counter + 1; raise wrong_pass()))); retrieve = (fun (try_pass: passwd) (retr_amt: int) -> if !counter = 5 then raise too_many_failures() else (if !curr_pass = try_pass then (counter := 0; if retr_amt < 0 then raise negative_amount() else if retr_amt > !balance then raise not_enough_balance else balance := !balance - retr_amt) else (counter := !counter + 1; raise wrong_pass()))); show_balance = (fun (try_pass: passwd) -> if !counter = 5 then raise too_many_failures() else (if !curr_pass = try_pass then (counter := 0; !balance) else (counter := !counter + 1; raise wrong_pass()))) } ;;
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 check_pswd pswd enter_pswd af skip = if (not skip) && !af >= 5 then raise too_many_failures else if enter_pswd = !pswd then (af := 0; ()) else (af := !af + 1; raise wrong_pass ) let lt_zero amnt = if amnt < 0 then raise negative_amount else ();;
let open_account (initial_pass: passwd) : bank_account = let pswd = ref initial_pass in let bal = ref 0 in let auth_fail = ref 0 in { update_pass = (fun e_pass new_pass -> (check_pswd pswd e_pass auth_fail true; pswd := new_pass; ()) ); deposit = (fun e_pass amnt -> check_pswd pswd e_pass auth_fail false; lt_zero amnt; bal := ( !bal + amnt ); () ); retrieve = (fun e_pass amnt -> check_pswd pswd e_pass auth_fail false; lt_zero amnt; if !bal < amnt then raise not_enough_balance else bal := ( !bal - amnt ); () ); show_balance = (fun e_pass -> (check_pswd pswd e_pass auth_fail false; !bal) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_v1, v2, w) -> (v2, w)) (List.filter (fun (v1, _v2, _w) -> v1 = 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) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented () *) let ex = {nodes = ["a"; "b"]; edges = [("a", "b", 2); (("b", "a", 5))]} let rm_node_paths edges node = List.filter (fun (v1, v2, _w) -> (not (v2 = node)) && (not (v1 = node))) edges let get_edges edges node = List.filter (fun (v1, _v2, _w) -> v1 = node ) edges let apnd ar v = let va = v :: [] in ar @ va;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec helper edges a endpoint path_list fw = if a = endpoint then (path_list, fw) else let useful_edges = get_edges edges a in let remaining_edges = rm_node_paths edges a in let rec try_all ue = match ue with | [] -> raise Fail | (_v1, v2, w) :: xt -> try helper remaining_edges v2 endpoint (apnd path_list v2) (fw + w) with Fail -> try_all xt in try_all useful_edges in (helper g.edges a b (a :: []) 0);;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec helper edges a endpoint path_list fw failure = if a = endpoint then (path_list, fw) else let useful_edges = get_edges edges a in let remaining_edges = rm_node_paths edges a in let rec try_all ue = match ue with | [] -> failure () | (_v1, v2, w) :: xt -> helper remaining_edges v2 endpoint (apnd path_list v2) (fw + w) (fun () -> try_all xt) in try_all useful_edges in (helper g.edges a b (a :: []) 0 (fun () -> 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 if paths = [] then None else Some (List.fold_left (fun (mnp, ml) (np, l) -> if ml < l then (np, l) else (mnp, ml)) ([], 0) paths );;
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let password_tries = ref 0 in let balance = ref 0 in { update_pass = (fun old_pass new_pass -> if not (old_pass = !current_pass) then (password_tries := !password_tries + 1; raise wrong_pass) else (password_tries := 0; current_pass := new_pass)); retrieve = (fun password amount -> if ((not (password = !current_pass)) && (!password_tries < 5)) then (password_tries := !password_tries + 1; raise wrong_pass) else if (!password_tries >= 5) then raise too_many_failures else if (password = !current_pass) then (password_tries := 0; if (amount < 0) then raise negative_amount else if (amount > !balance) then raise not_enough_balance else balance := !balance - amount)); deposit = (fun password amount -> if (not (password = !current_pass)) && (!password_tries < 5) then (password_tries := !password_tries + 1; raise wrong_pass) else if (!password_tries >= 5) then raise too_many_failures else if (password = !current_pass) then (password_tries := 0; if (amount < 0) then raise negative_amount else balance := !balance + amount)); show_balance = (fun password -> if (not (password = !current_pass)) && (!password_tries < 5) then (password_tries := !password_tries + 1; raise wrong_pass) else if (!password_tries >= 5) then raise too_many_failures else if (password = !current_pass) then (password_tries := 0; !balance) else !balance); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper l acc = match l with | [] -> acc | x :: xs -> let (v1,v2,w) = x in if v1 = vertex then helper xs ((v2,w) :: acc) else helper xs acc 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) (accW: int) : ('a list * weight) = let (v2,w) = node in if (List.exists (fun x -> x=v2) visited) then raise Fail else aux_list (neighbours g v2) (v2 :: visited) (accW + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (accW: int) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> let (v2,w) = x in if (v2 = b) then let accL = b :: visited in let accW = accW + w in (accL, accW) else try aux_node x visited accW with Fail -> aux_list xs visited accW in let nodes = neighbours g a in let (l,w) = aux_list nodes [a] 0 in let l = List.rev l in (l,w);;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (accW) (fc) (sc) : ('a list * weight)= let (v2,w) = node in if (List.exists (fun x -> x=v2) visited) then fc () else aux_list (neighbours g v2) (v2 :: visited) (accW + w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (accW) (fc) (sc) : ('a list * weight) = match nodes with | [] -> fc () | x :: xs -> let (v2,w) = x in if (v2 = b) then let accL = b :: visited in let accW = accW + w in sc (accL, accW) else let sc2 = sc in let fc2 = fun () -> aux_list xs visited accW fc sc in aux_node x visited accW fc2 sc2 in let nodes = neighbours g a in let (l,w) = aux_list nodes [a] 0 (fun () -> raise Fail) (fun x -> x) in let l = List.rev l in (l,w);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper l res = match l with | [] -> res | x :: xs -> let (_, w1) = x in let (_, w2) = res in if (w1 > w2) then helper xs x else helper xs res in let paths = find_all_paths g a b in if paths = [] then None else Some (helper paths ([], -1));;
let open_account (initial_pass: passwd) : bank_account = let passwrd = ref initial_pass in let counter = ref 0 in let balance = ref 0 in { update_pass = (fun input update -> if (input = !passwrd) then (passwrd := update; counter := 0) else (counter := !counter + 1; raise wrong_pass)); retrieve = (fun input num -> if (!counter = 5) then (raise too_many_failures) else if (input = !passwrd) then (if num < 0 then (counter := 0; raise negative_amount) else if !balance < num then (counter := 0; raise not_enough_balance) else (balance := !balance - num; counter := 0)) else (counter := !counter + 1; raise wrong_pass)); deposit = (fun input num -> if (!counter = 5) then (raise too_many_failures) else if (input = !passwrd) then (if num < 0 then (counter := 0; raise negative_amount) else (balance := !balance + num; counter := 0)) else (counter := !counter + 1; raise wrong_pass)); show_balance = (fun input -> if (!counter = 5) then (raise too_many_failures) else if (input = !passwrd) then (counter := 0; !balance) else (counter := !counter + 1;raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let {nodes;edges} = g in List.fold_left (fun acc (v1, v2, w) -> if v1 = vertex then (v2,w)::acc else acc) [] 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 v = b then ([v] , w) else if List.mem v visited then raise Fail else (let (path , cost) = aux_list (neighbours g v) (v :: visited) in (v :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | head :: tail -> try aux_node head visited with Fail -> aux_list tail 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 v = b then sc ([v], w) else if List.mem v visited then fc () else aux_list (neighbours g v) (v :: visited) fc (fun (path, cost) -> sc (v :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | head :: tail -> aux_node head visited (fun () -> aux_list tail visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun out -> out);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max path_list = match path_list with | [] -> None | [(path,weight)] -> Some (path,weight) | (path1,weight1)::(path2,weight2)::tail -> if weight1 >= weight2 then max ((path1,weight1)::tail) else max ((path2,weight2)::tail) in max (find_all_paths g a b);;
let message = "Wrong Password.";;
let open_account (initial_pass: passwd) : bank_account = let accName = ref initial_pass in let accBalance = ref 0 in let wrong_count = ref 0 in { update_pass = (fun a b -> if !wrong_count >= 5 && not (!accName = a) then raise wrong_pass else if not (!accName = a) then begin wrong_count := !wrong_count + 1; raise wrong_pass end else begin wrong_count := 0; accName := b end); deposit = (fun a b -> if !wrong_count >= 5 then raise too_many_failures else if not (!accName = a) && !wrong_count < 5 then begin wrong_count := !wrong_count + 1; raise wrong_pass end else if b < 0 then begin wrong_count := 0; raise negative_amount end else begin wrong_count := 0; accBalance := !accBalance + b end); retrieve = (fun a b -> if !wrong_count >= 5 then raise too_many_failures else if not (!accName = a) && !wrong_count < 5 then begin wrong_count := !wrong_count + 1; raise wrong_pass end else if b > !accBalance then begin wrong_count := 0; raise not_enough_balance end else if b < 0 then begin wrong_count := 0; raise negative_amount end else begin wrong_count := 0; accBalance := !accBalance - b end); show_balance = (fun a -> if !wrong_count >= 5 then raise too_many_failures else if not (!accName = a) && !wrong_count < 5 then begin wrong_count := !wrong_count + 1; raise wrong_pass end else begin wrong_count := 0; !accBalance end)} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let edges = g.edges in List.fold_left (fun base v -> let (v1, v2, w) = v in if v1 = vertex then base@[(v2, w)] else base) [] 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 if v = b then ([b], w) else if List.mem v visited then raise Fail else (let path, w1 = aux_list (node_neighbours) ([v]@visited) in ([v]@path, w1 + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list): ('a list * weight) = match nodes with | [] -> raise Fail | (v1, w1)::xs -> try aux_node (v1, w1) 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 f = (fun () -> raise Fail) in let s = (fun a -> a) in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (v, w) = node in let node_neighbours = neighbours g v in if v = b then sc ([b], w) else if List.mem v visited then fc () else aux_list (node_neighbours) ([v]@visited) fc (fun (a, c) -> sc (v::a, c + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc: ('a list * weight) = match nodes with | [] -> fc () | (v1, w1)::xs -> aux_node (v1, w1) visited (fun () -> aux_list xs visited fc sc) sc in aux_node (a, 0) [] f s;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_path = find_all_paths g a b in let biggest_weight = ref 0 in let rec the_path paths s c = match paths with | Some [] -> if s = [] then None else List.hd s | Some (h::xs) -> let (list, w) = h in if w > (c) then the_path (Some xs) ((Some h)::s) (w) else the_path (Some xs) (s) (c) in the_path (Some all_path) [] 0;;
let open_account (initial_pass: passwd) : bank_account = let passwd_r = ref initial_pass in let cur_balance = ref 0 in let counter = ref 0 in {update_pass = (fun input_passwd new_passwd -> if input_passwd = !passwd_r then (counter := 0; passwd_r := new_passwd) else (counter := !counter+1; raise wrong_pass)); deposit = (fun input_passwd money_d -> if !counter >= 5 then raise too_many_failures else if (input_passwd = !passwd_r && money_d >= 0) then (counter := 0;cur_balance := !cur_balance+money_d) else if input_passwd = !passwd_r && money_d < 0 then (counter := 0;raise negative_amount) else (counter := !counter+1;raise wrong_pass)); retrieve = (fun input_passwd money_r -> if !counter >= 5 then raise too_many_failures else ( if input_passwd = !passwd_r then if money_r < 0 then (counter := 0;raise negative_amount) else if money_r > !cur_balance then (counter := 0;raise not_enough_balance) else (counter := 0;cur_balance := !cur_balance-money_r) else (counter := !counter+1;raise wrong_pass))); show_balance = (fun input_passwd -> if !counter >= 5 then raise too_many_failures else if input_passwd = !passwd_r then (counter := 0;!cur_balance;) else (counter := !counter+1;raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1,v2,w) -> if v1=vertex then acc @ [(v2,w)] 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 (n,w) = node in if n=b then ([n],w) else if not (List.mem n visited) then let (p,c)=aux_list (neighbours g n) (visited@[n]) in (n::p,c+w) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |[] -> raise Fail |(n,w)::remaining -> try aux_node (n,w) visited with Fail -> aux_list remaining 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 (n,w) = node in if n=b then sc ([n],w) else if not (List.mem n visited) then let sc_cal = fun (p,c) -> (sc (n::p,c+w)) in aux_list (neighbours g n) (visited@[n]) fc sc_cal else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(n,w)::remaining -> let sc2 = sc in let fc2 = fun () -> aux_list remaining visited fc sc in aux_node (n,w) visited fc2 sc2 in aux_node (a,0) [] (fun () -> raise Fail) (fun sc_acc -> sc_acc);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_one all longest = match all with |[] -> Some longest |(l2,w2) :: remaining -> let (l1,w1)=longest in if w2>w1 then let longest = (l2,w2) in find_one remaining longest else find_one remaining longest in match find_all_paths g a b with |[] -> None |one::remaining -> find_one remaining one;;
let open_account (initial_pass: passwd) : bank_account = let passwd_r = ref initial_pass in let cur_balance = ref 0 in let counter = ref 0 in {update_pass = (fun input_passwd new_passwd -> if input_passwd = !passwd_r then (counter := 0; passwd_r := new_passwd) else (counter := !counter+1; raise wrong_pass)); deposit = (fun input_passwd money_d -> if !counter >= 5 then raise too_many_failures else if (input_passwd = !passwd_r && money_d >= 0) then (counter := 0;cur_balance := !cur_balance+money_d) else if input_passwd = !passwd_r && money_d < 0 then (counter := 0;raise negative_amount) else (counter := !counter+1;raise wrong_pass)); retrieve = (fun input_passwd money_r -> if !counter >= 5 then raise too_many_failures else ( if input_passwd = !passwd_r then if money_r < 0 then (counter := 0;raise negative_amount) else if money_r > !cur_balance then (counter := 0;raise not_enough_balance) else (counter := 0;cur_balance := !cur_balance-money_r) else (counter := !counter+1;raise wrong_pass))); show_balance = (fun input_passwd -> if !counter >= 5 then raise too_many_failures else if input_passwd = !passwd_r then (counter := 0;!cur_balance;) else (counter := !counter+1;raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (v1,v2,w) -> if v1=vertex then acc @ [(v2,w)] 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) acc : ('a list * weight) = let (n,w) = node in if n=b then (visited@[n],w+acc) else if not (List.mem n visited) then aux_list (neighbours g n) (visited@[n]) (acc+w) else raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc : ('a list * weight) = match nodes with |[] -> raise Fail |(n,w)::remaining -> try aux_node (n,w) visited acc with Fail -> aux_list remaining 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 (n,w) = node in if n=b then sc ([n],w) else if not (List.mem n visited) then let sc_cal = fun (p,c) -> (sc (n::p,c+w)) in aux_list (neighbours g n) (visited@[n]) fc sc_cal else fc () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |(n,w)::remaining -> let sc2 = sc in let fc2 = fun () -> aux_list remaining visited fc sc in aux_node (n,w) visited fc2 sc2 in aux_node (a,0) [] (fun () -> raise Fail) (fun sc_acc -> sc_acc);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec find_one all longest = match all with |[] -> Some longest |(l2,w2) :: remaining -> let (l1,w1)=longest in if w2>w1 then let longest = (l2,w2) in find_one remaining longest else find_one remaining longest in match find_all_paths g a b with |[] -> None |one::remaining -> find_one remaining one;;
let makePassword(initial) = let passwordMethods = ref initial in { set = (:=) passwordMethods ; check = ( fun pass -> pass = !passwordMethods); } let money() = let amount = ref 0 in { increase = (fun toadd -> amount := (!amount + toadd)); decrease = (fun todecrease -> amount := (!amount - todecrease)); get = (fun () -> !amount) } let attempt() = let tries = ref 0 in { increment = (fun () -> tries := (!tries +1)) ; reset = (fun() -> tries :=0); get = (fun() -> !tries) };;
let open_account (initial_pass: passwd) : bank_account = let b = money() in let p = makePassword(initial_pass) in let t = attempt() in {update_pass = (fun (oldp:passwd) (newp:passwd) -> if p.check(oldp) then (p.set(newp); t.reset()) else (t.increment(); raise wrong_pass)); retrieve = (fun (pass: passwd) (amt : int) -> if t.get() <5 then if p.check(pass) then if amt >=0 then if b.get() >= amt then ( b.decrease(amt) ;t.reset()) else raise not_enough_balance else raise negative_amount else (t.increment(); raise wrong_pass) else raise too_many_failures) ; deposit = (fun (pass :passwd) (amt:int) -> if t.get() < 5 then if p.check(pass) then if amt >= 0 then (b.increase(amt);t.reset()) else raise negative_amount else (t.increment(); raise wrong_pass) else raise too_many_failures); show_balance =(fun (pass :passwd) -> if t.get() <5 then if p.check(pass) then ( t.reset() ;b.get() ) else (t.increment(); raise wrong_pass) else raise too_many_failures) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper edge list = match edge with | ( a,b,c) :: xs -> if a = vertex then ( helper xs (List.append list [(b,c)])) else (helper xs list) | [] -> list 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) (sum: int): ('a list * weight) = let (n,w) = node in if List.mem n visited then raise Fail else if n = b then (List.rev (n::visited) , sum + w) else aux_list (neighbours g n) (n:: visited) (sum +w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum: int) : ('a list * weight) = match nodes with | [] -> raise Fail | (n2,w2) :: xs-> try aux_list xs (visited) (sum) with Fail -> aux_node (n2,w2)(visited) sum in aux_node (a,0) [] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (sum: int) fc sc: ('a list * weight) = let (n,w) = node in if List.mem n visited then fc() else if n = b then sc (List.rev (n::visited) , sum + w) else aux_list (neighbours g n) (n:: visited) (sum +w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sum: int) fc sc: ('a list * weight) = match nodes with | [] -> fc() | (n2,w2) :: xs-> let sc2 = sc in let fc2 = (fun () -> (aux_list xs (visited) (sum) fc sc ))in aux_node (n2,w2) (visited) sum fc2 sc2 in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun x ->x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec helper(all : ('a list * weight) list) (longest : 'a list) (maxcost: int) = match all with | [] -> if longest = [] then None else Some (longest,maxcost) | (l,w) ::xs-> if (w > maxcost) then helper xs l w else helper xs longest maxcost in helper (find_all_paths g a b) [] 0;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let attempts = ref 0 in let balance = ref 0 in { update_pass = (fun try_pass new_pass -> if try_pass = !pass then (pass := new_pass; attempts := 0) else (attempts := !attempts + 1; raise wrong_pass)); retrieve = (fun try_pass cash -> if !attempts >= 5 then raise too_many_failures else if try_pass = !pass then (if cash < 0 then (attempts := 0; raise negative_amount) else if cash > !balance then (attempts := 0; raise not_enough_balance) else (balance := !balance - cash; attempts := 0)) else (attempts := !attempts + 1; raise wrong_pass)); deposit = (fun try_pass cash -> if !attempts >= 5 then raise too_many_failures else if try_pass = !pass then (if cash < 0 then (attempts := 0; raise negative_amount) else (balance := !balance + cash; attempts := 0)) else (attempts := !attempts + 1; raise wrong_pass)); show_balance = (fun try_pass -> if !attempts >= 5 then raise too_many_failures else if try_pass = !pass then (attempts := 0; !balance) else (attempts := !attempts + 1; raise wrong_pass)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper_n edges v = match edges with | [] -> [] | (a,b,c)::xs -> if a = v then (b,c)::(helper_n xs v) else (helper_n xs v) in helper_n 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) = if List.mem (fst node) visited then raise Fail else if (fst node) = b then ([(fst node)], snd node) else ((fst node)::(fst (aux_list (neighbours g (fst node)) ((fst node)::visited))), (snd node) + (snd (aux_list (neighbours g (fst node)) ((fst node)::visited)))) 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)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented () let rec sum_weights g path weight = let rec match_edge edge x y = match edge with | (a,b,c)::_ when ((a = x) && (b = y)) -> c | _::d -> match_edge d x y | [] -> 0 in match path with | _::[] -> weight | x::xs -> sum_weights g xs (weight + (match_edge g.edges x (List.nth xs 0))) | [] -> 0 ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest all_paths curr_weight path = match all_paths with | [] -> if curr_weight = (-1) then None else Some path | x::xs -> if (snd x) >= curr_weight then longest xs (snd x) x else longest xs curr_weight path in longest (find_all_paths g a b) (-1) ([],0) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let money = ref 0 in let login_attempts = ref 0 in { update_pass = (fun old_password new_password -> if old_password = !password then (login_attempts := 0; password := new_password) else (login_attempts := !login_attempts+1; raise wrong_pass)); retrieve = (fun entered_password money_retrieved -> if !login_attempts < 5 then if entered_password = !password then if money_retrieved >= 0 then if !money >= money_retrieved then (money:=!money-money_retrieved; login_attempts:=0) else (login_attempts:=0; raise not_enough_balance) else (login_attempts:=0; raise negative_amount) else (login_attempts:=!login_attempts+1; raise wrong_pass) else raise too_many_failures); deposit = (fun entered_password money_added -> if !login_attempts < 5 then if entered_password = !password then if money_added >= 0 then (login_attempts:=0; money:=!money+money_added) else (login_attempts:=0; raise negative_amount) else (login_attempts:=!login_attempts+1; raise wrong_pass) else raise too_many_failures); show_balance = (fun entered_password -> if !login_attempts < 5 then if entered_password = !password then (login_attempts:=0; !money) else (login_attempts:=!login_attempts+1; raise wrong_pass) else raise too_many_failures); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun x -> fun (v1, v2, w) -> if (v1=vertex) then x@[(v2, w)] else 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) (cost : int) : ('a list * weight) = let (alpha, value) = node in if(alpha = b) then (visited@[b], cost + value) else if (List.mem alpha visited) then raise Fail else let new_visited = visited@[alpha] in aux_list (neighbours g alpha) (value+cost) (new_visited) and aux_list (nodes: ('a * weight) list) (cost : int) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> try (aux_node x visited cost) with Fail -> (aux_list xs cost visited) in aux_node (a,0) [] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (cost : int) fc sc : ('a list * weight)= let (alpha, value) = node in if(alpha = b) then sc (visited@[b], cost + value) else if (List.mem alpha visited) then fc () else let new_visited = visited@[alpha] in aux_list (neighbours g alpha) (value+cost) (new_visited) fc sc and aux_list (nodes: ('a * weight) list) (cost : int) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> aux_node x visited cost (fun () -> aux_list xs cost visited fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun x -> x);;
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 helper now next count = match next with | [] -> Some now | (alpha, value)::xs -> if (value > count) then helper (alpha, value) xs value else helper now xs count in match all_paths with | [] -> None | (x, y)::xs -> helper (x,y) xs y;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let password_count = ref 0 in let passwordChecker input f = if(input = !password) then ( password_count := 0; f ()) else( password_count := !password_count +1; raise wrong_pass) in let attemptChecker input f = if(!password_count >= 5) then raise too_many_failures else passwordChecker input f in let update_pass old_Pass new_Pass = passwordChecker old_Pass (fun () -> password := new_Pass) in let retrieve old_Pass amount2Retrieve = attemptChecker old_Pass (fun () -> if(amount2Retrieve < 0) then raise negative_amount else( if amount2Retrieve > !balance then raise not_enough_balance else balance := !balance - amount2Retrieve)) in let deposit old_Pass amount2Deposit = attemptChecker old_Pass (fun () -> if(amount2Deposit < 0) then raise negative_amount else balance := !balance + amount2Deposit); in let show_balance old_Pass = attemptChecker old_Pass (fun () -> !balance) in { update_pass; retrieve; deposit; show_balance } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun acc (a,b,weight) -> if(a = vertex) then (b, weight) :: 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 (id,weight) = node in if (id = b) then ([b], weight) else if List.mem id visited then raise Fail else( let (nodesOfPath, totalWeight) = aux_list (neighbours g id) (id :: visited) in (id :: nodesOfPath, weight + totalWeight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with |head :: tail -> ( try aux_node head visited with Fail -> aux_list tail 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 (id, weight) = node in if(id = b) then sc ([b], weight) else if List.mem id visited then fc () else aux_list (neighbours g id) (id :: visited) fc (fun (nodesOfPath, totalWeight) -> sc (id :: nodesOfPath, weight + totalWeight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |head :: tail -> ( (aux_node head visited (fun () -> aux_list tail visited fc sc)) sc) |[] -> fc () in aux_node (a, 0) [] (fun () -> raise Fail) (fun r -> r);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max_path allPaths = match allPaths with |[] -> None |[(path, totalWeight)] -> Some (path, totalWeight) |head :: tail -> ( let head_of_tail :: tail_of_tail = tail in let (id1, weight1) = head in let (id2, weight2) = head_of_tail in if(weight1 > weight2) then max_path (head :: tail_of_tail) else max_path tail) in max_path (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in { update_pass = (fun pwd new_password -> if pwd = !password then (attempts := 0; password := new_password) else (attempts := !attempts + 1; raise wrong_pass)); deposit = (fun pwd amount -> if !attempts >= 5 then raise too_many_failures else (if pwd = !password then (if amount < 0 then raise negative_amount else (balance := !balance + amount; attempts := 0)) else (attempts := !attempts + 1; raise wrong_pass))); retrieve = (fun pwd retrieve_amount -> if !attempts >= 5 then raise too_many_failures else if pwd = !password then (if retrieve_amount > !balance then raise not_enough_balance else if retrieve_amount < 0 then raise negative_amount else (balance := !balance - retrieve_amount; attempts := 0)) 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 = let rec helper graph = let l = graph.edges in match l with | [] -> [] | (v1, v2, w) :: tl -> if v1 <> vertex then helper {nodes = graph.nodes; edges = tl} else (helper {nodes = graph.nodes; edges = tl}) @ [(v2,w)] in helper g;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (visited : 'a list) (sum : int) : ('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 balance = ref 0 in let attempts = ref 0 in { update_pass = (fun pwd new_password -> if pwd = !password then (attempts := 0; password := new_password) else (attempts := !attempts + 1; raise wrong_pass)); deposit = (fun pwd amount -> if !attempts >= 5 then raise too_many_failures else (if pwd = !password then (if amount < 0 then raise negative_amount else (balance := !balance + amount; attempts := 0)) else (attempts := !attempts + 1; raise wrong_pass))); retrieve = (fun pwd retrieve_amount -> if !attempts >= 5 then raise too_many_failures else if pwd = !password then (if retrieve_amount > !balance then raise not_enough_balance else if retrieve_amount < 0 then raise negative_amount else (balance := !balance - retrieve_amount; attempts := 0)) 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 = let rec helper graph = let l = graph.edges in match l with | [] -> [] | (v1, v2, w) :: tl -> if v1 <> vertex then helper {nodes = graph.nodes; edges = tl} else (helper {nodes = graph.nodes; edges = tl}) @ [(v2,w)] in helper g;;