From 5fbe12129d3df1671b98ab2b6e914f3ff8741374 Mon Sep 17 00:00:00 2001 From: Emmanuel Delaborde Date: Fri, 4 Oct 2019 13:03:21 +1300 Subject: [PATCH 1/3] implement binary-search-tree exercise --- config.json | 12 ++++ exercises/binary-search-tree/Makefile | 9 +++ exercises/binary-search-tree/README.md | 60 +++++++++++++++++++ .../binary-search-tree/binary_search_tree.ml | 16 +++++ .../binary-search-tree/binary_search_tree.mli | 17 ++++++ exercises/binary-search-tree/dune | 16 +++++ exercises/binary-search-tree/dune-project | 2 + exercises/binary-search-tree/example.ml | 26 ++++++++ exercises/binary-search-tree/test.ml | 58 ++++++++++++++++++ 9 files changed, 216 insertions(+) create mode 100644 exercises/binary-search-tree/Makefile create mode 100644 exercises/binary-search-tree/README.md create mode 100644 exercises/binary-search-tree/binary_search_tree.ml create mode 100644 exercises/binary-search-tree/binary_search_tree.mli create mode 100644 exercises/binary-search-tree/dune create mode 100644 exercises/binary-search-tree/dune-project create mode 100644 exercises/binary-search-tree/example.ml create mode 100644 exercises/binary-search-tree/test.ml diff --git a/config.json b/config.json index 7ff2143f6f..292ca14978 100644 --- a/config.json +++ b/config.json @@ -447,6 +447,18 @@ "difficulty": 0, "topics": null, "deprecated": true + }, + { + "slug": "binary-search-tree", + "uuid": "ce05137f-d4e5-40f2-ae6b-6c43e00d458f", + "core": false, + "unlocked_by": null, + "difficulty": 3, + "topics": [ + "recursion", + "searching", + "trees" + ] } ] } diff --git a/exercises/binary-search-tree/Makefile b/exercises/binary-search-tree/Makefile new file mode 100644 index 0000000000..b71d6af2de --- /dev/null +++ b/exercises/binary-search-tree/Makefile @@ -0,0 +1,9 @@ +default: clean test + +test: + dune runtest + +clean: + dune clean + +.PHONY: clean diff --git a/exercises/binary-search-tree/README.md b/exercises/binary-search-tree/README.md new file mode 100644 index 0000000000..01d4572a16 --- /dev/null +++ b/exercises/binary-search-tree/README.md @@ -0,0 +1,60 @@ +# Binary Search Tree + +Insert and search for numbers in a binary tree. + +When we need to represent sorted data, an array does not make a good +data structure. + +Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes +`[1, 3, 4, 5, 2]` now we must sort the entire array again! We can +improve on this by realizing that we only need to make space for the new +item `[1, nil, 3, 4, 5]`, and then adding the item in the space we +added. But this still requires us to shift many elements down by one. + +Binary Search Trees, however, can operate on sorted data much more +efficiently. + +A binary search tree consists of a series of connected nodes. Each node +contains a piece of data (e.g. the number 3), a variable named `left`, +and a variable named `right`. The `left` and `right` variables point at +`nil`, or other nodes. Since these other nodes in turn have other nodes +beneath them, we say that the left and right variables are pointing at +subtrees. All data in the left subtree is less than or equal to the +current node's data, and all data in the right subtree is greater than +the current node's data. + +For example, if we had a node containing the data 4, and we added the +data 2, our tree would look like this: + + 4 + / + 2 + +If we then added 6, it would look like this: + + 4 + / \ + 2 6 + +If we then added 3, it would look like this + + 4 + / \ + 2 6 + \ + 3 + +And if we then added 1, 5, and 7, it would look like this + + 4 + / \ + / \ + 2 6 + / \ / \ + 1 3 5 7 +## Source + +Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file diff --git a/exercises/binary-search-tree/binary_search_tree.ml b/exercises/binary-search-tree/binary_search_tree.ml new file mode 100644 index 0000000000..ce27643a7a --- /dev/null +++ b/exercises/binary-search-tree/binary_search_tree.ml @@ -0,0 +1,16 @@ +open Base + +type bst + +let empty = failwith "'empty' is missing" + +let value _ = failwith "'empty' is missing" + +let left _ = failwith "'empty' is missing" + +let right _ = failwith "'empty' is missing" + +let insert _ _ = failwith "'empty' is missing" + +let to_list _ = failwith "'empty' is missing" + diff --git a/exercises/binary-search-tree/binary_search_tree.mli b/exercises/binary-search-tree/binary_search_tree.mli new file mode 100644 index 0000000000..b83b66560e --- /dev/null +++ b/exercises/binary-search-tree/binary_search_tree.mli @@ -0,0 +1,17 @@ +open Base + +type bst + +val empty : bst + +val value : bst -> (int, string) Result.t + +val left : bst -> (bst, string) Result.t + +val right : bst -> (bst, string) Result.t + +val insert : int -> bst -> bst + +val to_list : bst -> int list + + diff --git a/exercises/binary-search-tree/dune b/exercises/binary-search-tree/dune new file mode 100644 index 0000000000..3d0b594af7 --- /dev/null +++ b/exercises/binary-search-tree/dune @@ -0,0 +1,16 @@ +(executable + (name test) + (libraries base oUnit)) + +(alias + (name runtest) + (deps (:x test.exe)) + (action (run %{x}))) + +(alias + (name buildtest) + (deps (:x test.exe))) + +(env + (dev + (flags (:standard -warn-error -A)))) \ No newline at end of file diff --git a/exercises/binary-search-tree/dune-project b/exercises/binary-search-tree/dune-project new file mode 100644 index 0000000000..f4f8feafff --- /dev/null +++ b/exercises/binary-search-tree/dune-project @@ -0,0 +1,2 @@ +(lang dune 1.1) +(version 1.3.0) diff --git a/exercises/binary-search-tree/example.ml b/exercises/binary-search-tree/example.ml new file mode 100644 index 0000000000..3f784f8584 --- /dev/null +++ b/exercises/binary-search-tree/example.ml @@ -0,0 +1,26 @@ +open Base + +type 'a bst = Leaf | Node of 'a bst * 'a * 'a bst + +let empty = Leaf + +let value = function + | Leaf -> Error "empty tree" + | Node(_, v, _) -> v + +let left = function + | Leaf -> Error "empty tree" + | Node(l, _, _) -> l + +let right = function + | Leaf -> Error "empty tree" + | Node(_, _, r) -> r + +let insert v = function + | Leaf -> Node(Leaf, v, Leaf) + | Node(l, v', r) when v <= v' -> Node(insert v l, v', r) + | Node(l, v', r) -> Node(l, v', insert v r) + +let to_list = function +| Leaf -> [] +| Node(l, v, r) -> to_list(l) @ [v] @ to_list(r) \ No newline at end of file diff --git a/exercises/binary-search-tree/test.ml b/exercises/binary-search-tree/test.ml new file mode 100644 index 0000000000..d43b1e947b --- /dev/null +++ b/exercises/binary-search-tree/test.ml @@ -0,0 +1,58 @@ +open Base +open OUnit2 +open Binary_search_tree + +let result_to_string f = function + | Error m -> Printf.sprintf "Error \"%s\"" m + | Ok x -> f x |> Printf.sprintf "Some %s" + +let ae exp got _test_ctxt = + assert_equal ~printer:(result_to_string Int.to_string) exp got + +let intlist_to_string l = + List.map l ~f:Int.to_string + |> List.intersperse ~sep:"; " + |> List.fold ~init:"" ~f:(^) + |> fun s -> "[" ^ s ^ "]" + +let ael exp got _test_ctxt = + assert_equal ~printer:intlist_to_string exp got + +let tests = + let t4 = empty |> insert 4 in + let t42 = t4 |> insert 2 in + let l2 = t42 |> left in + let t44 = t4 |> insert 4 in + let l4 = t44 |> left in + let t45 = t4 |> insert 5 in + let r5 = t45 |> right in + let t4261357 = t42 |> insert 6 |> insert 1 |> insert 3 |> insert 5 |> insert 7 in + let t2 = empty |> insert 2 in + let t21 = t2 |> insert 1 in + let t22 = t2 |> insert 2 in + let t23 = t2 |> insert 3 in + let t213675 = t21 |> insert 3 |> insert 6 |> insert 7 |> insert 5 in + [ + "data is retained" >:: ae (Ok 4) (value t4); + "smaller number at left node 1" >:: ae (Ok 4) (value t42); + "smaller number at left node 2" >:: ae (Ok 2) (Result.bind l2 ~f:value); + "same number at left node 1" >:: ae (Ok 4) (value t44); + "same number at left node 2" >:: ae (Ok 4) (Result.bind l4 ~f:value); + "greater number at right node 1" >:: ae (Ok 4) (value t45); + "greater number at right node 2" >:: ae (Ok 5) (Result.bind r5 ~f:value); + "can create complex tree 1" >:: ae (Ok 4) (value t4261357); + "can create complex tree 2" >:: ae (Ok 2) (Result.bind (t4261357 |> left) ~f:value); + "can create complex tree 3" >:: ae (Ok 1) (Result.bind (Result.bind (t4261357 |> left) ~f:left) ~f:value); + "can create complex tree 4" >:: ae (Ok 3) (Result.bind (Result.bind (t4261357 |> left) ~f:right) ~f:value); + "can create complex tree 5" >:: ae (Ok 6) (Result.bind (t4261357 |> right) ~f:value); + "can create complex tree 6" >:: ae (Ok 5) (Result.bind (Result.bind (t4261357 |> right) ~f:left) ~f:value); + "can create complex tree 7" >:: ae (Ok 7) (Result.bind (Result.bind (t4261357 |> right) ~f:right) ~f:value); + "can sort single number" >:: ael [2] (to_list t2); + "can sort if second number is smaller than first" >:: ael [1;2] (to_list t21); + "can sort if second number is same as first" >:: ael [2;2] (to_list t22); + "can sort if second number is greater than first" >:: ael [2;3] (to_list t23); + "can sort complex tree" >:: ael [1; 2; 3; 5; 6; 7] (to_list t213675); + ] + +let () = + run_test_tt_main ("binary-search-tree tests" >::: tests) From d50f903c98939ce70c9755d00efc17df89e87f5f Mon Sep 17 00:00:00 2001 From: Emmanuel Delaborde Date: Sat, 5 Oct 2019 20:26:55 +1300 Subject: [PATCH 2/3] implement linked-list exercise --- config.json | 16 +-- exercises/binary-search-tree/README.md | 60 ------------ .../binary-search-tree/binary_search_tree.ml | 16 --- .../binary-search-tree/binary_search_tree.mli | 17 ---- exercises/binary-search-tree/example.ml | 26 ----- exercises/binary-search-tree/test.ml | 58 ----------- .../Makefile | 0 exercises/linked-list/README.md | 29 ++++++ .../{binary-search-tree => linked-list}/dune | 0 .../dune-project | 0 exercises/linked-list/example.ml | 48 +++++++++ exercises/linked-list/linked_list.ml | 15 +++ exercises/linked-list/linked_list.mli | 15 +++ exercises/linked-list/test.ml | 97 +++++++++++++++++++ 14 files changed, 212 insertions(+), 185 deletions(-) delete mode 100644 exercises/binary-search-tree/README.md delete mode 100644 exercises/binary-search-tree/binary_search_tree.ml delete mode 100644 exercises/binary-search-tree/binary_search_tree.mli delete mode 100644 exercises/binary-search-tree/example.ml delete mode 100644 exercises/binary-search-tree/test.ml rename exercises/{binary-search-tree => linked-list}/Makefile (100%) create mode 100644 exercises/linked-list/README.md rename exercises/{binary-search-tree => linked-list}/dune (100%) rename exercises/{binary-search-tree => linked-list}/dune-project (100%) create mode 100644 exercises/linked-list/example.ml create mode 100644 exercises/linked-list/linked_list.ml create mode 100644 exercises/linked-list/linked_list.mli create mode 100644 exercises/linked-list/test.ml diff --git a/config.json b/config.json index 292ca14978..3ce9dae714 100644 --- a/config.json +++ b/config.json @@ -449,16 +449,16 @@ "deprecated": true }, { - "slug": "binary-search-tree", - "uuid": "ce05137f-d4e5-40f2-ae6b-6c43e00d458f", + "slug": "linked-list", + "uuid": "840f4011-029a-4f5c-970f-f8e585cd4fdf", "core": false, - "unlocked_by": null, - "difficulty": 3, + "unlocked_by": "null", + "difficulty": 8, "topics": [ - "recursion", - "searching", - "trees" + "data_structures", + "lists", + "recursion" ] } ] -} +} \ No newline at end of file diff --git a/exercises/binary-search-tree/README.md b/exercises/binary-search-tree/README.md deleted file mode 100644 index 01d4572a16..0000000000 --- a/exercises/binary-search-tree/README.md +++ /dev/null @@ -1,60 +0,0 @@ -# Binary Search Tree - -Insert and search for numbers in a binary tree. - -When we need to represent sorted data, an array does not make a good -data structure. - -Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes -`[1, 3, 4, 5, 2]` now we must sort the entire array again! We can -improve on this by realizing that we only need to make space for the new -item `[1, nil, 3, 4, 5]`, and then adding the item in the space we -added. But this still requires us to shift many elements down by one. - -Binary Search Trees, however, can operate on sorted data much more -efficiently. - -A binary search tree consists of a series of connected nodes. Each node -contains a piece of data (e.g. the number 3), a variable named `left`, -and a variable named `right`. The `left` and `right` variables point at -`nil`, or other nodes. Since these other nodes in turn have other nodes -beneath them, we say that the left and right variables are pointing at -subtrees. All data in the left subtree is less than or equal to the -current node's data, and all data in the right subtree is greater than -the current node's data. - -For example, if we had a node containing the data 4, and we added the -data 2, our tree would look like this: - - 4 - / - 2 - -If we then added 6, it would look like this: - - 4 - / \ - 2 6 - -If we then added 3, it would look like this - - 4 - / \ - 2 6 - \ - 3 - -And if we then added 1, 5, and 7, it would look like this - - 4 - / \ - / \ - 2 6 - / \ / \ - 1 3 5 7 -## Source - -Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek) - -## Submitting Incomplete Solutions -It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file diff --git a/exercises/binary-search-tree/binary_search_tree.ml b/exercises/binary-search-tree/binary_search_tree.ml deleted file mode 100644 index ce27643a7a..0000000000 --- a/exercises/binary-search-tree/binary_search_tree.ml +++ /dev/null @@ -1,16 +0,0 @@ -open Base - -type bst - -let empty = failwith "'empty' is missing" - -let value _ = failwith "'empty' is missing" - -let left _ = failwith "'empty' is missing" - -let right _ = failwith "'empty' is missing" - -let insert _ _ = failwith "'empty' is missing" - -let to_list _ = failwith "'empty' is missing" - diff --git a/exercises/binary-search-tree/binary_search_tree.mli b/exercises/binary-search-tree/binary_search_tree.mli deleted file mode 100644 index b83b66560e..0000000000 --- a/exercises/binary-search-tree/binary_search_tree.mli +++ /dev/null @@ -1,17 +0,0 @@ -open Base - -type bst - -val empty : bst - -val value : bst -> (int, string) Result.t - -val left : bst -> (bst, string) Result.t - -val right : bst -> (bst, string) Result.t - -val insert : int -> bst -> bst - -val to_list : bst -> int list - - diff --git a/exercises/binary-search-tree/example.ml b/exercises/binary-search-tree/example.ml deleted file mode 100644 index 3f784f8584..0000000000 --- a/exercises/binary-search-tree/example.ml +++ /dev/null @@ -1,26 +0,0 @@ -open Base - -type 'a bst = Leaf | Node of 'a bst * 'a * 'a bst - -let empty = Leaf - -let value = function - | Leaf -> Error "empty tree" - | Node(_, v, _) -> v - -let left = function - | Leaf -> Error "empty tree" - | Node(l, _, _) -> l - -let right = function - | Leaf -> Error "empty tree" - | Node(_, _, r) -> r - -let insert v = function - | Leaf -> Node(Leaf, v, Leaf) - | Node(l, v', r) when v <= v' -> Node(insert v l, v', r) - | Node(l, v', r) -> Node(l, v', insert v r) - -let to_list = function -| Leaf -> [] -| Node(l, v, r) -> to_list(l) @ [v] @ to_list(r) \ No newline at end of file diff --git a/exercises/binary-search-tree/test.ml b/exercises/binary-search-tree/test.ml deleted file mode 100644 index d43b1e947b..0000000000 --- a/exercises/binary-search-tree/test.ml +++ /dev/null @@ -1,58 +0,0 @@ -open Base -open OUnit2 -open Binary_search_tree - -let result_to_string f = function - | Error m -> Printf.sprintf "Error \"%s\"" m - | Ok x -> f x |> Printf.sprintf "Some %s" - -let ae exp got _test_ctxt = - assert_equal ~printer:(result_to_string Int.to_string) exp got - -let intlist_to_string l = - List.map l ~f:Int.to_string - |> List.intersperse ~sep:"; " - |> List.fold ~init:"" ~f:(^) - |> fun s -> "[" ^ s ^ "]" - -let ael exp got _test_ctxt = - assert_equal ~printer:intlist_to_string exp got - -let tests = - let t4 = empty |> insert 4 in - let t42 = t4 |> insert 2 in - let l2 = t42 |> left in - let t44 = t4 |> insert 4 in - let l4 = t44 |> left in - let t45 = t4 |> insert 5 in - let r5 = t45 |> right in - let t4261357 = t42 |> insert 6 |> insert 1 |> insert 3 |> insert 5 |> insert 7 in - let t2 = empty |> insert 2 in - let t21 = t2 |> insert 1 in - let t22 = t2 |> insert 2 in - let t23 = t2 |> insert 3 in - let t213675 = t21 |> insert 3 |> insert 6 |> insert 7 |> insert 5 in - [ - "data is retained" >:: ae (Ok 4) (value t4); - "smaller number at left node 1" >:: ae (Ok 4) (value t42); - "smaller number at left node 2" >:: ae (Ok 2) (Result.bind l2 ~f:value); - "same number at left node 1" >:: ae (Ok 4) (value t44); - "same number at left node 2" >:: ae (Ok 4) (Result.bind l4 ~f:value); - "greater number at right node 1" >:: ae (Ok 4) (value t45); - "greater number at right node 2" >:: ae (Ok 5) (Result.bind r5 ~f:value); - "can create complex tree 1" >:: ae (Ok 4) (value t4261357); - "can create complex tree 2" >:: ae (Ok 2) (Result.bind (t4261357 |> left) ~f:value); - "can create complex tree 3" >:: ae (Ok 1) (Result.bind (Result.bind (t4261357 |> left) ~f:left) ~f:value); - "can create complex tree 4" >:: ae (Ok 3) (Result.bind (Result.bind (t4261357 |> left) ~f:right) ~f:value); - "can create complex tree 5" >:: ae (Ok 6) (Result.bind (t4261357 |> right) ~f:value); - "can create complex tree 6" >:: ae (Ok 5) (Result.bind (Result.bind (t4261357 |> right) ~f:left) ~f:value); - "can create complex tree 7" >:: ae (Ok 7) (Result.bind (Result.bind (t4261357 |> right) ~f:right) ~f:value); - "can sort single number" >:: ael [2] (to_list t2); - "can sort if second number is smaller than first" >:: ael [1;2] (to_list t21); - "can sort if second number is same as first" >:: ael [2;2] (to_list t22); - "can sort if second number is greater than first" >:: ael [2;3] (to_list t23); - "can sort complex tree" >:: ael [1; 2; 3; 5; 6; 7] (to_list t213675); - ] - -let () = - run_test_tt_main ("binary-search-tree tests" >::: tests) diff --git a/exercises/binary-search-tree/Makefile b/exercises/linked-list/Makefile similarity index 100% rename from exercises/binary-search-tree/Makefile rename to exercises/linked-list/Makefile diff --git a/exercises/linked-list/README.md b/exercises/linked-list/README.md new file mode 100644 index 0000000000..137ef7aa39 --- /dev/null +++ b/exercises/linked-list/README.md @@ -0,0 +1,29 @@ +# Linked List + +Implement a doubly linked list. + +Like an array, a linked list is a simple linear data structure. Several +common data types can be implemented using linked lists, like queues, +stacks, and associative arrays. + +A linked list is a collection of data elements called *nodes*. In a +*singly linked list* each node holds a value and a link to the next node. +In a *doubly linked list* each node also holds a link to the previous +node. + +You will write an implementation of a doubly linked list. Implement a +Node to hold a value and pointers to the next and previous nodes. Then +implement a List which holds references to the first and last node and +offers an array-like interface for adding and removing items: + +* `push` (*insert value at back*); +* `pop` (*remove value at back*); +* `shift` (*remove value at front*). +* `unshift` (*insert value at front*); +* `count` (*count elements*); + +To keep your implementation simple, the tests will not cover error +conditions. Specifically: `pop` or `shift` will never be called on an +empty list. + +If you want to know more about linked lists, check [Wikipedia](https://en.wikipedia.org/wiki/Linked_list). \ No newline at end of file diff --git a/exercises/binary-search-tree/dune b/exercises/linked-list/dune similarity index 100% rename from exercises/binary-search-tree/dune rename to exercises/linked-list/dune diff --git a/exercises/binary-search-tree/dune-project b/exercises/linked-list/dune-project similarity index 100% rename from exercises/binary-search-tree/dune-project rename to exercises/linked-list/dune-project diff --git a/exercises/linked-list/example.ml b/exercises/linked-list/example.ml new file mode 100644 index 0000000000..67b734add4 --- /dev/null +++ b/exercises/linked-list/example.ml @@ -0,0 +1,48 @@ +open Base + +type 'a node = + | Empty + | Node of { value: 'a; mutable prev: 'a node; mutable next: 'a node} +and 'a linked_list = { mutable first: 'a node; mutable last:'a node} + +let empty () = {first = Empty; last = Empty} + +let push v l = + let elt = Node { value = v; prev = l.last; next = Empty } in ( + match l.last with + | Empty -> l.first <- elt + | Node n -> n.next <- elt + ); + l.last <- elt + +let pop = function + | {last = Empty; _} -> failwith "empty list!" + | {last = Node {value; prev; _}; _} as l -> ( + match prev with + | Empty -> (l.last <- Empty; l.first <- Empty) + | Node n -> (l.last <- prev; n.next <- Empty) + ); + value + +let shift = function + | {first = Empty; _} -> failwith "empty list!" + | {first = Node {value; next; _}; _} as l -> ( + match next with + | Empty -> (l.first <- Empty; l.last <- Empty) + | Node n -> (l.first <- next; n.prev <- Empty) + ); + value + +let unshift v l = + let elt = Node { value = v; prev = Empty; next = l.first } in ( + match l.first with + | Empty -> l.last <- elt + | Node n -> n.prev <- elt + ); + l.first <- elt + +let count l = + let rec count_node acc = function + | Empty -> acc + | Node {next = n; _} -> count_node (1 + acc) n + in count_node 0 l.first \ No newline at end of file diff --git a/exercises/linked-list/linked_list.ml b/exercises/linked-list/linked_list.ml new file mode 100644 index 0000000000..e40fd6f129 --- /dev/null +++ b/exercises/linked-list/linked_list.ml @@ -0,0 +1,15 @@ +open Base + +type 'a linked_list + +let empty _ = failwith "'empty' is missing" + +let push _ _ = failwith "'push' is missing" + +let pop _ = failwith "'pop' is missing" + +let shift _ = failwith "'shift' is missing" + +let unshift _ = failwith "'unshift' is missing" + +let count _ = failwith "'count' is missing" diff --git a/exercises/linked-list/linked_list.mli b/exercises/linked-list/linked_list.mli new file mode 100644 index 0000000000..976f151c6c --- /dev/null +++ b/exercises/linked-list/linked_list.mli @@ -0,0 +1,15 @@ +open Base + +type 'a linked_list + +val empty : unit -> 'a linked_list + +val push : 'a -> 'a linked_list -> unit + +val pop : 'a linked_list -> 'a + +val shift : 'a linked_list -> 'a + +val unshift : 'a -> 'a linked_list -> unit + +val count : 'a linked_list -> int \ No newline at end of file diff --git a/exercises/linked-list/test.ml b/exercises/linked-list/test.ml new file mode 100644 index 0000000000..0b8cd6f30b --- /dev/null +++ b/exercises/linked-list/test.ml @@ -0,0 +1,97 @@ +open Base +open OUnit2 +open Linked_list + +let ae exp got _test_ctxt = + assert_equal ~printer:Int.to_string exp got + +let test1 = + let l = empty () in + push 10 l; + push 20 l; + let v1 = pop l in + let v2 = pop l in + ["add/extract elements to the end of the list with push/pop" >:: ae 20 v1; + "add/extract elements to the end of the list with push/pop" >:: ae 10 v2] + +let test2 = + let l = empty () in + push 10 l; + push 20 l; + let v1 = shift l in + let v2 = shift l in + ["extract elements from the beginning of the list with shift" >:: ae 10 v1; + "extract elements from the beginning of the list with shift" >:: ae 20 v2] + +let test3 = + let l = empty () in + unshift 10 l; + unshift 20 l; + let v1 = shift l in + let v2 = shift l in + ["add/extract elements from the beginning of the list with unshift/shift" >:: ae 20 v1; + "add/extract elements from the beginning of the list with unshift/shift" >:: ae 10 v2] + +let test4 = + let l = empty () in + unshift 10 l; + unshift 20 l; + let v1 = pop l in + let v2 = pop l in + ["add/extract elements from the beginning of the list with unshift/shift" >:: ae 10 v1; + "add/extract elements from the beginning of the list with unshift/shift" >:: ae 20 v2] + +let test5 = + let l = empty () in + push 10 l; + push 20 l; + let v1 = pop l in + push 30 l; + let v2 = shift l in + unshift 40 l; + push 50 l; + let v3 = shift l in + let v4 = pop l in + let v5 = shift l in + ["example" >:: ae 20 v1; + "example" >:: ae 10 v2; + "example" >:: ae 40 v3; + "example" >:: ae 50 v4; + "example" >:: ae 30 v5] + +let test6 = + let l = empty () in + let v1 = count l in + push 10 l; + let v2 = count l in + push 20 l; + let v3 = count l in + ["can count its elements" >:: ae 0 v1; + "can count its elements" >:: ae 1 v2; + "can count its elements" >:: ae 2 v3] + +let test7 = + let l = empty () in + push 10 l; + let _ = pop l in + unshift 20 l; + let v1 = count l in + let v2 = pop l in + ["sets head/tail after popping last element" >:: ae 1 v1; + "sets head/tail after popping last element" >:: ae 20 v2] + +let test8 = + let l = empty () in + unshift 10 l; + let _ = shift l in + push 20 l; + let v1 = count l in + let v2 = shift l in + ["sets head/tail after shifting last element" >:: ae 1 v1; + "sets head/tail after shifting last element" >:: ae 20 v2] + +let tests = + test1 @ test2 @ test3 @ test4 @ test5 @ test6 @ test7 @ test8 + +let () = + run_test_tt_main ("linked-list tests" >::: tests) From 6cc34fcc023b1baecf8b84e966ccb12f39a1c0dc Mon Sep 17 00:00:00 2001 From: voila Date: Fri, 18 Oct 2019 21:53:32 +1300 Subject: [PATCH 3/3] Update exercises/linked-list/dune-project Co-Authored-By: Simon Shine --- exercises/linked-list/dune-project | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/linked-list/dune-project b/exercises/linked-list/dune-project index f4f8feafff..7655de0773 100644 --- a/exercises/linked-list/dune-project +++ b/exercises/linked-list/dune-project @@ -1,2 +1 @@ (lang dune 1.1) -(version 1.3.0)