text
stringlengths
0
601k
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight): ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if (v = b) then (visited @ [v], acc + w) else aux_list (neighbours g v ) (visited @ [v]) (acc + w); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited acc with Fail -> aux_list t visited acc 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) (acc : weight) fc sc : ('a list * weight)= let (v , w) = node in if (List.mem v visited ) then fc () else if (v = b) then sc (visited @ [v], acc+w) else aux_list (neighbours g v) (visited@[v]) (acc+w) fc sc; and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> aux_node h visited acc (fun () -> aux_list t visited acc fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun (l,w) -> (l,w));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (longest) (l,w) = match longest with | None -> Some(l,w) | Some(longestPath, maxWeight) -> if (w >= maxWeight) then Some(l,w) else Some(longestPath, maxWeight) in List.fold_left f None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 and password = ref initial_pass and attempts = ref 0 in let check_passwd (pass: passwd): bool = if (!attempts <= 5) then if (pass = !password) then (attempts := 0; true) else (attempts := !attempts+1; raise wrong_pass) else raise too_many_failures in { update_pass = (fun (oldP: passwd) (newP: passwd) -> if (check_passwd oldP) then if (oldP = !password) then (password := newP; attempts := 0) else (attempts := !attempts+1; raise wrong_pass)) ; retrieve = (fun (pass: passwd) (amount: int) -> if (check_passwd pass) then if (amount < 0) then raise negative_amount else if (amount <= !balance) then balance := !balance - amount else raise not_enough_balance) ; deposit = (fun (pass: passwd) (amount: int) -> if (check_passwd pass) then if (amount < 0) then raise negative_amount else balance := !balance + amount) ; show_balance = (fun (pass: passwd) -> if (check_passwd pass) then !balance else raise too_many_failures); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbors edge -> let (v1,v2,w) = edge in if v1 = vertex then neighbors @ [(v2,w)] else neighbors ) [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (acc : weight): ('a list * weight) = let (v, w) = node in if (List.mem v visited) then raise Fail else if (v = b) then (visited @ [v], acc + w) else aux_list (neighbours g v ) (visited @ [v]) (acc + w); and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try aux_node h visited acc with Fail -> aux_list t visited acc 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) (acc : weight) fc sc : ('a list * weight)= let (v , w) = node in if (List.mem v visited ) then fc () else if (v = b) then sc (visited @ [v], acc+w) else aux_list (neighbours g v) (visited@[v]) (acc+w) fc sc; and aux_list (nodes: ('a * weight) list) (visited: 'a list) (acc : weight) fc sc : ('a list * weight) = match nodes with | [] -> fc () | h::t -> aux_node h visited acc (fun () -> aux_list t visited acc fc sc) sc in aux_node (a,0) [] 0 (fun () -> raise Fail) (fun (l,w) -> (l,w));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (longest) (l,w) = match longest with | None -> Some(l,w) | Some(longestPath, maxWeight) -> if (w >= maxWeight) then Some(l,w) else Some(longestPath, maxWeight) in List.fold_left f None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let pass = ref initial_pass in let tries = ref 0 in { show_balance = (fun(input : passwd) -> if(5<=(!tries)) then raise too_many_failures else if((compare input !pass)=0) then !balance else raise wrong_pass; ); update_pass = (fun(input : passwd)(newpass : passwd) -> if((compare input !pass)=0) then (pass := newpass; tries := 0) else raise wrong_pass); deposit = (fun(input : passwd)(amount : int) -> if(5<=(!tries)) then raise too_many_failures else if((compare input !pass)=0) then (if(amount<0) then raise negative_amount else balance := !balance + amount) else raise wrong_pass); retrieve = (fun(input : passwd)(amount : int) -> if(5<=(!tries)) then raise too_many_failures else if((compare input !pass)=0) then (if(!balance < amount) then raise not_enough_balance else (if(amount<0) then raise negative_amount else balance := !balance - amount)) else raise wrong_pass); } ;; let origin (edge: 'a * 'a * weight): 'a = let (origin, dest, wei) = edge in origin ;; let destination (edge: 'a * 'a * weight): 'a = let (origin, dest, wei) = edge in dest ;; let weight (edge: 'a * 'a * weight): int = let (origin, dest, wei) = edge in wei ;; let l_map (edge: 'a * 'a * weight): ('a * weight)= let (origin, dest, wei) = edge in (dest, wei) ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rel_edge = List.filter(fun edge -> ((compare (origin edge) vertex) = 0)) g.edges in List.map(l_map) rel_edge ;; let ending (endcase : 'a list * weight) : ('a list * weight) = endcase ;; let exit (list: 'a list * weight) : ('a list * weight)= list exception NotFound;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) (sofar : 'a list * weight) : ('a list * weight) = let vertex = fst node in let new_weight = snd node in let adjacent = neighbours g vertex in let sofar_list = fst sofar in let sofar_wt = snd sofar in let new_list = sofar_list@[vertex] in let new_wt = sofar_wt + new_weight in let new_sofar = (new_list, new_wt) in let new_visited = visited@[vertex] in if((compare vertex b) = 0) then exit new_sofar else aux_list adjacent new_visited new_sofar and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sofar : 'a list * weight) : ('a list * weight) = if(List.length nodes = 0) then raise Fail else let valid_cont = List.filter(fun node -> List.for_all(fun visit -> not(((compare visit (fst node))) = 0)) visited ) nodes in if(List.exists(fun node -> ((compare (fst node) b) = 0)) valid_cont) then let target = List.filter(fun adj -> ((compare b (fst adj)) = 0)) valid_cont in let t_node = List.nth target 0 in aux_node t_node visited sofar else if(List.length valid_cont = 0) then raise Fail else let rec rec_search list = match list with | [] -> raise NotFound | head::body -> try aux_node head visited sofar; with NotFound -> rec_search body; in rec_search valid_cont 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) (sofar : 'a list * weight) : ('a list * weight) = let vertex = fst node in let new_weight = snd node in let adjacent = neighbours g vertex in let sofar_list = fst sofar in let sofar_wt = snd sofar in let new_list = sofar_list@[vertex] in let new_wt = sofar_wt + new_weight in let new_sofar = (new_list, new_wt) in let new_visited = visited@[vertex] in if((compare vertex b) = 0) then exit new_sofar else aux_list adjacent new_visited new_sofar and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sofar : 'a list * weight) : ('a list * weight) = if(List.length nodes = 0) then raise Fail else let valid_cont = List.filter(fun node -> List.for_all(fun visit -> not(((compare visit (fst node))) = 0)) visited ) nodes in if(List.exists(fun node -> ((compare (fst node) b) = 0)) valid_cont) then let target = List.filter(fun adj -> ((compare b (fst adj)) = 0)) valid_cont in let t_node = List.nth target 0 in aux_node t_node visited sofar else if(List.length valid_cont = 0) then raise Fail else let rec rec_search list = match list with | [] -> raise NotFound | head::body -> try aux_node head visited sofar; with NotFound -> rec_search body; in rec_search valid_cont in aux_node (a, 0) [] ([], 0);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec aux_node (node: 'a * weight) (visited : 'a list) (sofar : 'a list * weight) : ('a list * weight) = let vertex = fst node in let new_weight = snd node in let adjacent = neighbours g vertex in let sofar_list = fst sofar in let sofar_wt = snd sofar in let new_list = sofar_list@[vertex] in let new_wt = sofar_wt + new_weight in let new_sofar = (new_list, new_wt) in let new_visited = visited@[vertex] in if((compare vertex b) = 0) then exit new_sofar else aux_list adjacent new_visited new_sofar and aux_list (nodes: ('a * weight) list) (visited: 'a list) (sofar : 'a list * weight) : ('a list * weight) = if(List.length nodes = 0) then ([], 0) else let valid_cont = List.filter(fun node -> List.for_all(fun visit -> not(((compare visit (fst node))) = 0)) visited ) nodes in if(List.exists(fun node -> ((compare (fst node) b) = 0)) valid_cont) then let target = List.filter(fun adj -> ((compare b (fst adj)) = 0)) valid_cont in let t_node = List.nth target 0 in aux_node t_node visited sofar else if(List.length valid_cont = 0) then ([], 0) else let rec rec_search list = match list with | [] -> raise NotFound | head::body -> try aux_node head visited sofar; with NotFound -> rec_search body; in rec_search valid_cont in let results: (('a list * weight) list) = [] in results@[aux_node (a, 0) [] ([], 0)]; if((List.length results) = 1) then let result = (List.nth results 0) in if((List.length (fst result)) = 0) then None else Some (List.nth results 0) else let current_largest = 0 in let current_path = ([], 0) in let rec rec_search list = match list with | [] -> None | head::body -> let comp = (List.length (fst head)) in if(current_largest < comp) then let current_largest = compare in let current_path = head in rec_search body else rec_search body; in rec_search results;;;
let open_account (initial_pass: passwd) : bank_account = let pw = ref initial_pass in let money = ref 0 in let fc = ref 0 in { update_pass = ( fun (op : passwd) (np : passwd) -> if (op = !pw) then (fc := 0 ; pw := np) else (fc := !fc + 1 ; raise wrong_pass) ); retrieve = ( fun (op : passwd) (amt : int) -> if ((op = !pw) && (!fc < 5)) then ( (fc := 0); if (amt >= 0) then ( if (amt <= !money) then (money := !money - amt) else raise not_enough_balance ) else raise negative_amount ) else ((fc := !fc + 1) ; if (!fc <= 5) then (raise wrong_pass) else (raise too_many_failures) ) ); deposit = ( fun (op : passwd) (amt : int) -> if ((op = !pw) && (!fc < 5)) then ((fc := 0); if (amt >= 0) then (money := !money + amt) else raise negative_amount ) else ((fc := !fc + 1) ; if (!fc <= 5) then (raise wrong_pass) else (raise too_many_failures) ) ); show_balance = ( fun (op : passwd) -> if ((op = !pw) && (!fc < 5)) then ((fc := 0);!money) else ( (fc := !fc + 1); if (!fc <= 5) then (raise wrong_pass) else (raise too_many_failures) ) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f1 l (_,e2,w) = ((e2,w)::l) in let f2 (e1,_,_) = e1=vertex in List.fold_left f1 [] (List.filter f2 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 ns = neighbours g (fst node) in let ns_name = fst (List.split ns) in if (List.mem b ns_name) then ([(fst node);b],snd (List.find (fun x -> (let (name,_) = x in name=b)) ns)) else let rtn = (aux_list ns ((fst node)::visited)) in let fst_n = (List.hd (fst rtn)) in let fst_n_cost = snd (List.find (fun x -> (let (name,_) = x in name = fst_n)) ns) in ((fst node)::(fst rtn), (fst_n_cost+(snd rtn))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | hd::tl -> if (List.mem (fst hd) visited) then aux_list tl visited else try aux_node hd visited with Fail -> aux_list tl visited in if a=b then ([a],0) else 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 ns = neighbours g (fst node) in let ns_name = fst (List.split ns) in if (List.mem b ns_name) then let sc_result = sc () in ((List.append (fst sc_result) [(fst node);b]), (snd sc_result)+(snd (List.find (fun x -> (let (name,_) = x in name=b)) ns))) else aux_list ns ((fst node)::visited) fc (fun () -> ((List.append (fst (sc ())) [(fst node)]),snd (sc ()))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | hd::tl -> if (List.mem (fst hd) visited) then (aux_list tl visited fc sc) else aux_node hd visited (fun () -> aux_list tl visited fc sc) (fun () -> ((fst (sc ())),(snd (sc()))+(snd hd))) in if a=b then ([a],0) else aux_node (a,0) [] (fun () -> raise Fail) (fun () -> ([],0)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let path = find_all_paths g a b in if path=[] then None else let path_sorted = List.sort (fun a b -> ((snd b)-(snd a))) path in Some (List.hd path_sorted) ;;
let open_account (initial_pass: passwd) : bank_account = let curpasswd= ref initial_pass in let balance= ref 0 in let failtime= ref 0 in {update_pass = (fun(old_pass: passwd) (new_pass: passwd) -> if (String.equal old_pass !curpasswd) then (curpasswd := new_pass; failtime:= 0) else (failtime:=!failtime+1; raise wrong_pass) ); retrieve = (fun (pass: passwd) (amt: int) -> if !failtime<5 then ( if (String.equal pass !curpasswd) then( if !balance<0 ||amt<0 then (failtime:= 0;raise negative_amount) else if !balance<amt && !balance>=0 then (failtime:= 0;raise not_enough_balance) else (balance:=!balance-amt;failtime:= 0)) else (failtime:=!failtime+1; raise wrong_pass)) else raise too_many_failures ); deposit= (fun (pass: passwd) (amt: int) -> if !failtime<5 then ( if (String.equal pass !curpasswd) then ( if !balance<0||amt<0 then (failtime:= 0;raise negative_amount) else balance:=!balance+amt;failtime:= 0) else (failtime:=!failtime+1; raise wrong_pass)) else raise too_many_failures ); show_balance= (fun (pass: passwd) -> if !failtime<5 then ( if not (String.equal pass !curpasswd) then (failtime:=!failtime+1; raise wrong_pass) else failtime:= 0;(if !balance<0 then raise negative_amount else !balance;)) else raise too_many_failures); } ;; let checknb (vertex: 'a) (node:('a * 'a * weight)) (k:(string * weight) list) = let (v1,v2,w)=node in if v1=vertex then (v2,w)::k else k;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (checknb 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) = let nbs=List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) (neighbours g (fst node)) in if List.length nbs=0 then raise Fail else (if List.exists (fun x->(fst x)=b) nbs then ((List.rev (b::(fst node)::visited)), (snd node)+(snd (List.find (fun x->(fst x)=b) (nbs)))) else aux_list (List.map (fun x->((fst x),(snd node +snd x))) nbs) ((fst node)::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let nodes1= List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) nodes in match nodes1 with |[]->raise Fail |x::h->try aux_node x visited with Fail ->aux_list h (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 nbs=List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) (neighbours g (fst node)) in if List.length nbs=0 then fc() else (if List.exists (fun x->(fst x)=b) nbs then sc ((List.rev (b::(fst node)::visited)), (snd node)+(snd (List.find (fun x->(fst x)=b) (nbs)))) else (let sc2=sc in let fc2= fun () -> (aux_list (List.tl (List.map (fun x->((fst x),(snd node +snd x))) nbs)) ((fst node)::visited) fc sc) in aux_list (List.map (fun x->((fst x),(snd node +snd x))) nbs) ((fst node)::visited) fc sc)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let nodes1= List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) nodes in match nodes1 with |[]->fc() |x::h-> if List.hd (List.rev (fst (aux_node x visited fc sc))) = b then sc (aux_node x visited fc sc) else aux_list h (visited) fc 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 choices:('a list * weight) list =find_all_paths g a b in let rec aux list acc = match list with |[]->None |[x]->if (snd x)>(snd acc) then Some x else Some acc |x::h->(if (snd x)>(snd acc) then (aux h x) else (aux h acc)) in aux choices ([a],0);;
let open_account (initial_pass: passwd) : bank_account = let curpasswd= ref initial_pass in let balance= ref 0 in let failtime= ref 0 in {update_pass = (fun(old_pass: passwd) (new_pass: passwd) -> if (String.equal old_pass !curpasswd) then (curpasswd := new_pass; failtime:= 0) else (failtime:=!failtime+1; raise wrong_pass) ); retrieve = (fun (pass: passwd) (amt: int) -> if !failtime<5 then ( if (String.equal pass !curpasswd) then( if !balance<0 ||amt<0 then (failtime:= 0;raise negative_amount) else if !balance<amt && !balance>=0 then (failtime:= 0;raise not_enough_balance) else (balance:=!balance-amt;failtime:= 0)) else (failtime:=!failtime+1; raise wrong_pass)) else raise too_many_failures ); deposit= (fun (pass: passwd) (amt: int) -> if !failtime<5 then ( if (String.equal pass !curpasswd) then ( if !balance<0||amt<0 then (failtime:= 0;raise negative_amount) else balance:=!balance+amt;failtime:= 0) else (failtime:=!failtime+1; raise wrong_pass)) else raise too_many_failures ); show_balance= (fun (pass: passwd) -> if !failtime<5 then ( if not (String.equal pass !curpasswd) then (failtime:=!failtime+1; raise wrong_pass) else failtime:= 0;(if !balance<0 then raise negative_amount else !balance;)) else raise too_many_failures); } ;; let checknb (vertex: 'a) (node:('a * 'a * weight)) (k:(string * weight) list) = let (v1,v2,w)=node in if v1=vertex then (v2,w)::k else k;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (checknb 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) = let nbs=List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) (neighbours g (fst node)) in if List.length nbs=0 then raise Fail else (if List.exists (fun x->(fst x)=b) nbs then ((List.rev (b::(fst node)::visited)), (snd node)+(snd (List.find (fun x->(fst x)=b) (nbs)))) else aux_list (List.map (fun x->((fst x),(snd node +snd x))) nbs) ((fst node)::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let nodes1= List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) nodes in match nodes1 with |[]->raise Fail |x::h->try aux_node x visited with Fail ->aux_list h (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 nbs=List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) (neighbours g (fst node)) in if List.length nbs=0 then (if List.length visited=0 then raise Fail else fc()) else (if List.exists (fun x->(fst x)=b) nbs then sc ((List.rev (b::(fst node)::visited)), (snd node)+(snd (List.find (fun x->(fst x)=b) (nbs)))) else ( let sc2=sc in let fc2= fun () -> (aux_list (List.tl (List.map (fun x->((fst x),(snd node +snd x))) nbs)) ((fst node)::visited) fc sc) in aux_node (List.hd (List.map (fun x->((fst x),(snd node +snd x))) nbs)) ((fst node)::visited) fc2 sc )) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let nodes1= List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) nodes in match nodes1 with |[]->fc() |x::h->(let sc2= sc in let fc2=fun() -> aux_list h visited fc sc in aux_list nodes1 visited fc2 sc2) 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 curpasswd= ref initial_pass in let balance= ref 0 in let failtime= ref 0 in {update_pass = (fun(old_pass: passwd) (new_pass: passwd) -> if (String.equal old_pass !curpasswd) then (curpasswd := new_pass; failtime:= 0) else (failtime:=!failtime+1; raise wrong_pass) ); retrieve = (fun (pass: passwd) (amt: int) -> if !failtime<5 then ( if (String.equal pass !curpasswd) then( if !balance<0 ||amt<0 then (failtime:= 0;raise negative_amount) else if !balance<amt && !balance>=0 then (failtime:= 0;raise not_enough_balance) else (balance:=!balance-amt;failtime:= 0)) else (failtime:=!failtime+1; raise wrong_pass)) else raise too_many_failures ); deposit= (fun (pass: passwd) (amt: int) -> if !failtime<5 then ( if (String.equal pass !curpasswd) then ( if !balance<0||amt<0 then (failtime:= 0;raise negative_amount) else balance:=!balance+amt;failtime:= 0) else (failtime:=!failtime+1; raise wrong_pass)) else raise too_many_failures ); show_balance= (fun (pass: passwd) -> if !failtime<5 then ( if not (String.equal pass !curpasswd) then (failtime:=!failtime+1; raise wrong_pass) else failtime:= 0;(if !balance<0 then raise negative_amount else !balance;)) else raise too_many_failures); } ;; let checknb (vertex: 'a) (node:('a * 'a * weight)) (k:(string * weight) list) = let (v1,v2,w)=node in if v1=vertex then (v2,w)::k else k;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (checknb 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) = let nbs=List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) (neighbours g (fst node)) in if List.length nbs=0 then raise Fail else (if List.exists (fun x->(fst x)=b) nbs then ((List.rev (b::(fst node)::visited)), (snd node)+(snd (List.find (fun x->(fst x)=b) (nbs)))) else aux_list (List.map (fun x->((fst x),(snd node +snd x))) nbs) ((fst node)::visited)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = let nodes1= List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) nodes in match nodes1 with |[]->raise Fail |x::h->try aux_node x visited with Fail ->aux_list h (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 nbs=List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) (neighbours g (fst node)) in if List.length nbs=0 then (if List.length visited=0 then raise Fail else fc()) else (if List.exists (fun x->(fst x)=b) nbs then sc ((List.rev (b::(fst node)::visited)), (snd node)+(snd (List.find (fun x->(fst x)=b) (nbs)))) else ( let sc2=sc in let fc2= fun () -> (aux_list (List.tl (List.map (fun x->((fst x),(snd node +snd x))) nbs)) ((fst node)::visited) fc sc) in aux_node (List.hd (List.map (fun x->((fst x),(snd node +snd x))) nbs)) ((fst node)::visited) fc2 sc )) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = let nodes1= List.filter (fun x -> not (List.exists (fun y->(fst x)=y) visited)) nodes in match nodes1 with |[]->fc() |x::h->(let sc2= sc in let fc2=fun() -> aux_list h visited fc sc in aux_list nodes1 visited fc2 sc2) 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 and balance = ref 0 and wrong = ref 0 in {update_pass = (fun old_pass new_pass -> if !password = old_pass then (wrong := 0; password := new_pass) else (wrong := !wrong + 1; raise wrong_pass) ); retrieve = (fun pass amount -> if !wrong < 5 then if !password <> pass then (wrong := !wrong + 1; raise wrong_pass) else (wrong := 0; if amount <0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount) else raise too_many_failures); deposit = (fun pass amount -> if !wrong < 5 then if !password <> pass then (wrong := !wrong + 1; raise wrong_pass) else (wrong := 0; if amount <0 then raise negative_amount else balance := !balance + amount) else raise too_many_failures); show_balance =(fun pass -> if !wrong < 5 then if !password <> pass then (wrong := !wrong + 1; raise wrong_pass) else (wrong := 0; !balance) else raise too_many_failures )} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun nb_list (start,dest,weight) -> if vertex = start then (dest, weight) :: nb_list else nb_list) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((vertex, v_weight): 'a * weight) (visited : 'a list) : ('a list * weight) = if vertex = b then ((vertex::[]),v_weight) else try let (path_list, cost) = aux_list (neighbours g vertex) (vertex :: visited) in (vertex :: path_list, v_weight + cost) with Fail -> raise Fail and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | (vertex, v_weight) :: [] -> if List.mem vertex visited then raise Fail else aux_node (vertex, v_weight) visited | (vertex, v_weight) :: tl -> ( if List.mem vertex visited then aux_list tl visited else try aux_node (vertex, v_weight) visited with Fail -> aux_list tl visited ) | [] -> raise Fail in aux_node (a,0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((vertex, v_weight): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if vertex = b then sc ((vertex :: []),v_weight) else (let sc2 = (fun (path_list,cost) -> sc (vertex :: path_list,v_weight+cost)) in aux_list (neighbours g vertex) (vertex :: visited) fc sc2 ) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | (vertex, v_weight) :: [] -> if List.mem vertex visited then fc () else aux_node (vertex, v_weight) visited fc sc | (vertex, v_weight) :: tl -> ( if List.mem vertex visited then aux_list tl visited fc sc else let fc2 = fun () -> aux_list tl visited fc sc in aux_node (vertex, v_weight) visited fc2 sc ) | [] -> fc () in aux_node (a,0) [] (fun () -> raise Fail) (fun (path_list,cost) -> (path_list,cost));;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = (find_all_paths g a b) in let rec find_max path_list (cur_path, cur_cost) : ('a list * weight) = match path_list with | (path, cost) :: tl -> if cost > cur_cost then find_max tl (path, cost) else find_max tl (cur_path, cur_cost) | [] -> (cur_path, cur_cost) in match all_paths with | [] -> None | _ -> Some (find_max all_paths ([], 0));;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass and balance = ref 0 and wrong_num = ref 0 in { update_pass = (fun old_pass new_pass -> if !pass = old_pass then begin pass := new_pass; wrong_num := 0 end else begin wrong_num := wrong_num.contents + 1; raise wrong_pass end); retrieve = (fun try_pass num -> if !wrong_num >= 5 then raise too_many_failures else if !pass = try_pass then begin wrong_num := 0; if num < 0 then raise negative_amount else if num > balance.contents then raise not_enough_balance else balance := balance.contents - num end else begin wrong_num := wrong_num.contents + 1; raise wrong_pass end); deposit = (fun try_pass num -> if !wrong_num >= 5 then raise too_many_failures else if !pass = try_pass then begin wrong_num := 0; if num < 0 then raise negative_amount else balance := balance.contents + num end else begin wrong_num := wrong_num.contents + 1; raise wrong_pass end); show_balance = (fun try_pass -> if !wrong_num >= 5 then raise too_many_failures else if !pass = try_pass then begin wrong_num := 0; balance.contents end else begin wrong_num := wrong_num.contents + 1; raise wrong_pass end) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = fun l (node_a, node_b, w) -> if node_a = vertex then (node_b, w)::l else l in List.fold_left f [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((n, w): 'a * weight) (visited : 'a list) : ('a list * weight) = if n = b then (List.rev (b::visited), w) else aux_list (List.map (fun (n, w') -> (n, w'+w)) (neighbours g n)) (n::visited) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (n, w)::rest -> if List.mem n visited then aux_list rest visited else try aux_node (n, w) visited with Fail -> aux_list rest visited in aux_node (a, 0) [];;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node ((n, w): 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= if n = b then sc (List.rev (b::visited), w) else sc (aux_list (List.map (fun (n, w') -> (n, w'+w)) (neighbours g n)) (n::visited) fc sc) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | (n, w)::rest -> if List.mem n visited then sc (aux_list rest visited fc sc) else let s2 = sc in let f2 = fun () -> aux_list rest visited fc sc in aux_node (n, w) visited f2 s2 in aux_node (a, 0) [] (fun () -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with |[] -> None |(path, w)::rest -> let rec helper (path, w) rest = match rest with | [] -> (path, w) | (path', w')::rest' -> if w' > w then helper (path', w') rest' else helper (path, w) rest' in Some (helper (path, w) rest);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let attempts = ref 0 in let check pass f = if pass = !password then (attempts := 0; f ()) else (incr attempts; raise wrong_pass) in let attempt pass f = if !attempts >= 5 then raise too_many_failures else check pass f in let update_pass old new_pass = check old (fun () -> password := new_pass) in let deposit pass money = attempt pass (fun () -> if money < 0 then raise negative_amount else balance := !balance + money) in let show_balance pass = attempt pass (fun () -> !balance) in let retrieve pass money = attempt pass (fun () -> if money < 0 then raise negative_amount else if !balance >= money then balance := !balance - money else raise not_enough_balance) in {update_pass; deposit; retrieve; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left(fun acc (v1,v2,w)-> if v1 = vertex then (v2,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) = if fst node = b then ([b], snd node) else if List.mem (fst node) visited then raise Fail 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 |(n,w) :: ns -> try aux_node (n,w) visited with Fail -> aux_list ns 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)= if fst node = b then sc ([b], snd node) else if List.mem (fst node) visited then fc () else aux_list (neighbours g (fst node)) ((fst node) :: visited) fc (fun (p,c) -> sc ((fst node) :: p, (snd node) + c)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |n :: ns -> aux_node n visited (fun () -> aux_list ns visited fc sc ) sc in aux_node (a,0) [] (fun () -> raise Fail) (fun a -> a);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec longest = function |[] -> None |[(c, d)] -> Some (c, d) |(e, f) :: (g, h) :: tl -> if f > h then longest ((e, f) :: tl) else longest ((g, h) :: tl) in longest (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass; in let fail_count = ref 0; in let show_balance = ref 0; in { update_pass = (fun old_password -> fun new_password -> if old_password <> !password then (fail_count := !fail_count + 1; raise wrong_pass ) else ((password := new_password); fail_count := 0 ) ); retrieve = (fun old_password -> fun get -> if (!fail_count >= 5) then raise too_many_failures; if old_password <> !password then (fail_count := !fail_count + 1; raise wrong_pass ) else (fail_count := 0; if get < 0 then raise negative_amount; if !show_balance >= get then show_balance := !show_balance - get else raise not_enough_balance) ); deposit = (fun old_password -> fun put -> if !fail_count >= 5 then raise too_many_failures; if old_password <> !password then (fail_count := !fail_count + 1; raise wrong_pass ) else (fail_count := 0; if put < 0 then raise negative_amount; show_balance := !show_balance + put) ); show_balance = (fun old_password -> if !fail_count >= 5 then raise too_many_failures; if old_password <> !password then (fail_count := !fail_count + 1; raise wrong_pass ) else (fail_count := 0; !show_balance) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let fold (pair: 'a * 'a * weight) ls = match pair with | (x, y, z) when x = vertex -> (y, z) :: ls | _ -> ls in List.fold_right fold 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 cur_node, w = node in if cur_node = b then (b :: [], w) else if List.mem cur_node visited then raise Fail else let path, total_weight = aux_list (neighbours g cur_node) (cur_node :: visited) in (cur_node :: path, w + total_weight) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: lst -> try aux_node x visited with Fail -> aux_list lst 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 cur_node, w = node in if cur_node = b then sc (b :: [], w) else if List.mem cur_node visited then fc () else aux_list (neighbours g cur_node) (cur_node :: visited) fc (fun (path, total_weight) -> sc (cur_node :: path, w + total_weight)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x :: lst -> try aux_node x visited (fun () -> aux_list lst visited fc sc) sc with Fail -> aux_list lst visited fc sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun l -> l);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec max l = match l with | [] -> None | [(x, y)] -> Some (x, y) | (x, y) :: (a, b) :: lst -> if y >= b then max ((x, y) :: lst) else max ((a, b) :: lst) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let hidden_password = ref initial_pass in let balance = ref 0 in let counter = ref 0 in let authenticator (curr_pass) : unit = if (curr_pass = !hidden_password) && (!counter < 5) then (counter := 0) else if (!counter < 5) then (counter := !counter + 1; raise wrong_pass) else (counter := !counter + 1; raise too_many_failures) in let authenticator_up_pass (curr_pass) : unit = if curr_pass = !hidden_password then counter := 0 else (counter := !counter + 1; raise wrong_pass) in let update_aux (old_pass : passwd) (new_pass : passwd) : unit = authenticator_up_pass old_pass; hidden_password := new_pass in let deposit_aux (curr_pass : passwd) (amount : int) : unit = authenticator curr_pass; if amount < 0 then raise negative_amount else balance := (!balance + amount) in let retrieve_aux (curr_pass : passwd) (amount : int) : unit = authenticator curr_pass; if amount < 0 then raise negative_amount else if !balance >= amount then balance := (!balance - amount) else raise not_enough_balance in let show_bal_aux (curr_pass : passwd) : int = authenticator curr_pass; !balance in let this_bank_acc = { update_pass = update_aux; retrieve = retrieve_aux; deposit = deposit_aux; show_balance = show_bal_aux } in this_bank_acc ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun x xs -> match x with |(u, v, w) when (u = vertex) -> (v, w) :: xs |_ -> xs ) 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) (total_w: weight) : ('a list * weight) = let (name, weight) = node in if name = b then (List.rev (name :: visited), (weight + total_w)) else if List.exists ((=) name) visited then raise Fail else aux_list (neighbours g name) (name :: visited) (weight + total_w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (total_w: weight) : ('a list * weight) = match nodes with | [] -> raise Fail | x :: xs -> try aux_node x visited total_w with Fail -> aux_list xs visited total_w 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) fc sc: ('a list * weight)= let (name, weight) = node in if name = b then sc (List.rev (name :: visited)) (weight) else if List.exists ((=) name) visited then fc () else let sc2 = (fun x y -> sc x (y + weight)) in aux_list (neighbours g name) (name :: 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 x y -> (x, y)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match (find_all_paths g a b) with | [] -> None | x::xs-> Some (List.fold_right (fun el acc -> let (_,el_weight) = el in let (_, acc_weight) = acc in if el_weight > acc_weight then el else acc) xs x) ;;
let open_account (initial_pass: passwd) : bank_account = let curr_pass_ref = ref initial_pass in let count = ref 0 in let balance = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass <> !curr_pass_ref then (count := !count + 1; raise wrong_pass) else (count := 0; curr_pass_ref := new_pass)); deposit = (fun pass amount -> if pass <> !curr_pass_ref then if !count > 5 then raise too_many_failures else (count := !count + 1; raise wrong_pass); if pass = !curr_pass_ref then if amount < 0 then raise negative_amount else (count := 0; balance := !balance + amount)); retrieve = (fun pass amount -> if pass <> !curr_pass_ref then if !count > 5 then raise too_many_failures else (count := !count + 1; raise wrong_pass); if pass = !curr_pass_ref then if amount > !balance then raise not_enough_balance else if amount < 0 then raise negative_amount else (count := 0; balance := !balance - amount)); show_balance = (fun pass -> if pass <> !curr_pass_ref then if !count > 5 then raise too_many_failures else (count := !count + 1; raise wrong_pass) else (count := 0; !balance)); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun neighbors edge -> (let (v1, v2, w) = edge in if v1 = vertex then (v2, w) :: neighbors else neighbors)) [] g.edges;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([b], 0) else let rec aux_node node visited total : ('a list * weight) = match neighbours g node with | [] -> raise Fail | _ -> aux_list (neighbours g node) visited total and aux_list nodes visited total : ('a list * weight) = match nodes with | [] -> raise Fail | (vt, wt)::tl -> (if vt = b then ((visited @ [vt]), (total + wt)) else if List.mem vt visited then aux_list tl visited total else try aux_node vt (visited @ [vt]) (total + wt) with Fail -> aux_list tl visited total) in aux_node a [a] 0;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([b], 0) else let rec aux_node node visited total fc sc : ('a list * weight) = match neighbours g node with | [] -> fc () | _ -> aux_list (neighbours g node) visited total fc sc and aux_list nodes visited total fc sc : ('a list * weight) = match nodes with | [] -> fc () | (vt, wt)::tl -> (if vt = b then sc ((visited @ [vt]), (total + wt)) else if List.mem vt visited then aux_list tl visited total fc sc else (aux_node vt (visited @ [vt]) (total + wt) (fun () -> aux_list tl visited total fc sc) sc)) in aux_node a [a] 0 (fun () -> raise Fail) (fun x -> x);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | hd::tl -> (let find_max max curr = let (_, curr_wt), (_, max_wt) = curr, max in if curr_wt > max_wt then curr else max in Some (List.fold_left find_max hd tl));;
let makeCounter () = let localCount = ref 0 in { increase = (fun () -> incr localCount; !localCount) ; reset = (fun () -> localCount := 0) } type account = { mutable password : passwd; mutable balance : int; mutable failed_attempts : int; mutable accountNotLocked : bool };;
let open_account (initial_pass: passwd) : bank_account = let account = {password = initial_pass; balance = 0; failed_attempts = 0; accountNotLocked = true} in let pass_check pass = if account.failed_attempts >= 5 then raise too_many_failures else if pass = account.password then (account.failed_attempts <- 0; true) else (account.failed_attempts <- (account.failed_attempts + 1); if account.failed_attempts = 5 then (account.accountNotLocked <- false; raise wrong_pass) else false) in let update_pass old_pass new_pass = if old_pass = account.password then (account.password <- new_pass; account.failed_attempts <- 0; account.accountNotLocked <- true) else raise wrong_pass in let retrieve pass amount = if pass_check pass then if amount < 0 then raise negative_amount else if account.balance >= amount then account.balance <- (account.balance - amount) else raise not_enough_balance else raise wrong_pass in let deposit pass amount = if pass_check pass then if amount < 0 then raise negative_amount else account.balance <- (account.balance + amount) else raise wrong_pass in let show_balance pass = if pass_check pass then account.balance else raise wrong_pass in {update_pass; retrieve; deposit; show_balance} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = function | (a, b, w) -> a = vertex in let new_list = List.filter f g.edges in List.map (fun (a, b, w) -> (b, w)) new_list ;; let rec reaches_dest neighbour_nodes_weights dest = match neighbour_nodes_weights with | [] -> [] | (node, w)::xs -> if node = dest then [(node, w)] else reaches_dest xs dest ;; let remove_ones ones graph = let h = function | (a, w) -> not (List.mem a ones) in List.filter h graph ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([a; b], 0) else let visited_nodes = a::[] in let rec aux edges visited acc = if reaches_dest edges b != [] then ( let x = List.nth (reaches_dest edges b) 0 in let (l,w) = x in (List.rev (l::visited), w + acc) ) else match edges with | [] -> raise Fail | (l, w)::_ -> let next_order_neighbours = remove_ones visited (neighbours g l) in aux next_order_neighbours (l::visited) (acc + w) in aux (neighbours g a) visited_nodes 0 ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = if a = b then ([a; b], 0) else let visited_nodes = a::[] in let rec aux edges visited acc = if reaches_dest edges b != [] then ( let x = List.nth (reaches_dest edges b) 0 in let (l,w) = x in (List.rev (l::visited), w + acc) ) else match edges with | [] -> raise Fail | (l, w)::_ -> let next_order_neighbours = remove_ones visited (neighbours g l) in aux next_order_neighbours (l::visited) (acc + w) in aux (neighbours g a) visited_nodes 0 ;; let rec remove_last_edge edges bef_last last final = match edges with | [] -> (List.rev final) | (a', b', w)::tl -> if (a' = bef_last && b' = last) then remove_last_edge tl bef_last last final else remove_last_edge tl bef_last last ((a', b', w)::final) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = Some (find_path g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let attmp = ref 0 in let check_pass pwd f = if pwd = !pass then (attmp := 0; f ()) else (incr attmp; raise wrong_pass) in let attempt pwd f = if !attmp >= 5 then raise too_many_failures else check_pass pwd f in let update_pass old new_p = check_pass old (fun () -> pass := new_p) in let deposit pwd amt = if amt < 0 then raise negative_amount else attempt pwd (fun () -> balance := !balance + amt) in let show_balance pwd = attempt pwd (fun () -> !balance) in let retrieve pwd amt = attempt pwd (fun () -> if amt < 0 then raise negative_amount else if !balance >= amt then balance := !balance - amt else raise not_enough_balance) in {update_pass; deposit; show_balance; retrieve} ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (v1, v2, w) acc -> if v1 = vertex then (v2,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) acc: ('a list * weight) = let (u,w) = node in if List.exists (fun v -> u = v) visited then raise Fail else if u = b then (visited@[u], acc + w) else let nbs = neighbours g u in aux_list nbs (visited@[u]) (acc+w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) acc: ('a list * weight) = match nodes with | h::tail -> (try aux_node h visited acc with Fail -> aux_list tail visited acc) | [] -> raise Fail in try aux_list [(a,0)] [] 0 with Fail -> raise Fail;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (u,w) = node in if List.exists (fun v -> u = v) visited then fc () else if u = b then sc ([u], w) else let nbs = neighbours g u in aux_list nbs (u::visited) fc (fun (path, w' ) -> sc (u::path, w+w')) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | h::tail -> aux_node h visited (fun () -> aux_list tail visited fc sc) sc | [] -> fc () in aux_list [(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 l = match l with | [] -> None | [(p,w)] -> Some (p,w) | (p1,w1)::(p2,w2)::rest -> if w1 >=w2 then max ((p1,w1)::rest) else max ((p2,w2)::rest) in max (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let attempts = ref 0 in let total = ref 0 in let pswd = ref initial_pass in { update_pass = (fun old_pass new_pass -> if (old_pass <> !pswd ) then (attempts := !attempts +1; raise wrong_pass) else (attempts := 0; pswd := new_pass)); deposit = (fun x amount -> (if (!attempts >=5) then raise too_many_failures else if (x <> !pswd) then (attempts := !attempts + 1; raise wrong_pass) else (attempts := 0; total := !total + amount) )); retrieve = (fun x amount -> if (!attempts >=5) then raise too_many_failures else if (x <> !pswd) then (attempts := !attempts + 1; raise wrong_pass) else if (amount > !total) then (attempts := 0; raise negative_amount) else (attempts := 0; total := !total - amount) ); show_balance= (fun x -> if (!attempts >=5) then raise too_many_failures else if ( x <> !pswd) then (attempts := !attempts + 1; raise wrong_pass) else (attempts :=0; !total) ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun (v1, v2, w) acc -> if v1 = vertex then (v2,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 (c,w) = node in if c = b then ([b] , w) else if List.mem c visited then raise Fail else (let (path , cost) = aux_list (neighbours g c) (c :: visited) in (c :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | (v,w) :: vs -> try aux_node (v,w) visited with Fail -> aux_list vs 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 (c,w) = node in if c = b then sc ([b], w) else if List.mem c visited then fc () else aux_list (neighbours g c) (c :: visited) fc (fun (path, cost) -> sc (c :: path, cost + w)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with | [] -> fc () | v :: vs -> aux_node v visited (fun () -> aux_list vs visited fc sc) sc in aux_node (a, 0) [] (fun () -> raise Fail) (fun l -> l);;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let rec min l = match l with | [] -> None | [(p,w)] -> Some (p,w) | (p1,w1)::(p2,w2)::rest -> if w1 >= w2 then min ((p1,w1)::rest) else min ((p2,w2)::rest) in min (find_all_paths g a b);;
let open_account (initial_pass: passwd) : bank_account = let curr_pass = ref initial_pass in let balance = ref 0 in let bad_attempts = ref 0 in { update_pass = ( fun s1 -> fun s2 -> if s1 = !curr_pass then (bad_attempts := 0; curr_pass := s2 ) else (bad_attempts := !bad_attempts + 1; raise wrong_pass) ); retrieve = (fun s -> fun i -> if !bad_attempts >= 5 then raise too_many_failures else if s = !curr_pass then (bad_attempts := 0; if i > !balance then raise not_enough_balance else if i < 0 then raise negative_amount else balance := !balance - i ) else if s != !curr_pass then (bad_attempts := !bad_attempts + 1; raise wrong_pass ) ); deposit = (fun s -> fun i -> if !bad_attempts >= 5 then raise too_many_failures else if s = !curr_pass then ( bad_attempts := 0; if i >= 0 then balance := !balance + i else raise negative_amount ) else (bad_attempts := !bad_attempts + 1; raise wrong_pass ) ); show_balance = (fun s -> if !bad_attempts >= 5 then raise too_many_failures else if s = !curr_pass then (bad_attempts := 0; !balance ) else (bad_attempts := !bad_attempts + 1; raise wrong_pass) ) } ;; let rec neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec is_neighbour v n e = match e with | [] -> 0 | (v',n',w) :: t -> if (v = v' && n = n' && v' != n' ) then w else is_neighbour v n t in match g with | {nodes = []; edges = e} -> [] | {nodes = h :: t; edges = e} -> let w = is_neighbour vertex h e in let new_g = {nodes = t; edges = e} in if w > 0 then let new_g = {nodes = t; edges = e} in (h,w) :: neighbours new_g vertex else neighbours new_g 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 (inital_pass : passwd) : bank_account = let cur_pass = ref inital_pass in let balance = ref 0 in let num_of_tries = ref 0 in { update_pass = (fun password new_pass -> if !cur_pass = password then begin num_of_tries := 0 ; cur_pass := new_pass end else begin num_of_tries := !num_of_tries + 1; raise wrong_pass end ); retrieve = (fun password amount -> if !cur_pass = password then if !num_of_tries >= 5 then raise too_many_failures else begin num_of_tries := 0; if amount < 0 then raise negative_amount else if amount > !balance then raise not_enough_balance else balance := !balance - amount end else if !num_of_tries >= 5 then raise too_many_failures else begin num_of_tries := !num_of_tries + 1; raise wrong_pass end ); deposit = (fun password amount -> if !cur_pass = password then if !num_of_tries >= 5 then raise too_many_failures else begin num_of_tries := 0; if amount < 0 then raise negative_amount else balance := !balance + amount end else if !num_of_tries >= 5 then raise too_many_failures else begin num_of_tries := !num_of_tries + 1; raise wrong_pass end ); show_balance = (fun password -> if !cur_pass = password then if !num_of_tries >= 5 then raise too_many_failures else begin num_of_tries := 0; !balance end else if !num_of_tries >= 5 then raise too_many_failures else begin num_of_tries := !num_of_tries + 1; raise wrong_pass end ); } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper acc x = let (in_vertex, out_vertex, weight) = x in if in_vertex = vertex then acc @ [(out_vertex, weight)] else acc in List.fold_left helper [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (cost: weight) (visited : 'a list) : ('a list * weight) = if node = b then (visited, cost) else aux_list (neighbours g node) cost visited and aux_list (nodes: ('a * weight) list) (cost: weight) (visited: 'a list) : ('a list * weight) = match nodes with | [] -> raise Fail | h::t -> try let (node, edgecost) = h in if List.mem node visited then raise Fail else aux_node node (cost + edgecost) (visited @ [node]) with Fail -> aux_list t cost visited in aux_node a 0 [a] ;;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a) (cost: 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 helper longest x = let (_, l_cost) = longest in let (_, x_cost) = x in if x_cost > l_cost then x else longest in match find_all_paths g a b with |[] -> None |paths -> Some (List.fold_left helper ([], -1) paths) ;;
let open_account (initial_pass: passwd) : bank_account = let truepass= ref initial_pass in let amount = ref 0 in let failures= ref 0 in { update_pass=(fun old_password new_password -> if old_password = (!truepass) then (truepass:=new_password; failures:=0) else (incr failures ;raise wrong_pass )); retrieve=(fun old_password retrieve_amount -> if !failures>=5 then (incr failures ; raise too_many_failures) else if old_password <>(!truepass) then (incr failures ;raise wrong_pass) else match retrieve_amount with |retrivec when retrivec <0 -> failures :=0 ; raise negative_amount |retrivec when retrivec >(!amount) -> failures := 0 ; raise not_enough_balance |_ -> (amount := (!amount)-retrieve_amount); failures := 0); deposit= (fun old_password deposit_amount -> if !failures>=5 then (incr failures ; raise too_many_failures) else if old_password <>(!truepass) then (incr failures ;raise wrong_pass) else if deposit_amount <0 then (failures:=0;raise negative_amount ) else (amount := (!amount)+deposit_amount); failures:=0); show_balance= (fun old_password -> if !failures>=5 then (incr failures ; raise too_many_failures) else if old_password = (!truepass) then ( failures:=0;!amount )else (incr failures; raise wrong_pass) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left (fun (x : ('a * weight) list) (edge: 'a * 'a * weight) -> match edge with |(a,b,c) when a =vertex ->(b,c)::x |_ -> 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) (path:'a list * weight) : ('a list * weight) = match node with |(c,d) when c=b-> let(x,y) = path in (x@[c],(d+y)) |(c,d) when List.mem c visited -> raise Fail |(c,d)-> aux_list (neighbours g c) (visited @[c]) (let (x,y) = path in ((x@[c]),(d+y))) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (path:'a list * weight) : ('a list * weight) = match nodes with |[] -> raise Fail |hd :: tl ->try aux_node hd visited path with Fail -> aux_list tl visited path in aux_list [(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) fc sc : ('a list * weight)= match node with |(c,d) when c=b-> sc node |(c,d) when List.mem c visited -> fc() |(c,d)-> aux_list (neighbours g c) (visited @[c]) fc (fun(x,y)-> let(p,q)=(sc node) in(p@[x],y+q)) and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = match nodes with |[] -> fc () |hd :: tl -> aux_node hd visited (fun()-> aux_list tl visited fc sc ) sc in aux_list [(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 paths= find_all_paths g a b in match paths with |[]->None |_-> Some (List.fold_left (fun(c:'a list * weight) (d:'a list * weight)-> let ((list1,weight1),(list2,weight2)) = (c, d) in if weight1 > weight2 then (list1, weight1) else (list2, weight2) ) ([],0) paths);;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let pw = ref initial_pass in let error = ref 0 in { update_pass = ( fun old_pw new_pw -> if old_pw = !pw then (error := 0; pw := new_pw) else (error := (!error + 1) ; raise wrong_pass) ) ; retrieve = ( fun in_pw amount -> if !error = 5 then raise too_many_failures else if in_pw = !pw then if amount < 0 then raise negative_amount else if !balance < amount then raise not_enough_balance else (error := 0; balance := (!balance - amount)) else (error := (!error + 1) ; raise wrong_pass) ) ; deposit = ( fun in_pw amount -> if !error = 5 then raise too_many_failures else if in_pw = !pw then if amount < 0 then raise negative_amount else (error := 0; balance := (!balance + amount)) else (error := (!error + 1) ; raise wrong_pass) ) ; show_balance = ( fun in_pw -> if !error = 5 then raise too_many_failures else if in_pw = !pw then (error := 0; !balance) else (error := (!error + 1) ; raise wrong_pass) ) } ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_right (fun x xs-> let (v1, v2, w) = x in if v1 = vertex then (v2, w)::xs else xs ) 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: int) : ('a list * weight) = let (v,w) = node in if v = b then (List.fold_left (fun xs x -> x::xs) [] (v::visited), weight + w) else if (neighbours g v) = [] then raise Fail else if List.mem v visited then raise Fail else aux_list (neighbours g v) (v::visited) (weight + w) and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight: int) : ('a list * weight) = match nodes with | [] -> raise Fail | x::xs -> let (v,w) = x in try aux_node (v, w) visited weight with Fail -> aux_list xs visited weight 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) (weight: int) fc sc : ('a list * weight)= let (v,w) = node in if v = b then sc (v, w) visited weight else if (neighbours g v) = [] then fc () else if List.mem v visited then fc () else aux_list (neighbours g v) (v::visited) (weight + w) fc sc and aux_list (nodes: ('a * weight) list) (visited: 'a list) (weight: int) fc sc : ('a list * weight) = match nodes with | [] -> fc () | x::xs -> let (v,w) = x in let sc2 = sc in let fc2 = fun () -> aux_list xs visited weight fc sc in aux_node (v, w) visited weight fc2 sc2 in aux_node (a, 0) [] 0 (fun () -> raise Fail) (fun (v,w) visited sum -> (List.fold_left (fun xs x -> x::xs) [] (v::visited), sum + w)) ;;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let list = find_all_paths g a b in if list = [] then None else Some (List.fold_left ( fun x y -> let (l1, w1) = x in let (l2, w2) = y in if w1 > w2 then x else y ) ([a], 0) list) ;;
let extract_pass (x1, x2, x3) = x1 let extract_balance (x1, x2, x3) = x2 let extract_times (x1, x2, x3) = x3;;
let open_account (initial_pass: passwd) : bank_account = let account = ref (initial_pass, 0, 0) in { update_pass = (fun p1 p2 -> let old_pass = (extract_pass !account) in let balance = (extract_balance !account) in let old_times = (extract_times !account) in let _ = print_endline (old_pass ^ " " ^ p1 ^ " " ^ p2) in if p1 = old_pass then (account := (p2, balance, 0); ()) else (account := (old_pass, balance, old_times + 1); raise wrong_pass) ); deposit= (fun pass amount -> let old_pass = (extract_pass !account) in let balance = (extract_balance !account) in let times = (extract_times !account) in if times >= 5 then raise too_many_failures else if pass = old_pass then if amount > 0 then (account:= (old_pass, balance + amount, 0); ()) else (account:= (old_pass, balance, 0); raise negative_amount) else if times + 1 > 5 then (account := (old_pass, balance, times + 1); raise too_many_failures) else (account:= (old_pass, balance, times + 1); raise wrong_pass) ); retrieve = (fun pass amount -> let old_pass = (extract_pass !account) in let balance = (extract_balance !account) in let times = (extract_times !account) in if times >= 5 then raise too_many_failures else if pass = old_pass then if amount < 0 then (account:= (old_pass, balance, 0); raise negative_amount) else if amount > balance then (account:= (old_pass, balance, 0); raise not_enough_balance) else (account:= (old_pass, balance-amount, 0); ()) else if times + 1 > 5 then (account := (old_pass, balance, times + 1); raise too_many_failures) else (account:= (old_pass, balance, times + 1); raise wrong_pass) ); show_balance= (fun pass -> let old_pass = (extract_pass !account) in let balance = (extract_balance !account) in let times = (extract_times !account) in if times >= 5 then raise too_many_failures else if pass = old_pass then (account:= (old_pass, balance, 0); balance) else if times + 1 > 5 then (account := (old_pass, balance, times + 1); raise too_many_failures) else (account := (old_pass, balance, times + 1); raise wrong_pass) ); } ;;