Parth Khandenath
Parth Khandenath

Reputation: 92

Getting unknown error in SudokuSolver Backtracking problem

I was trying a Backtracking problem (SudokuSolver) I am getting an error which no-one could resolve or even understand. Hence I am seeking help here. The problem is as follows: enter image description here

And the main function/part of my code is: enter image description here

Here my removerow, removecol and removebox functions are removing the numbers which have already occurred in that row, column and sub-box of 3x3 respectively. The error I am getting is very long that is:

enter image description here

Can someone please help me with this? I'm stuck at it since 2 days. Ready to provide further clarification if needed. Please find the entire code here: link

Upvotes: 0

Views: 45

Answers (3)

Joyful Wasp
Joyful Wasp

Reputation: 406

Sorry! I just realized that I answered to the wrong post!

Upvotes: 0

Martin
Martin

Reputation: 391

I have created a test class, to try understand what your functions are doing - or rather, to make sure they do what I think they do...

import unittest
from Otter.Help_Parth.entire_code import tellbox
from Otter.Help_Parth.entire_code import removerow
from Otter.Help_Parth.entire_code import removecol
from Otter.Help_Parth.entire_code import removebox

class MyTestCase(unittest.TestCase):

    def test_tellbox(self):
        """ Put each of the 81 x,y combination to tellbox
            and retrieve the 81 corresponding box-numbers
            assert that the numbers are as expected. """
        lst_results = list()
        for i in range(9):
            for j in range(9):
                lst_results.append(tellbox(i, j))
        self.assertEqual(lst_results, [0, 0, 0, 3, 3, 3, 6, 6, 6, 0, 0, 0, 3, 3, 3, 6, 6, 6, 0, 0, 0, 3, 3, 3, 6, 6, 6, 1, 1, 1, 4, 4, 4, 7, 7, 7, 1, 1, 1, 4, 4, 4, 7, 7, 7, 1, 1, 1, 4, 4, 4, 7, 7, 7, 2, 2, 2, 5, 5, 5, 8, 8, 8, 2, 2, 2, 5, 5, 5, 8, 8, 8, 2, 2, 2, 5, 5, 5, 8, 8, 8])

    def test_removerow(self):
        """ Given a partly filled board, a list and an x,y coordinate
            try call removerow() and assert that the returned list are as expected. """
        board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
                 [4, 5, 6, 7, 8, 9, 1, 2, 3],
                 [7, 8, 9, 1, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0]]
        l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        x, y = 2, 4
        res = removerow(l, board, x, y)
        self.assertEqual([2, 3, 4, 5, 6], res)

    def test_removecol(self):
        """ Given a partly filled board, a list and an x,y coordinate
            try call removecol() and assert that the returned list are as expected. """
        board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
                 [4, 5, 6, 7, 8, 9, 1, 2, 3],
                 [7, 8, 9, 1, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0]]
        l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        x, y = 2, 4
        res = removecol(l, board, x, y)
        self.assertEqual([1, 2, 3, 4, 6, 7, 9], res)

    def test_removebox(self):
        """ Given a partly filled board, a list and an x,y coordinate
            try call removebox() and assert that the returned list are as expected. """
        board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
                 [4, 5, 6, 7, 8, 9, 1, 2, 3],
                 [7, 8, 9, 1, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0, 0]]
        l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        x, y = 2, 4
        bno = tellbox(x, y)
        res = removebox(l, board, x, y, bno)
        self.assertEqual([2, 3], res)

if __name__ == '__main__':
    unittest.main()

It all goes quite well, until removebox(). If you can read and understand my test_removebox(self) Are you kind to confirm that, under the given circumstances, it should in fact return [2, 3]?

Otherwise, please tell what it should return, and why?

When I run the test class, it returns the following:

enter image description here

NB: To be able to run the test class against your code, you need to change it like this:

if __name__ == '__main__':
    board = [[ int(ele) for ele in input().split() ] for i in range(9)]
    ans=solveSudoku(board,0,0)
    if ans:
        print('true')
    else:
        print('false')

Upvotes: 1

Martin
Martin

Reputation: 391

Both your code-sample and the output seems incomplete. We don't know your variables, so I assume 'board' is a list of 81 integers, but what is the mysterious (and newer used) boxl? And why would you set board[x][y] = 0 (in the line with the comment), when you have just gone through the trouble of setting it = ele?

We would be able to help you better, if you provided the entire code, in a run-able form.

Upvotes: 0

Related Questions