text
stringlengths
0
601k
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.fold_left(fun acc (v,n, w) -> if (v = vertex) then (n,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 (n,w) = node in let neighbour_list = neighbours g n in if n = b then [n],w else if List.exists (fun r -> r = n) visited then raise Fail else try let next_node,next_weigh...
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 let neighbour_list = neighbours g n in if n = b then sc ([n],w) else if List.exists (fun r -> r = n) visited then fc() else aux_list neighbour_list...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | [hd] -> Some hd | hd::tl -> Some(List.fold_left (fun p1 p2 -> let (path1, w1) = p1 in let (path2, w2) = p2 in if w1 > w2 then p1 else p2 ) hd tl);;
let open_account a = let pass = ref a in let balance = ref 0 in let attempts = ref 0 in let check_pass pwd f = if pwd = !pass then (attempts := 0; f ()) else (incr attempts; raise wrong_pass) in let attempt pwd f = if !attempts >= 5 then raise too_many_failures else check_pass pwd f in let update_pass old new_a = check...
let neighbours (g: 'a graph) (vertex: 'a) = 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, w) visited : ('a list * weight) = if node = b then ([b], w) else if List.mem node visited then raise Fail else (let (path, cost) = aux_list (neighbours g node) (node :: visited) in (node :: path, cost + w)) and aux_list (nodes: (...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node, w) visited fc sc : ('a list * weight) = if node = b then sc ([b], w) else if List.mem node visited then fc () else aux_list (neighbours g node) (node :: visited) fc (fun (path, cost) -> sc (node :: path, cost + w)) and aux_list ...
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 init_pass = ref initial_pass in let balance = ref 0 in let count = ref 0 in let check_pswd pswd = (if pswd <> !init_pass || !count >= 5 then (count := !count + 1; if !count > 5 then raise too_many_failures else raise wrong_pass) else count := 0 ) in let negat...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let helper = List.filter (fun e -> let (x,y,w) = e in vertex = x) g.edges in List.map (fun (x,y,w) -> (y,w)) helper;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) total_weight : ('a list * weight) = let (x, w) = node in let neighbours_node = neighbours g x in if x = b then ((visited @ [x]), total_weight + w) else if List.length neighbours_node = 0 then rais...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) total_weight fc sc : ('a list * weight) = let (x, w) = node in let neighbours_node = neighbours g x in if x = b then sc ((visited @ [x]),(total_weight + w)) else if List.length neighbours_node = ...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let all_paths = find_all_paths g a b in match all_paths with | [] -> None | _ -> Some (List.fold_right (fun (x1,w1) (x2,w2) -> if w1 >= w2 then (x1,w1) else (x2,w2)) all_paths ([], 0));;
let open_account (initial_pass: passwd) : bank_account = let cur_pass = ref initial_pass in let attempts = ref 0 in let balance = ref 0 in let change_pass new_pass = function () -> cur_pass := new_pass in let deposit_money amount = function () -> if amount < 0 then raise negative_amount else balance := balance.contents...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let get_edge (departure, arrival, w) = if departure = vertex then [(arrival, w)] else [] in List.concat (List.map get_edge g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (name, cost) (visited : 'a list) : ('a list * weight) = let add_Node (trajectory, totalc) = (name :: trajectory, cost + totalc) in if List.mem name visited then raise Fail else if name = b then ([name], cost) else let neighbouring_nodes...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let failure = function () -> raise Fail in let success = function a -> a in let rec aux_node (name, cost) (visited : 'a list) fc sc : ('a list * weight) = if List.mem name visited then fc () else if name = b then sc ([name], cost) else let neighbouring...
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_longest current (big_trajectory, big_cost) = if List.length current > 0 then let (trajectory, totalc) = List.hd current in if totalc > big_cost then find_longest (List.tl current) (traj...
let open_account (initial_pass: passwd) : bank_account = let curr_password = ref initial_pass in let curr_acc_balance = ref 0 in let num_of_attempts = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass = !curr_password then (curr_password := new_pass; num_of_attempts := 0) else (num_of_attempts := !num_of_a...
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) = let (a, w) = node in if a = b then ([b], w) else if (List.exists (fun x -> x = a) visited) then raise Fail else let neighbour_nodes = neighbours g a in match neighbour_nodes...
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 (a, w) = node in if a = b then (sc ([b], w)) else if (List.exists (fun x -> x = a) visited) then fc () else let neighbour_nodes = neighbours g a in match neighbour...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let paths = find_all_paths g a b in match paths with | [] -> None | _ -> let (_, weights) = List.split paths in let max_weight = (List.nth (List.sort compare weights) ((List.length weights) - 1)) in let longest_paths = List.find_all (fun ...
let open_account (initial_pass: passwd) : bank_account = let password: passwd ref = ref initial_pass in let accHoldings = ref 0 in let pass_failed_attempts = ref 0 in let auth_password (pass: passwd) : bool = if !pass_failed_attempts >= 5 then raise too_many_failures; if pass = !password then begin pass_failed_attempts...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let neighbourHelper edge neighbours = let (origin, destination, weight) = edge in if origin = vertex then (destination, weight) :: neighbours else neighbours in List.fold_right neighbourHelper (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) w : ('a list * weight) = let new_visited = (fst node) :: visited in let new_w = w + (snd node) in let new_neighbours = neighbours g (fst node) in if List.length new_neighbours = 0 then 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 w : ('a list * weight)= let new_visited = (fst node) :: visited in let new_w = w + (snd node) in let new_neighbours = neighbours g (fst node) in if List.exists (fun c -> (fst c) = b) new_ne...
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 List.length paths = 0 then None else Some (List.hd (List.sort (fun c d -> compare (snd c) (snd d)) paths)) ;;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_bal = ref 0 in let num_tries = ref 0 in { update_pass = (fun x y -> if (x = !password) then (password := y ; num_tries := 0;()) else (num_tries := !num_tries +1; raise wrong_pass)); deposit= (fun x y -> if (!num_tries >...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec traverse edges looking = match edges with | [] -> [] | x::xs -> let (y,m,w) = x in if y = looking then (m, w)::(traverse xs looking) else traverse xs looking in traverse g.edges vertex;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) : ('a list * weight) = let (node,nodeweight) = n in if node = b then ([b], nodeweight) else if (not (List.mem node visited)) then ( let (a,b) = (aux_list (neighbours g node) (visited@[node])) in (nod...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (node,nodeweight) = n in if node = b then sc ([b], nodeweight) else if (not (List.mem node visited)) then ( let (a,b) = (aux_list (neighbours g node) (visited@[node])...
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 let rec findit highcost highpath allpaths = match allpaths with | [] -> Some (highpath, highcost) | (list, weight)::xs -> if (weight > highcost) then (findit weight list xs) else (findit highcos...
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let curr_bal = ref 0 in let num_tries = ref 0 in { update_pass = (fun x y -> if (x = !password) then (password := y ; num_tries := 0;()) else (num_tries := !num_tries +1; raise wrong_pass)); deposit= (fun x y -> if (!num_tries >...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec traverse edges looking = match edges with | [] -> [] | x::xs -> let (y,m,w) = x in if y = looking then (m, w)::(traverse xs looking) else traverse xs looking in traverse g.edges vertex;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) : ('a list * weight) = let (node,nodeweight) = n in if node = b then ([b], nodeweight) else if (not (List.mem node visited)) then ( let (a,b) = (aux_list (neighbours g node) (visited@[node])) in (nod...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (n: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (node,nodeweight) = n in if node = b then sc ([b], nodeweight) else if (not (List.mem node visited)) then (aux_list (neighbours g node) (node :: visited) fc (fun (p, ...
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 let rec findit highcost highpath allpaths = match allpaths with | [] -> Some (highpath, highcost) | (list, weight)::xs -> if (weight > highcost) then (findit weight list xs) else (findit highcos...
let open_account (initial_pass: passwd) : bank_account = notimplemented () ;;
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = notimplemented ();;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) : ('a list * weight) = notimplemented () in notimplemented ();;
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= notimplemented () and aux_list (nodes: ('a * weight) list) (visited: 'a list) fc sc : ('a list * weight) = notimplemented () in notimplemented ();;
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented ();;
let open_account (p: passwd) : bank_account = let password = ref p in let attempts = ref 0 in let balance = ref 0 in let check_password (p: passwd): bool = if (!attempts < 3) then if (p == !password) then (attempts := 0; true) else (attempts := !attempts + 1; raise wrong_pass) else raise too_many_failures in { update_p...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f (ww: ('a * weight) list) (edge: ('a * 'a * weight)): ('a * weight) list = let (ver1, ver2, w) = edge in if (ver1 = vertex) then (ver2, w)::ww else ww in List.fold_left f [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (ww: weight) (visited : 'a list) : ('a list * weight) = let (ver1,ver2) = node in if (List.mem ver1 visited) then raise Fail else if (b == ver1) then (visited@[ver1], ver2 + ww) else aux_list (neighbours g ver1) (vis...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (ww: weight) (visited : 'a list) (aa) (bb) : ('a list * weight)= let (ver1,ver2) = node in if (List.mem ver1 visited) then aa () else if (b == ver1) then bb (visited@[ver1], ver2 + ww) else aux_list (neighbours g ve...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let f (best) (l,w) = match best with | None -> Some(l,w) | Some(maxPath, maxWeight) -> if (w > maxWeight) then Some(l,w) else Some(maxPath, maxWeight) in List.fold_left f None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let cur_passwd = ref initial_pass in let cur_amount = ref 0 in let attempt_counter = ref 0 in { update_pass = (fun input_passwd new_passwd -> if !cur_passwd <> input_passwd then (attempt_counter := !attempt_counter + 1; raise wrong_pass) else (cur_passwd := new_p...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let l1 = [] in let rec neighbours_helper (edge: ('a * 'a * weight) list) (vertex: 'a) (input_list: ('a * weight) list) : ('a * weight) list = match edge with | [] -> input_list | (a, b, c) :: t -> if a = vertex then neighbours_helper t vertex [(b,c)] @ in...
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let sum_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = match node with |(value,weight) -> if List.mem value visited then raise Fail else ( if value = b then ((visited @ [value]), !sum_weight) else let n...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let sum_weight = ref 0 in let added = ref [] in let subtract_weight = ref 0 in let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= let (value, weight) = node in if (not (List.mem value visited)) then ( sum_weight := !su...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = try Some (find_path g a b) with Fail -> None;;
let open_account (initial_pass: passwd) : bank_account = let current_pass = ref initial_pass in let invalid_attempt = ref 0 in let current_balance = ref 0 in { update_pass = (fun old_pass new_pass -> if old_pass <> !current_pass then (invalid_attempt := !invalid_attempt + 1; raise wrong_pass) else (invalid_attempt := 0...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let isNeighbours (theList: ('a * weight) list) (edge: ('a * 'a * weight)) = let theVertex = vertex in let (x,y,z) = edge in if x = theVertex then (y,z)::theList else theList in List.fold_left isNeighbours [] 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 (x,_) = node in (if List.mem x visited then raise Fail else aux_list (neighbours g x) (x::visited) weight ) and aux_list (nodes: ('a * weight) list) (visit...
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 (weight: int) : ('a list * weight)= let (node,theWeight) = node in if List.mem node visited then fc() else aux_list (neighbours g node) (node::visited) fc (fun(e,f)-> sc (node::e, f+theWeig...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let result = List.sort (fun (_,d) (_,f) -> compare d f ) (find_all_paths g a b) in if result = [] then None else Some (List.nth (List.rev result) 0);;
let open_account (initial_pass: passwd) : bank_account = let balance = ref 0 in let password = ref initial_pass in let num_pass_fail = ref 0 in let max_pass_fail = 5 in let auth max f pass = if !num_pass_fail < max || max = 0 then if pass = !password then ( num_pass_fail := 0; f () ) else ( num_pass_fail := !num_pass_f...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = g.edges |> List.filter (fun (v1, _, _) -> v1 = vertex) |> List.rev_map (fun (_, v2, w) -> (v2, w));;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux rev_path cost = match rev_path with | [] -> raise (Msg "rev_path is not supposed to be empty") | curr :: _ -> if curr = b then (List.rev rev_path, cost) else let rec aux' l = match l with | [] -> raise Fail | (v, w) :: ns -> try aux (v :: re...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux rev_path cost success fail = match rev_path with | [] -> raise (Msg "rev_path is not supposed to be empty") | curr :: _ -> if curr = b then success (List.rev rev_path, cost) else let ns = neighbours g curr |> List.filter (fun (v, _) -> not ...
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 :: paths -> Some( List.fold_left (fun a b -> if snd b > snd a then b else a) path paths );;
let open_account (initial_pass: passwd) : bank_account = let ini_pw = ref initial_pass in let balance = ref 0 in let failure = ref 0 in let check_password password = if password = !ini_pw then failure := 0 else (failure := !failure + 1; if !failure <= 5 then raise wrong_pass else raise too_many_failures) in let check_p...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec find_outnode edges visited = match edges with |(a,d,w)::xs -> if a = vertex then find_outnode xs ((d,w)::visited) else find_outnode xs visited |[] -> visited in find_outnode g.edges [] ;; let rev_list l = let rec rev_acc acc = function | [] -> acc...
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) dist: ('a list * weight) = let (v,w) = node in if (v = b) then ((rev_list (b::visited)),(dist+w)) else (if (List.mem v visited) then raise Fail else aux_list (neighbours g v) (v :: visited) (dist+...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc dist: ('a list * weight) = let (v,w) = node in if (v = b) then ((rev_list (b::visited)),(dist+w)) else (if (List.mem v visited) then fc() else aux_list (neighbours g v) (v :: visited) fc (dist...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l = find_all_paths g a b in match l with | [] -> None | _ -> let rec compare l acc weight = match l with | hd :: tl -> let (a,b) = hd in if b > weight then compare tl hd b else compare tl acc weight | [] -> acc in Some (compare l ([],...
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let num_wrong = ref 0 in { update_pass = (fun (old_pass: passwd) (new_pass: passwd) -> match old_pass with | x when x = !pass -> num_wrong := 0; pass := new_pass |_ -> num_wrong := !num_wrong + 1; raise wrong_...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let is_v1 x = match x with | (a, b, c) when a = vertex -> true |_ -> false in let l1 = List.filter is_v1 g.edges in List.map (fun (a1, b, c) -> (b, c)) l1;;
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) = match node with |(x, y) when x = b -> ([b], y) |(x, y) when List.mem x visited -> raise Fail |(x, y) -> try let z = aux_list (neighbours g x) (x :: visited) in let (e,f) = z...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) fc sc : ('a list * weight)= match node with |(x, y) when x = b -> sc ([b], y) |(x, y) when List.mem x visited -> fc () |(x, y) -> let succ = (fun r -> let (c,d) = r in sc (x :: c, d + y)) in aux_...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let l1 = find_all_paths g a b in match l1 with |[] -> None |x :: xs -> Some (List.fold_left (fun x y-> let (c, d) = x in let (e,f) = y in if f > d then y else x) ([], 0) l1);;;
let open_account (initial_pass: passwd) : bank_account = let pass= ref initial_pass in let balance= ref 0 in let counter= ref 0 in { update_pass =( fun old_pass new_pass -> if old_pass = !pass then ( pass := new_pass; counter := 0; ) else ( counter := !counter +1; raise wrong_pass ) ); deposit =( fun password amount ->...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let check l ( (v1, v2, w): ('a * 'a * weight) ) = if vertex = v1 then (v2, w) :: l else l in List.fold_left (check) [] g.edges ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let (n , w) = node in let add_el el = let (l, curr_w) = el in (List.cons n l , curr_w + w) in if n = b then ( [n] , w ) else match neighbours g n with | [] -> raise Fail | l...
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 match neighbours g n with | [] -> fc () | lst -> aux_list lst (List.cons n visited) fc ( fun r -> let (l, curr_...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = match find_all_paths g a b with | [] -> None | (lst, w) :: tl -> ( let rec find_longest l (long, highest_cost)= match l with | [] -> Some ( (long, highest_cost) ) | ( cur , cur_cost) :: tl -> ( if cur_cost > highest_cost then find_longest...
let open_account (initial_pass: passwd) : bank_account = let update_password info old_pass new_pass : unit = if old_pass = info.password then ( info.password <- new_pass; info.failures <- 0 ) else ( info.failures <- info.failures + 1; raise wrong_pass ) in let ret info pass money : unit = if info.failures >= 5 then rai...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = List.map (fun (_, y, z) -> (y, z)) (List.filter (fun (x, _, _) -> x = vertex) g.edges);;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let neigh src path = List.filter (fun (x, _) -> not (List.mem x path)) (neighbours g src) in let rec aux_node (node: 'a * weight) (visited : 'a list) : ('a list * weight) = let l = fun _ -> aux_list (neigh (fst node) visited) ((fst node)::visited) in if...
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 neigh src path = List.filter (fun (x, _) -> not (List.mem x path)) (neighbours g src) in let l = fun _ -> aux_list (neigh (fst node) visited) ((fst node)::visited)...
let find_longest_path g a b : ('a list * int) option = let l = find_all_paths g a b in if List.length l > 0 then Some (List.hd (List.sort (fun (_,y) (_,x)-> compare x y) (l))) else None;;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let balance = ref 0 in let count = ref 0 in { update_pass = (fun (oldpass:passwd) (npwd:passwd) -> if (oldpass = !pass) then (pass:=npwd ; count := 0) else (count := !count+1 ; raise wrong_pass)) ; deposit = (fun (pwd:passwd) (amoun...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let rec helper (g: 'a graph) (vertex: 'a) (acc: ('a * weight) list) = match g with | { nodes=_ ; edges=[]} -> acc | {nodes= l1 ; edges= (a, b, weight) :: tl } -> if a=vertex then helper {nodes= l1 ; edges = tl} vertex ((b, weight)::acc) else helper {nodes...
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let l1 = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (weight:int): ('a list * weight) = let (d, w) = node in if (List.mem d visited) then raise Fail else if d=b then ((visited@[d]), w+weight) else aux_list (neighbours g d)...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let l1 = neighbours g a in let rec aux_node (node: 'a * weight) (visited : 'a list) (weight:int) fc sc : ('a list * weight)= let (c, w) = node in if (List.mem c visited) then fc() else if c=b then (sc c w) else aux_list (neighbours g c) (visited@[c]) (...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = notimplemented();;
let open_account (initial_pass: passwd) : bank_account = let pass = ref initial_pass in let cur_balance = ref 0 in let attempts = ref 0 in { update_pass = (fun oldpass newpass -> if oldpass = !pass then begin attempts := 0; pass := newpass end else begin attempts := !attempts + 1; raise wrong_pass end); deposit = (fun ...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let foldR = List.fold_right (fun edge nodes -> let (a, b, wgt) = edge in if a = vertex then (b, wgt) :: nodes else nodes ) in foldR g.edges [] ;; let rec append lst1 lst2 = match lst1 with | [] -> lst2 | h :: t -> h :: (append t lst2) ;;
let find_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node node visited listnodes: ('a list * weight) = match (neighbours g node) with | [] -> raise Fail | _ -> aux_list (neighbours g node) visited listnodes and aux_list (nodes : ('a * weight) list) (visited: 'a list) listnodes : ('a list * wei...
let find_path' (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) = let rec aux_node (node :'a * weight) (visited : 'a list) listnodes fc sc : ('a list * weight)= let (vertex, weight) = node in if (List.mem vertex visited) then fc () else if (vertex = b) then sc ((append visited [vertex]), listnodes + weight) else aux_...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let path (worst) (l,w) = match worst with | None -> Some(l,w) | Some(maxPath, maxWeight) -> if (w > maxWeight) then Some(l,w) else Some(maxPath, maxWeight) in List.fold_left path None (find_all_paths g a b) ;;
let open_account (initial_pass: passwd) : bank_account = let tries_count : int ref = ref 0 in let password : passwd ref = ref initial_pass in let acc_balance : int ref = ref 0 in {update_pass = (fun old_pass new_pass -> if old_pass = !password then (tries_count := 0; password := new_pass;) else (tries_count := !tries_c...
let neighbours (g: 'a graph) (vertex: 'a) : ('a * weight) list = let f = fun (a,b,c) s -> if a = vertex then (b,c)::s else s in List.fold_right (f) g.edges [] let helper g node b weight visited aux_list neighbour = if node = b then ([node], weight) else if (neighbour = []) then raise Fail else if (List.mem node visited...
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 (nd, wt) = node in (let neighbour = neighbours(g)(nd) in (helper(g)(nd)(b)(wt)(visited)(aux_list)(neighbour)) ) and aux_list (nodes: ('a * weight) list) (visited: 'a lis...
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 (nd, wt) = node in (let neighbour = neighbours(g)(nd) in (helper2(g)(nd)(wt)(visited)(aux_list)(fc)(sc)(neighbour)) ) and aux_list (nodes: ('a * weight) list) (vis...
let find_longest_path (g: 'a graph) (a: 'a) (b: 'a) : ('a list * weight) option = let allPaths = find_all_paths(g)(a)(b) in helper4(allPaths);;
let open_account (initial_pass: passwd) : bank_account = let password = ref initial_pass in let balance = ref 0 in let wrongAttempts = ref 0 in { update_pass = (fun oldPass newPass -> if oldPass = !password then (password := newPass ; wrongAttempts := 0) else (wrongAttempts := (!wrongAttempts + 1); raise wrong_pass)); ...