Docsity

Assignment 6 with Resolution | Programming Languages | CS 401, Assignments of Programming Languages

Toggle sidebar
bg1
CS 405/505 ASSIGNMENT #6
Due Wednesday, March 7, 2007 1
1. A Sudoku (http://www.sudoko.com) puzzle is solved on a 9 ×9 grid, which may be subdi-
vided as a 3 ×3 grid of 3 ×3 grids. The object of the puzzle is to fill in the grid so that
every row, every column, and every 3 ×3 box contains the digits 1 through 9. Usually some
elements of the grid are already filled in when the puzzle begins. An example of such a puzzle
is:
527 6 9 8 1 43
6 9 1 7 43 5 2 8
843215679
2 6 8 5 3 9 7 1 4
975 4 6 1 8 32
1 3 4 8 2 7 9 6 5
356182497
4 8 9 3 76 2 5 1
712 9 5 4 3 86
Note that the digits in bold face are the ones already given.
This problem is to write a set of LISP functions which solve a Sudoku puzzle.
(a) Write a LISP function (puzzle) to return the puzzle above as a list of 9 rows of entries,
each row being a list of 9 entries. Use a nil to indicate the entries which are not set
initially (i.e., those that are not bold).
(b) Write a LISP function (row P N) which takes a puzzle Pand extracts the N-th row, with
N= 1 being the first row. If Pdoes not have an N-th row, then the function returns nil.
The built-in LISP function (nth N L) which returns the N-th element of list L, with
N= 0 being the first row, will be useful. Test your function on the following, assuming
P is the example puzzle given above:
(row (puzzle) 2) ⇒(nil nil 1 7 nil 3 5 nil nil)
(row (puzzle) 9) ⇒(nil 1 nil nil nil nil nil 8 nil)
(row (puzzle) 0) ⇒nil
(c) Write a LISP function (column P N) which takes a puzzle Pand extracts the N-th
column, with N= 1 being the first column. If Pdoes not have an N-th column, then the
function returns nil. Test your function on the following, assuming P is the example
puzzle given above:
(column (puzzle) 2) ⇒(2 nil nil nil 7 nil nil nil 1)
(column (puzzle) 9) ⇒(nil nil 9 nil nil nil 7 nil nil)
(column (puzzle) 0) ⇒nil
(d) Write a LISP function (box P I J) which takes a puzzle Pand extracts the box at
coordinates Iand J, with (I,J) = (1,1) being the first box. If Pdoes not have the
indicated box, then the function returns nil. Test your function on the following,
assuming P is the example puzzle given above:
1This is the absolute deadline for turning in this assignment. No late assignments will be accepted.
pf2

Partial preview of the text

Download Assignment 6 with Resolution | Programming Languages | CS 401 and more Assignments Programming Languages in PDF only on Docsity!

CS 405/505 ASSIGNMENT

Due Wednesday, March 7, 2007 1

  1. A Sudoku (http://www.sudoko.com) puzzle is solved on a 9 × 9 grid, which may be subdi- vided as a 3 × 3 grid of 3 × 3 grids. The object of the puzzle is to fill in the grid so that every row, every column, and every 3 × 3 box contains the digits 1 through 9. Usually some elements of the grid are already filled in when the puzzle begins. An example of such a puzzle is:

5 2 7 6 9 8 1 4 3 6 9 1 7 4 3 5 2 8 8 4 3 2 1 5 6 7 9 2 6 8 5 3 9 7 1 4 9 7 5 4 6 1 8 3 2 1 3 4 8 2 7 9 6 5 3 5 6 1 8 2 4 9 7 4 8 9 3 7 6 2 5 1 7 1 2 9 5 4 3 8 6

Note that the digits in bold face are the ones already given. This problem is to write a set of LISP functions which solve a Sudoku puzzle.

(a) Write a LISP function (puzzle) to return the puzzle above as a list of 9 rows of entries, each row being a list of 9 entries. Use a nil to indicate the entries which are not set initially (i.e., those that are not bold). (b) Write a LISP function (row P N) which takes a puzzle P and extracts the N-th row, with N = 1 being the first row. If P does not have an N-th row, then the function returns nil. The built-in LISP function (nth N L) which returns the N-th element of list L, with N = 0 being the first row, will be useful. Test your function on the following, assuming P is the example puzzle given above: (row (puzzle) 2) ⇒ (nil nil 1 7 nil 3 5 nil nil) (row (puzzle) 9) ⇒ (nil 1 nil nil nil nil nil 8 nil) (row (puzzle) 0) ⇒ nil (c) Write a LISP function (column P N) which takes a puzzle P and extracts the N-th column, with N = 1 being the first column. If P does not have an N-th column, then the function returns nil. Test your function on the following, assuming P is the example puzzle given above: (column (puzzle) 2) ⇒ (2 nil nil nil 7 nil nil nil 1) (column (puzzle) 9) ⇒ (nil nil 9 nil nil nil 7 nil nil) (column (puzzle) 0) ⇒ nil (d) Write a LISP function (box P I J) which takes a puzzle P and extracts the box at coordinates I and J, with (I, J) = (1, 1) being the first box. If P does not have the indicated box, then the function returns nil. Test your function on the following, assuming P is the example puzzle given above:

(^1) This is the absolute deadline for turning in this assignment. No late assignments will be accepted.

(box (puzzle) 1 2) ⇒ (nil nil nil 7 nil 3 nil 1 nil) (box (puzzle) 3 3) ⇒ (4 nil 7 2 nil nil nil 8 nil) (box (puzzle) 2 4) ⇒ nil (e) Write a LISP function (elements L) which takes a row, column or box L and returns the values of the fixed (i.e., a specific integer rather than a list of possibilities, including nil) row, column or box elements as a linear list. Test your function on the following: (elements ’(7 1 2 9 5 4 3 8 6)) ⇒ (7 1 2 9 5 4 3 8 6) (elements ’(2 nil nil nil 7 nil nil nil 1)) ⇒ (2 7 1) (elements ’((2 4 6 9) (2 4 6 9) 1 7 (2 4 6 9) 3 5 (2 4 6 9) (2 4 6 9))) ⇒ (1 7 3 5) (f) Write a LISP function (initialize L) which takes a row, column or box L and returns the row, column or box with all null values set to the initial set of candidates (i.e., those digits not yet fixed). Test your function on the following: (initialize ’(7 1 2 9 5 4 3 8 6)) ⇒ (7 1 2 9 5 4 3 8 6) (initialize ’(2 nil nil nil 7 nil nil nil 1)) ⇒ (2 (3 4 5 6 8 9) (3 4 5 6 8 9) (3 4 5 6 8 9) 7 (3 4 5 6 8 9) (3 4 5 6 8 9) (3 4 5 6 8 9) 1) (initialize ’(nil nil 1 7 nil 3 5 nil nil)) ⇒ ((2 4 6 8 9) (2 4 6 8 9) 1 7 (2 4 6 8 9) 3 5 (2 4 6 8 9) (2 4 6 8 9)) (g) Write a LISP function (update L) which takes a row, column or box L and returns the row, column or box with all fixed digits removed from the candidate sets. Test your function on the following: (update ’(7 1 2 9 5 4 3 8 6)) ⇒ (7 1 2 9 5 4 3 8 6) (update ’(2 (3 4 5 6 8 9) 4 (3 4 5 6 8 9) 7 (3 4 5 6 8 9) 5 (3 4 5 6 8 9) 1)) ⇒ (2 (3 6 8 9) 4 (3 6 8 9) 7 (3 6 8 9) 5 (3 6 8 9) 1) (update ’((2 4 6 8 9) 9 1 7 4 3 5 2 (2 4 6 8 9))) ⇒ ((6 8) 9 1 7 4 3 5 2 (6 8)) (h) You will be provided with a function (solve P) which takes a puzzle P and returns the solution. This function will call the functions you have written above. Run solve to test the system.

  1. Consider the Core program below:

input x; input y; while (x != y) loop if (x < y) then y := y - x; else x := x - y; end if; end loop; output x;

Assuming that the input file contains the integers 28 and 42 (i.e., the list <28, 42>), use the denotational semantics of Core to trace the interpretation of the program.