Commits

John Chandler committed 4fe2f9b

Added code to allow replacing of a column

Comments (0)

Files changed (2)

src/pseudohkoo/core.clj

   ; returns the specified column from the grid
   (map (fn [row] (nth row column-index)) grid))
 
+(defn get-columns [grid]
+  ; retrieve all columns from the grid (this is like rotating the grid by 90 degrees)
+  (map (fn [index] (get-column grid index)) (range (count (first grid)))))
+
 (defn get-row [grid row-index]
   ; returns the specified row from the grid, or [] if row out-of-bounds
   (nth grid row-index []))
 	tail (nthnext row (+ column-index 1))]
     (concat head [new-value] tail)))
 
+(defn replace-column [grid column-index new-column]
+  ; returns a copy of the grid with the given column replaced by the new one
+  (map
+   (fn [index]
+     (update-row (get-row grid index) column-index (nth new-column index)))
+   (range (count new-column))))
+
 (defn get-cell [grid row-index column-index]
   ; returns the value of the grid cell at the given location, or nil if out-of-bounds
   (nth (get-row grid row-index) column-index nil))
   ; return a "refined" grid - replacing nil cells with definite or possible values as appropriate 
   (map refine-row grid))
 
+; get each column
+; refine
+; replace columns in grid
+
 (defn row-complete? [row]
   ; returns true if row has been completed or false if not
   (= (required-values row) (set row)))

test/pseudohkoo/test/core.clj

     (is (= [1 2 3] (get-column grid 0)))))
 
 
+;; get-columns
+(deftest test-get-columns
+  ; retrieve all columns from the grid (this is like rotating the grid by 90 degrees)
+  (let [grid     [[1 4 7]
+	          [2 5 8]
+	          [3 6 9]]
+	expected [[1 2 3]
+		  [4 5 6]
+		  [7 8 9]]
+       ]
+    (is (= expected (get-columns grid)))))
+
+
+;; replace-column
+(deftest test-replace-column
+  ; replace a given column with a new one
+  (let [grid     [[1 1 1]
+	          [1 1 1]
+	          [1 1 1]]
+	expected [[2 1 1]
+		  [3 1 1]
+		  [4 1 1]]
+       ]
+    (is (= expected (replace-column grid 0 [2 3 4])))))
+
+
 ;; get-row
 (deftest test-get-row
   ; retrieve a row from the grid
 	       [  2   3   4,   5   6   7,   8  nil  1]]
 	result (refine-grid grid)
       ]
-    (is (= [1 2 3, 4 5 6, 7 8 9] (nth result 0)))
+    (is (= [1 2 3, 4 5 6, 7 8 9] (nth result 0)))))
 
-    )
-  )
+;(deftest test-refine-grid-columns
+  ; test refinement of a "nearly done" grid needing help from both row and column refinement
+;  (let [grid  [[  1   2   3,   4   5   6,   7 nil nil]
+;	       [nil   1   2,   3   4   5,   6   7   8]
+;	       [  8 nil   1,   2   3   4,   5   6   7]
+;	       [  7   8 nil,   1   2   3,   4   5   6]
+;	       [  6   7   8, nil   1   2,   3   4   5]
+;	       [  5   6   7,   8 nil   1,   2   3   4]
+;	       [  4   5   6,   7   8 nil,   1   2   3]
+;	       [  3   4   5,   6   7   8, nil   1   2]
+;	       [  2   3   4,   5   6   7,   8   9  1]]
+;	result (refine-grid grid)
+;      ]
+;    (is (= [1 2 3, 4 5 6, 7 8 9] (nth result 0)))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.