Browse Source

Add reverse order interval

 + Allow to treat 1-4 (gives [1;2;3;4]) differently from 4-1 (gives
 [4;3;2;1]).
 + Add corresponding test
Leo 8 years ago
parent
commit
461835777d
2 changed files with 9 additions and 2 deletions
  1. 8 2
      src/id_parsing.ml
  2. 1 0
      src/test/id_parsing_t.ml

+ 8 - 2
src/id_parsing.ml

@@ -74,8 +74,11 @@ let atomise human_ids =
 let deinter = function
   | Unique a -> [Unique a]
   | Between (a,b) ->
-    (* Note that (a-b+1) is the length of interval [a;b] *)
-    List.init (abs(a-b)+1) ~f:(fun i -> Unique (a + i))
+      (* Two use cases to preserve order *)
+      if a < b
+      (* Note that (a-b+1) is the length of interval [a;b] *)
+      then List.init (b-a+1) ~f:(fun i -> Unique (a + i))
+      else List.init (a-b+1) ~f:(fun i -> Unique (a - i))
 ;;
 
 (* Transform string (separated) as follow:
@@ -83,6 +86,9 @@ let deinter = function
  * 1-5 → [1; 2; 3; 4; 5]
  * Multiple occurances should stay, i.e.
  * 1,3,1-4 → [1,3,1,2,3,4] 
+ * Order matters:
+ * 1-3 → [1,2,3]
+ * 3-1 → [3,2,1] *)
 let list_from_human human =
   atomise human
   |> List.map ~f:deinter

+ 1 - 0
src/test/id_parsing_t.ml

@@ -48,6 +48,7 @@ let to_human test solution () =
 let ll_data = [
   ( "1", [1], "Canonical case: unique" );
   ( "1-3", [1;2;3], "Canonical case: between" );
+  ( "3-1", [3;2;1], "Canonical case: between, reversed" );
   ( "1-3,5-8,10-12", [1;2;3;5;6;7;8;10;11;12], "Canonical case: list of interval" );
   ( "1,3,5", [1;3;5], "Canonical case: list of unique" );
   ( "1-3,5,10-12,23", [1;2;3;5;10;11;12;23], "Canonical case: both" );