Rate this product

CPE 101: Fundamentals of Computer Science

To further your understanding of iteration and using multidimensional lists, as well as implementing
an exhaustive search algorithm.
For this program, you will be writing a solver for 5×5 Calcudoku puzzles. A Calcudoku puzzle is an
NxN grid where the solution satisfies the following:
Each row can only have the numbers 1 through N with no duplicates
Each column can only have the numbers 1 through N with no duplicates
The sum of the numbers in a cage (areas with a bold border) should equal the number shown in
the upper left portion of the cage
Puzzle input and output files can be downloaded from:
Here is a sample puzzle (left) and its solution (right):
A sample input file is shown below.
9 0 5 6
7 1 2
10 3 8 13
14 4 9 14 19
3 7
8 10 11 16
13 12 17 21 22
5 15 20
1 of 3 3/15/19, 1:27 PM
6 18 23 24
The first line contains the number of cages in the puzzle. After the first line, each subsequent line
describes a cage. The first number of a line is the sum of the cage and all numbers afterward refer to
the positions of the cells that make up the cage. In the puzzle, the cell positions are numbered starting
with 0 for the upper left cell and increase from left to right.
Your program should display a solution to the puzzle output in the following format:
4 1 2 5 3
1 5 4 3 2
2 3 5 4 1
3 4 1 2 5
5 2 3 1 4
Solving a Puzzle
Your program will solve puzzles using an exhaustive search (or “brute force”) approach, in which it
tries (potentially) all possible solutions until it finds the correct one. Your algorithm should perform
the following:
1. Initialize all cells to 0
2. Increment the value in the current cell by 1 (starting from the top-left cell)
If the incremented value is greater than the maximum possible value, set the current cell
to 0 and move back to the previous cell
Otherwise, check if the number is valid. If so, continue to the next cell to the right
(advancing to the next row when necessary)
3. Repeat Step 2 until the puzzle is fully populated and valid
In order for this algorithm to work, you will need to write functions to test if a puzzle is in a valid
state. As you populate the puzzle with numbers, it becomes invalid if:
Duplicates exist in any row or column
The sum of values in a fully populated cage does not equal the required sum
The sum of values in a partially populated cage equals or exceeds the required sum
The main function (with optional helper functions) should perform the following steps:
Store the data from the puzzle input file in a 2D list of integers
Create a 5×5 grid using a 2D list and fill it with zeros
Only one while loop is recommended to validly populate every cell in the grid
transpose(grid: List[List[int]]) – List[List[int]]
2 of 3 3/15/19, 1:27 PM
Return the transposition of the given grid. Assume that the grid has a square number of elements but
make no assumptions about its actual size.
validate_rows(grid: List[List[int]]) – bool
Return True if all rows contain no duplicate positive numbers and False otherwise.
validate_cages(grid: List[List[int]], cages: List[List[int]]) – bool
Return True if the sum of values in a fully populated cage equals the required sum or the sum of
values in a partially populated cage is less than the required sum and False otherwise.
Each puzzle can be found in a separate file:,,, …
Your program should be run using:
python3 <
You should compare your output with the corresponding output files using diff:
test1.out, test2.out, test3.out, …
On a CSL server with and in your current directory:
/home/dkauffma/casey.exe 101 calcudoku
3 of 3 3/15/19, 1:27 PM

Open chat
Need help?
Can we help you?