Commit e2db83a2 authored by Andreas Pfurtscheller's avatar Andreas Pfurtscheller

Add HW09 馃搫

parent 113f644c
......@@ -6,24 +6,106 @@ type notes = (string * behavior) list
type selection_alg = (string * int * int) list -> int -> string list
exception Invalid_file_format of string
exception Unknown_behavior of string
let behavior_from_string string = match string with
| "nice" -> Nice
| "naughty" -> Naughty
| s -> raise (Unknown_behavior (Printf.sprintf "Unknown behavior '%s'." s))
(* 9.3 - 1 *)
let read_notes = todo
let read_notes filename =
let file = open_in filename in
let rec read_entry db =
try
let line = input_line file in
match String.split_on_char ':' line with
| [name;behavior] -> read_entry ((name,(behavior_from_string behavior))::db)
| _ -> raise (Invalid_file_format "Invalid line format.")
with
| End_of_file -> db
| Unknown_behavior msg -> raise (Invalid_file_format msg)
in
try
let db = read_entry [] in
close_in file;
db
with e -> close_in file; raise (Invalid_file_format filename)
(* 9.3 - 2 *)
let read_wishlist = todo
let read_wishlist filename =
let file = open_in filename in
let rec read_entry db =
try
let line = input_line file in
match String.split_on_char ':' line with
| [wish;imp] -> let importance = (int_of_string imp) in
if (String.length wish) <= 0 || importance < 0 || importance > 100
then raise (Invalid_file_format "Invalid importance")
else read_entry ((wish,importance)::db)
| _ -> raise (Invalid_file_format "Invalid line format.")
with End_of_file -> db
in
try
let db = read_entry [] in
close_in file;
db
with e -> close_in file; raise (Invalid_file_format filename)
(* 9.3 - 3 *)
let load_catalogue = todo
let load_catalogue filename =
let file = open_in filename in
let rec read_entry db =
try
let line = input_line file in
match String.split_on_char ':' line with
| [toy;w] -> let weight = (int_of_string w) in
if (String.length toy) <= 0 || weight < 0 then raise (Invalid_file_format "Weight must be positive.")
else read_entry ((toy,weight)::db)
| _ -> raise (Invalid_file_format "Invalid line format.")
with End_of_file -> db
in
try
let db = read_entry [] in
close_in file;
db
with e -> close_in file; raise (Invalid_file_format filename)
(* 9.3 - 4 *)
let write_list = todo
let write_list filename db =
let file = open_out filename in
List.iter (fun p -> Printf.fprintf file "%s\n" p) db;
close_out file
(* 9.3 - 5 *)
let write_letter = todo
let write_letter filename =
let file = open_out filename in
Printf.fprintf file "hey kiddo, try again next year.\n\nlove,\nsanta";
close_out file
(* 9.3 - 6 *)
let run_santas_factory = todo
let run_santas_factory c a =
let toys = load_catalogue "toys_catalogue.txt" in
let notes = read_notes "santas_notes.txt" in
let toy_from_catalogue name = List.find (fun (n,_) -> (n = name)) toys in
let handle_naughty name = write_letter (Printf.sprintf "%s_letter.txt" name) in
let handle_nice name =
let wishlist = read_wishlist (Printf.sprintf "%s_wishlist.txt" name) in
let rec build_wishes db list = match list with
| [] -> db
| (wish,importance)::tl -> try
let (_,weight) = toy_from_catalogue wish in
build_wishes ((wish,importance,weight)::db) tl
with Not_found -> build_wishes db tl in
let wishes = build_wishes [] wishlist in
let selected_presents = a wishes c in
write_list (Printf.sprintf "%s_presents.txt" name) selected_presents in
let handle_note (name,behavior) = match behavior with
| Nice -> (handle_nice name)
| Naughty -> (handle_naughty name) in
List.iter (fun name -> try handle_note name with Invalid_file_format e -> ()) notes
(* 9.3 - 7 *)
let knapsack = todo
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment