Jarvis icon indicating copy to clipboard operation
Jarvis copied to clipboard

Add sudoku support

Open pranavprakash20 opened this issue 4 years ago • 1 comments

Adding support for solving sudoku puzzles.

Sample input : 3 0 6 5 0 8 4 0 0 5 2 0 0 0 0 0 0 0 0 8 7 0 0 0 0 3 1 0 0 3 0 1 0 0 8 0 9 0 0 8 6 3 0 0 5 0 5 0 0 9 0 6 0 0 1 3 0 0 0 0 2 5 0 0 0 0 0 0 0 0 7 4 0 0 5 2 0 6 3 0 0 ~> Hi, what can I do for you? sudoku /home/ws/pranav/sudoku_input 3 1 6 5 2 8 4 9 7 5 2 1 3 4 7 8 6 9 2 8 7 6 5 4 9 3 1 6 4 3 9 1 5 7 8 2 9 7 2 8 6 3 1 4 5 7 5 8 4 9 1 6 2 3 1 3 4 7 8 9 2 5 6 8 6 9 1 3 2 5 7 4 4 9 5 2 7 6 3 1 8

pranavprakash20 avatar Jun 07 '21 12:06 pranavprakash20

Really nice! Like it.

Just some smaller notices (parsing a file is always tricky!):

Crashes if for example one line has leading zeros or contains illegal characters:

Some error occurred, please open an issue on github!
Here is error:

Traceback (most recent call last):
  File "/home/phil/git/Jarvis/jarviscli/CmdInterpreter.py", line 286, in try_do
    do(self, s)
  File "/home/phil/git/Jarvis/jarviscli/plugin.py", line 208, in run
    self._backend[0](jarvis.get_api(), s)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 40, in __call__
    self.sudoku.append([int(i) for i in line.split(" ")])
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 40, in <listcomp>
    self.sudoku.append([int(i) for i in line.split(" ")])
ValueError: invalid literal for int() with base 10: '\n'

Crahses if numbers are missing:

Some error occurred, please open an issue on github!
Here is error:

Traceback (most recent call last):
  File "/home/phil/git/Jarvis/jarviscli/CmdInterpreter.py", line 286, in try_do
    do(self, s)
  File "/home/phil/git/Jarvis/jarviscli/plugin.py", line 208, in run
    self._backend[0](jarvis.get_api(), s)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 43, in __call__
    if self.solve():
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 59, in solve
    return self.solve(row, col + 1)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 68, in solve
    if self.solve(row, col + 1):
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 59, in solve
    return self.solve(row, col + 1)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 59, in solve
    return self.solve(row, col + 1)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 68, in solve
    if self.solve(row, col + 1):
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 59, in solve
    return self.solve(row, col + 1)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 59, in solve
    return self.solve(row, col + 1)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 68, in solve
    if self.solve(row, col + 1):
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 63, in solve
    if self._is_valid(row, col, num):
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 93, in _is_valid
    if self.sudoku[i][col] == num:
IndexError: list index out of range

Crashes if file does not exists or no parameter given

~> Hi, what can I do for you?
sudoku
Some error occurred, please open an issue on github!
Here is error:

Traceback (most recent call last):
  File "/home/phil/git/Jarvis/jarviscli/CmdInterpreter.py", line 286, in try_do
    do(self, s)
  File "/home/phil/git/Jarvis/jarviscli/plugin.py", line 208, in run
    self._backend[0](jarvis.get_api(), s)
  File "/home/phil/git/Jarvis/jarviscli/plugins/sodoku.py", line 37, in __call__
    with open(sudoku, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: ''

(Maybe add a try-except for above issues and minimum print "invalid input file")

And causes infinity loop, if sudoku is unsolvable (e.g. input file contains twice same number in one line)

pnhofmann avatar Jun 15 '21 13:06 pnhofmann