Commits

John Chandler  committed cb0a271

Sunday morning refactoring of refine-row. Still looks rough.

  • Participants
  • Parent commits c2eb21b

Comments (0)

Files changed (2)

File src/pseudohkoo/core.clj

     (first value)
     (vec value)))
 
+(defn current-values [row]
+  ; returns the set of known/definite current values for the row
+  (set (filter integer? row)))
+
+(defn required-values [row]
+  ; returns the set of required values for the row
+  ; i.e. the set of numbers needed for a row to be complete
+  (set (range 1 (+ (count row) 1))))
+
+(defn missing-values [row]
+  ; returns the set of numbers that are still missing from the row
+  (clojure.set/difference (required-values row) (current-values row)))
+
 (defn refine-row [row]
   ; return a row with non-definite values refined, where possible
-  (let [required-values (set (range 1 (+ (count row) 1)))
-	current-values  (set (filter integer? row))
-	missing-values  (clojure.set/difference required-values current-values)]
-    (map (fn [cell]
-	   (if (nil? cell)
-	     (format-value missing-values)
-	     (if (vector? cell)
-	       (format-value (clojure.set/intersection missing-values (set cell)))
-	       cell)))
-	 row)))
+  (map (fn [cell]
+	 (if (nil? cell)
+	   (format-value (missing-values row))
+	   (if (vector? cell)
+	     (format-value (clojure.set/intersection (missing-values row) (set cell)))
+	     cell)))
+       row))
 
 (defn refine-grid [grid]
   ; return a "refined" grid - replacing nil cells with definite or possible values as appropriate 
 
 (defn row-complete? [row]
   ; returns true if row has been completed or false if not
-  (let [required-values (set (range 1 (+ (count row) 1)))]
-    (= required-values (set row))))
+  (= (required-values row) (set row)))
 
 (defn grid-complete? [grid]
   ; returns true if grid is finished or false if not

File test/pseudohkoo/test/core.clj

   (is (= [1 2 3, 4 5 6, 7 8 9]         (refine-row [1 2 3, 4 5 6, 7 8 [3 9]]))))
 
 
+;; refine-column
+;(deftest test-refine-column-nearly-done
+  ; test refining of a "nearly done" column i.e. we're missing a value or two
+;  (is (=  (refine-col)))
+;
+;  )
+
+
 ;; refine-grid
 (deftest test-refine-grid-nearly-done
   ; test refinement of a "nearly done" grid i.e. we're missing 9s