2 kyu
Hard Sudoku Solver
692 of 900f.rodrigues
Loading description...
Games
Algorithms
Game Solvers
Puzzles
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Nice kata, thanks!
When attempting the sample test, my solution succeeds. When pressing the attempt button, my solution succeeds for all of the 9 tests, but I still get a timed out error. There is also a question mark next to the amount of tests I failed. Has this something to do with the way I raise an exception/just inefficient code on my part? Thanks for the help.
Sample tests are usually a very small portion of tests that are run.
This just means that you haven't reached the end of test runner running all tests (due to timeout), so it wasn't sure how many tests there were in total. I was stuck in the same spot last time I tried it, but I know my code wasn't efficient at all, hence the timeout.
Thankyou for your response! I'll keep trying then :)
Hello,
I've got two issues. Timeout and random tests that fail. Timeout is the fun part. Random tests is the strange part and i need some help. If I believe the output of the attempt :
My solution is wrong but I have no idea of the expected.
My solver found this solution :
What should be the expected output ?
Expected is error, because there is more than one valid solution, for example:
If I copied everything correctly, you can see how the rows in the middle band are different.
i cant even describe how i struggled during this f#kin kata, it is almaust destroed my life. few mounths passed i did it, it was terrible but i like this pain. thank you hel yeaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
I really liked solving this kata, but in the end of the day it was super frustrating that time after time optimizations did not result into all tests passing within the time limit! I'd suggest to add a hint to the description about the desired solving speed in the description, so that one may estimate how far is he or she from "getting there".
For me, the solution that passed solved the basic test case in ±30-50ms.
Small hint regarding optimization in Python: try using cProfile to find out which part of the program takes the most time to execute.
I seem to have a problem where the program asks me to mark a grid that seems valid as invalid, like this one:
I am sure this is simple oversight on my part, as many people have solved this in Python and I don't think that everyone has made a mistake but me. Still, I can't find a reason for why this grid should be invalid. If someone could enlighten me I would be very grateful as I have been working on my solution for a week and would love to finally submit it :).
AFAIU, if you have a sudoku with zero empty (zeroed) cells, which doesn't require to be solved, it should be marked as invalid.
It could have been a good test but proving sudoku has single solution requires backtracking, which make the job time out, mainly because of the 200 random tests.
How did others, including me, solved it then?
The tests are randomly generate, you just have to get lucky and not run into bugged one.
All katas have random tests (or should have). You give no proof that tests would be buggy. If you have not been able to solve this kata (I have not either) this cannot be considered a kata issue.
Try to practica on easier ones, 2kyu katas are supposed to be hard coding challenge.
I run my solution 20 times. I got 4 timeouts, and remaining 16 runs had quite comsistent times of 5-8s. So yes, it seems that its possible to get a pessimistic case, but I think the ratio is not terrible, and i also think there is many better solutions than mine.
@akar-0 Thanks for the advice, the difficulty is definitly in the back tracking, but like mentioned, the test results are inconsistent and it makes it hard to debug.
@Erhu, proving that sudoku has multiple solution may be done in different ways and one of them is not that time-consuming. Think of what it means that there are several solutions in terms of the resulting elements positions & values.
According to your tests, this grid is invalid:
[1, 0, 0, 0, 0, 0, 4, 7, 0] [0, 8, 0, 2, 0, 4, 0, 1, 0] [0, 6, 5, 0, 0, 0, 0, 0, 9] [0, 0, 0, 0, 0, 1, 0, 9, 0] [0, 0, 0, 3, 5, 0, 0, 0, 0] [0, 7, 6, 0, 0, 9, 0, 0, 8] [2, 0, 0, 5, 0, 0, 7, 4, 0] [0, 0, 0, 0, 1, 8, 0, 0, 0] [0, 3, 0, 7, 0, 0, 0, 0, 0]
Edit: OK, scratch that, apparently the problem was caused by another grid, not this one. I updated my solution to deal with those scenarios. Is there a way to delete the previous solution if it turned out to be not 100% correct?
In the "single solutions" list, there is the following problem:
Input [6, 0, 0, 0, 0, 0, 0, 0, 2] [0, 0, 3, 6, 0, 1, 7, 0, 0] [0, 7, 0, 0, 4, 0, 0, 1, 0] [0, 5, 0, 9, 0, 4, 0, 3, 0] [0, 0, 9, 0, 0, 0, 1, 0, 0] [0, 6, 0, 7, 0, 8, 0, 2, 0] [0, 3, 0, 0, 6, 0, 0, 5, 0] [0, 0, 5, 3, 0, 9, 4, 0, 0] [7, 0, 0, 0, 0, 0, 0, 0, 3]
to which my program provides the following output:
Output [6, 1, 8, 5, 9, 7, 3, 4, 2] [4, 9, 3, 6, 2, 1, 7, 8, 5] [5, 7, 2, 8, 4, 3, 9, 1, 6] [2, 5, 7, 9, 1, 4, 6, 3, 8] [3, 8, 9, 2, 5, 6, 1, 7, 4] [1, 6, 4, 7, 3, 8, 5, 2, 9] [9, 3, 1, 4, 6, 2, 8, 5, 7] [8, 2, 5, 3, 7, 9, 4, 6, 1] [7, 4, 6, 1, 8, 5, 2, 9, 3]
And yet... it says the solution is invalid. What is going on?
your code passes that test ("single solution"). Either that's not the problem you're facing, or you should reset the trainer.
Closing
Potential an error in the checker script:
The following input generates a false error message "invalid should raise error": I have (possibly) replicated how to the two solutions are found on different paths, but they are the same. Below is my results without early exit; with givens == 27, the question cannot have multiple solutions.
I took another look, the solver is correct; the assumption that givens >= 17 would guarantee a unique solution is falsy.
The two solutions you showed in the original post are not the same, they are different. Have a look at both of them where I've marked Xs.
Therefore there is no unique solution.
Also note that over 500 people have solved this in Python, so the chance that all 500 of them made the same error as the checker in order to pass is very unlikely. I am going to close this, but you can post more evidence if you are convinced there is a problem.
Hi Kacarott, I have corrected myself in the 2nd reply; sorry about the confusion.
As mentioned in the 2nd comment in the question, "the minimum requirement for unique solution is 17 givens" is a bit misleading. 17 is not a condition to have multiple solutions.
cannot cross more than 172 tests should i quite? or keep trying?
there are 40 random tests, with 5 assertions per tests. => up to you to decide, but it looks like you need to change your approach if you wanna succeed here.
This comment has been hidden.
is there any pre solution way to find whether a sudoku has more than one solution ?
@Quark Fox it might be easier if you come to the Discord server for help.
can anyone help this nub? what are the criteria of an in valid sudoku grid(unsolved)
if values out of range (1-9) if given clue < 17 if given clue digit set < 8 ie ((n**2) -1) if cules are other than int if grid dimension is distorted if clues in row col or blocks are repeated if have multiple solution
You need to learn the rules of the game to solve the problem. Considering its rank, it's on you to do that. Yu may find thousands of articles about sudoku on the internet, if you need explanations.
how may i ask following is an invalid sudoku grid?
[0, 0, 0, 0, 0, 6, 4, 0, 0], [0, 4, 5, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 9, 0, 5, 0, 3],
[8, 0, 3, 0, 7, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 1, 8, 0], [0, 0, 1, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 5, 0, 0, 2, 0, 0], [0, 1, 0, 9, 8, 4, 0, 6, 0], [0, 0, 9, 0, 0, 2, 0, 0, 0]
Hi,
Not an issue, a question. And the reason is in the description:
Cheers
Thanks!
But this grid has solvable, has one solution, dimensions are right, clues are okay, number of clues are okay!!
yes, this grid is solvable. You most likely aren't looking at the input causing problems.
I seem to have the same issue. From the description, I checked all these criteria:
There seems to be something I am missing... Any hints?
@bartfastiel: copy your current code and reset the trainer. I updated the feedback yesterday: the input wasn't showing up for tests expecting failure => that grid isn't the one you failed.
@bratfastiel i wish i could help you, but how can i ? when i too am lost!!
Thanks @Blind4Basics and @Quark_Fox for your quick answers. I reset the trainer and tried again. Still the same issue...
My code and https://www.sudokuwiki.org/sudoku.htm says, that there is only one, valid solution:
use this string to import to sudokuwiki.org:
Ya i too have checked it, it has one solution, maybe you picked wrong grid. May be your error is in someother grid,
@Quark_Fox I double checked, that it is the correct grid: The test case's output AND my own System.out.printlns output the same grid right before the "random" test case fails.
Only the random tests fail. The other tests all are consistently green (besides timeouts from time to time).
Also my own, local test cases all are green:
I was wondering, whether it makes a difference whether the constructor or the
solve
method throws the exception on invalid inputs. But that does not seem to be relevant: I tried to do all validations an calculations in either of both methods with still the same troubles (random tests failing, other tests green).This comment has been hidden.
can it flactuate from 152-23? while testing? can i know what is the number i need to cross?
answered below (I guess...)
Okay!!
while testing can same algorithem flactuate between completing as high as 125 then drop to as low as 25-26???
yes: there are random tests, so if your code fails on some specific kind of situation, the number of failing tests will change from one run to the other.
Thanks!
can any one help? what is in valid grid here?
after 6 long and hard days, trying to write in code how i solve sudoku, learning many new tricks of sudoku, trying and failing and trying.... and a thing or two about my limits and wierdness of how computers work, i mean i tried back tracking with while loop, but i knew there is something called backtracking!!! i have been trying to submit my sudoku soloution for last few hours, but getting timeout after completing 23-60 then exiting..... little bit sad!! though no point asking, but i am asking anyway, are the tests okay? it was hard for me ... but i knew i can make it .... but now .... i don't know what to feel or think? how many i 've to pass for a pass? isnt it good enough that it can work out the puzzle anyway!! come on guys!!
This kata is titled "hard" for a reason.
If I see correctly, solution is called by tests 63 times. Tests of my solution take 6-8.5 seconds.
What exactly would be your suggestion?
wrong flag!! no suggestion from my side, but do you have any or can i have some? did you mean total 63 tests?
Thank you for this kata! It was great!
Thanks. For some reason with my code in its current form the following happens:
This makes no sense to me. For some reason unless I force it to skip the single solution tests, it gets stuck there. Does anyone know why this might be please?
If you use Python, you can print and flush it to see some value even if the code halts with a timeout.
print(some_value, flush = True)
Thanks for suggestion. It doesn't seem to be working for me but thanks for replying anywyay. I'll see what I can do
print("balbla", flush=True)
Not a kata issue => closing.
It is frustrating that my code keeps timing out without any indication as to when it was that I timed out. By using older code I am able to see the 10 invalid tests but for some reason I don't even get that far with my new, better code. I know it is better because I ran the invalid tests in Spyder and pass them much much quicker than the older code.
Does anyone know where I can access the random tests please? Also, is the 12000ms limit the total time limit or the time limit per test section? Thanks
12000ms limit is the total time limit.
I am frustrated at the fact that perfectly fine grids are labeled invalid in the test set. This prevents me from complete this kata despite pouring a day of work in it.
Which ones? Proof?
most likely, you were looking at the wrong logs, like other users below. => closing.
what kind of error exactly should I raise in python? I tried exceptions errors runtimeErrors and the test fails me showing me my own error....
raise ValueError('puzzle is unsolvable')
I got an error for this input: Input grid: [4, 7, 0, 3, 0, 2, 0, 6, 0] [0, 0, 9, 0, 0, 0, 2, 0, 0] [0, 8, 0, 0, 0, 0, 7, 0, 0] [0, 5, 0, 0, 1, 9, 0, 0, 0] [0, 0, 0, 6, 0, 5, 0, 0, 0] [0, 0, 0, 2, 8, 0, 0, 5, 0] [0, 0, 3, 0, 0, 0, 0, 9, 0] [0, 0, 2, 0, 0, 0, 8, 0, 0] [0, 4, 0, 8, 0, 6, 0, 7, 2] this is correct puzzle but i have "Invalid grid should raise an error"
The test data contains many incorrect solutions for the invalid grids. Most of them are solveable with one solution, but not all. This adds some boring unintended difficutlties to the Kata.
not an issue.
That grid is solvable. If the tests expect an issue, you're looking at the wrong input.
I got an error for this input: [0, 0, 0, 0, 0, 0, 3, 1, 0] [0, 8, 0, 0, 6, 0, 0, 0, 0] [0, 1, 0, 0, 0, 2, 5, 0, 4] [0, 0, 0, 2, 4, 0, 0, 0, 6] [0, 0, 7, 0, 8, 0, 9, 0, 0] [8, 0, 0, 0, 9, 5, 0, 0, 0] [1, 0, 3, 9, 0, 0, 0, 5, 0] [0, 0, 0, 0, 1, 0, 0, 4, 0] [0, 2, 8, 0, 0, 0, 0, 0, 0] "Invalid grid should raise an error"
However, this is correct puzzle and it has single solution. I guess the test data is not very good?
read the spec.
Not an issue
My code is raising an error for the following input:
[0, 0, 0, 7, 0, 0, 0, 0, 0] [0, 0, 8, 0, 4, 9, 0, 1, 0] [0, 0, 5, 8, 0, 0, 0, 6, 0] [8, 0, 9, 0, 0, 0, 6, 0, 0] [0, 0, 0, 9, 0, 3, 0, 0, 0] [0, 0, 2, 0, 0, 0, 4, 0, 9] [0, 6, 0, 0, 0, 2, 7, 0, 0] [0, 2, 0, 5, 1, 0, 9, 0, 0] [0, 0, 0, 0, 0, 4, 0, 0, 0]
However it seems the input is lacking commas between the elements (And if this was on purpose my code would pass the test since it raises a ValueError).
If I manually add the commas back and run the code on my editor I get the right single solution:
[2, 1, 4, 7, 5, 6, 3, 9, 8], [6, 3, 8, 2, 4, 9, 5, 1, 7], [7, 9, 5, 8, 3, 1, 2, 6, 4], [8, 7, 9, 4, 2, 5, 6, 3, 1], [1, 4, 6, 9, 7, 3, 8, 5, 2], [3, 5, 2, 1, 6, 8, 4, 7, 9], [9, 6, 1, 3, 8, 2, 7, 4, 5], [4, 2, 3, 5, 1, 7, 9, 8, 6], [5, 8, 7, 6, 9, 4, 1, 2, 3]
Any thoughts?
All inputs are valid data structures, there is no "input missing commas". You most likely printed input to the console in a way that removed them.
you solved it in the meantime, so you forgot to close this (invalid) issue.
This comment has been hidden.
This comment has been hidden.
Java version has an issue with random tests reporting puzzles as having invalid grid, even though they are valid and meet all criteria. Recieved a fail for the below puzzle, even though it meets all criteria as a valid puzzle.
randomTests Log Input grid: [0, 0, 8, 0, 0, 9, 0, 0, 0] [9, 0, 0, 7, 1, 0, 4, 0, 0] [0, 0, 0, 4, 0, 0, 7, 0, 0] [1, 0, 9, 0, 4, 0, 0, 8, 0] [0, 5, 0, 0, 0, 0, 0, 1, 0] [0, 3, 0, 0, 2, 0, 5, 0, 7] [0, 0, 6, 0, 0, 5, 0, 0, 0] [0, 0, 7, 0, 8, 6, 0, 0, 3] [0, 0, 0, 1, 0, 0, 2, 0, 0]
Your result: [7, 4, 8, 6, 3, 9, 1, 2, 5] [9, 6, 5, 7, 1, 2, 4, 3, 8] [3, 2, 1, 4, 5, 8, 7, 6, 9] [1, 7, 9, 5, 4, 3, 6, 8, 2] [8, 5, 2, 9, 6, 7, 3, 1, 4] [6, 3, 4, 8, 2, 1, 5, 9, 7] [2, 9, 6, 3, 7, 5, 8, 4, 1] [4, 1, 7, 2, 8, 6, 9, 5, 3] [5, 8, 3, 1, 9, 4, 2, 7, 6]
Invalid grid, should have thrown an IllegalArgumentException.
Are you sure it has only one solution?
I have validated there is only one solution using a different website.
Input string: ..8..9...9..71.4.....4..7..1.9.4..8..5.....1..3..2.5.7..6..5.....7.86..3...1..2..
Website used: https://www.thonky.com/sudoku/solution-count
same here but for python
That grid is valid => you were not looking at the input expecting the error.
Raising an error in Python is not catching the error. Also, it's not stated what kind of error to throw.
it can be any kind of error that you've encountered in the past
wdym raising an error is not catching the error?
What do you mean it is not catching the error? You are probably throwing an error when you should not. In general it doesn't matter which kind of error throw, the tests only control an error is thrown when it should be. I cannot say more because I haven't solved the kata, but this seems very light to raise an issue. You must provide clear examples of bad tests according to you.
No answer, no example, no issue.
Something wrong with Python test cases.
"Invalid grid should raise an error" appears for
board = [ [0, 0, 2, 0, 0, 1, 0, 0, 0], [3, 0, 0, 6, 8, 0, 7, 0, 0], [0, 0, 0, 5, 0, 0, 6, 0, 0], [7, 0, 5, 0, 2, 0, 0, 3, 0], [0, 1, 0, 0, 0, 0, 0, 5, 0], [0, 8, 0, 0, 4, 0, 9, 0, 1], [0, 0, 7, 0, 0, 4, 0, 0, 0], [0, 0, 4, 0, 1, 7, 0, 0, 9], [0, 0, 0, 9, 0, 0, 8, 0, 0]]
This board has only one solution, I tested it with my script and a lot of other submitted scripts. Can anyone suggest what the problem is?
It looks like some of the base grids used to generate unsolvable cases might accidentally generate valid case. :/ That's annoying...
Does it happen often?
edit: are you absolutely sure this is the input causing troubles? (note: I checked, this one is effectively solvable)
This case was from random section, I can't reproduce it.
But I got the same error for another test grid and it looks like I once again confused the sequence of the log and error messages, the user interface is rather unfriendly. This test grid really has multiple solutions, but my scripts return only one. My mistake.
Thanks for your help!
Solved using optimised backtracking algorithm. But sometimes it fails with 'Invalid grid should raise exception' at random tests. I check for multiple solutions. Fixed test with multiple solution passed. What can be wrong with grid that I don't check? P.S. I've managed to submit solutions since it passes sometimes :)
This comment has been hidden.
Can anybody tell me what the "Log" boxes are for in the Output window (Python version, if that matters)? I'm trying to debug a problem that only seems to show up in the random tests where it complains that an "Invalid grid should raise an error."
Occasionally it shows a log box immediately below that showing an input grid and my solution; but I can't tell if that log applies to the red line above it or the green line below. However, the ones I've looked at are all perfectly-valid single-solution cases so far. (Tested on my own graphical solver from 15+ years ago, plus a couple of web solvers just in case.) This, of course, means nothing if the logged input/output grids don't go with the failed test case.
Or, does anyone have an idea how I can find what input grid failed the test? From there I can go offline and find the problem.
Please see if this paragraph helps: https://docs.codewars.com/training/troubleshooting#print-input
Yes, that helps quite a bit. Thank you! I had already found a way to correlate the stderr output I was producing to the error message. The count isn't quite right but I found the case where I'm not seeing a second solution. That guide (now bookmarked!) will help in the future, though.
The previous log entries are still a mystery. I didn't print them in my code, so they must have come from the testing framework. Oh well, not my problem...
Thanks again!
JavaScript translation.
I'm not sure I understand the assignment here. How am I supposed to figure out if a puzzle is unsolvable without searching.. literally every sudoku board that could generate from the seed I'm given?
who said you didn't have to "search"? ;) But there are some case that are clearly invalid too (input validation), so no need to "search" for those.
what is the maxiumum text ms to pass
My code (Python) have checked invalid puzzles and raised errors, something just like below. It throws correct RuntimeErrors on my own Python IDE(3.7.3), but fails Invalid grids test with red info "Invalid grid should raise an error.". Would some experts tell me what is the right way to raise errors for this Kata?
if len([y for x in puzzle for y in x]) != 81: raise RuntimeError("Invalid grid: row/col numbers")
read this line from description carefully
does your
if
check for all of this? I don't think soThank you for the reply. Yes, you are correct, my code show here only check incorrect number of rows/cols, but other codes not show here check "numbers out of 0-9", "dumplicate numbers", "no solution" and so on. It should not fail 9 out of 10 Invalid grids tests with these codes which run normally on my own Python IDE.
Finally I find the root cause. I use iteration to find the solution. If I write only one iteration function to both return solution and raise errors, the problem above occurs. However, if I write two functions, one is iteration function to find the solution, and another function to validate puzzle, raise errors, call iteration function and return the solution, the problem above will never happen. I am afraid if it is an issue for this Kata?
This comment has been hidden.
In which language?
Sorry! Python.
My time is about 1350ms, I think there is still room for improvement:)
Very well! Q.E.D. Dancing links from Algorithm X is the best solution!
my code times out, but the problem is np-complete, wonder what the problem might be...
This comment has been hidden.
This kata asks you to solve sudoku that require guessing and to throw errors if there's more than one solution.
Did not figure out some invalid puzzles. Better to add more explanations. Just passed the test by luck.
I found one solution for each of them.
Why should they throw an IllegalArgumentException??
There are more than 16 Given Numbers and also 8 different numbers given.
The Grid size is valid.
First one has two solutions, which should result in error, so the problem is in your code.
Could you give me a hint what i have to check to find this kind of case ? I know i have to check for at least 17 givens and that at least 8 different numbers are used. But other than that im kinda lost^^
I assume your code already finds a solution? I don't know what approach you took but you can memoize any found solution and let your code continue search, so if it does find any other solution afterwards it throws or if it runs out of options it either returns the only solution or throws if none was found.
Hello I don't understand what should be return in case of error detected
Somebody please open this for C#.
Not a suggestion.
How else am I going to request a C# translation for this Kata?
You don't. If you want some kata to be available in another language, translate it yourself.
For me a suggestion for translation request seems ok.there are not many people accepting them but still it seems for me to be in scope of Suggestion flag.
If the idea seems valid, letting that happen will result in the discourses flooded with this kind of requests. Not sure it's a good idea.
...On the other hand, FArekkusu's answer is as sympathetic and useful as usual: "you cannot solve it in the current languages? no problem! translate it!"
Yeah sure... x/
nice problem :)
You are going to get ban soon so, don't worry;p
for what?
for CHEATING:P
what do you mean?
my favorite 2 kyu problem :)
so hard
.
Failed on "Invalid grid, should have thrown an IllegalArgumentException.". Why I can see the failed puzzle? Looks like others could do that
just print it to the console
thanks
What kind of exception must be thrown on Python? It's not covered in the description and tests
any kind you want, just raise something one way or another.
I get "should be raise error" on puzzle:
But I found only one solution:
I checked four puzzle (rotated 90 degrees) and all solved puzzles are equal.
There are no errors in random tests?
same crap. Spent two nights on a code that checks more than one solution and fits in time. And it still doesn’t work (: my eye starts twitching
@aropan: impossible. This test will expect a soluiton, not an error.
Output log after "Invalid grid should raise an error" is correspond to message?
No, the log appears above the test result.
Oh.. I finished the kata an hour ago and just now saw your message. Well, at least it will help me in the future
Hello,
How many tests can I expect? Right now my result looks like this: ingle solutions (11 of 11 Assertions) Invalid grids (10 of 10 Assertions) Unsolvable ones (2 of 2 Assertions) Random tests (52 of 53 Assertions) Execution Timed Out (12000 ms)
python: 40 random tests with 5 assertions each. Meaning 200 assertions displayed in the random part only. Your code passes 25% of them for now.
This test failed with error: "Invalid grid, should have thrown an IllegalArgumentException."
Grid: [5, 0, 0, 0, 0, 0, 1, 0, 0] [0, 4, 0, 2, 0, 8, 0, 6, 0] [0, 0, 3, 0, 0, 0, 0, 0, 5] [9, 0, 6, 0, 7, 0, 4, 0, 3] [0, 0, 0, 4, 0, 0, 2, 0, 0] [0, 7, 0, 0, 2, 3, 0, 1, 0] [0, 3, 0, 7, 0, 0, 0, 5, 0] [0, 0, 7, 0, 0, 5, 0, 0, 0] [4, 0, 5, 0, 1, 0, 7, 0, 8]
But it has solution!
|5 8 2|3 6 7|1 9 4| |1 4 9|2 5 8|3 6 7|
|7 6 3|1 4 9|8 2 5|
|9 2 6|5 7 1|4 8 3| |3 5 1|4 8 6|2 7 9|
|8 7 4|9 2 3|5 1 6|
|2 3 8|7 9 4|6 5 1| |6 1 7|8 3 5|9 4 2|
|4 9 5|6 1 2|7 3 8|
It has more than one solution:
I'd bet on that, yes.
This comment has been hidden.
You are completely right! Thank you!
Somebody translate to Javascript please
+1
This comment has been hidden.
This comment has been hidden.
but there are fixed tests with multiple solutions... :o => ?
Odd, I might be mistaken
I confirm there are not any, I just modified my code to return after finding one solution, it passes the fixed tests and rarely the random ones too (I had to spam "Attempt" a hundred times). EDIT: my code prechecks that the initial number of filled squares is higher than 17 (otherwise we are sure there are multiple solutions), maybe that's the issue. There should be cases with more than 17 squares filled and multiple solutions.
ruby random test: puzzle: [[8, 0, 0, 5, 0, 0, 0, 0, 3], [0, 0, 5, 0, 6, 0, 1, 0, 0], [7, 0, 0, 0, 0, 1, 0, 2, 4], [0, 5, 0, 0, 8, 0, 3, 0, 0], [1, 0, 0, 0, 0, 0, 2, 0, 7], [0, 0, 7, 0, 2, 0, 0, 4, 0], [5, 7, 0, 3, 0, 0, 0, 0, 6], [0, 0, 3, 0, 4, 0, 7, 0, 0], [4, 0, 0, 0, 0, 5, 0, 0, 9]] my solved: [[8, 2, 1, 5, 7, 4, 6, 9, 3], [3, 4, 5, 2, 6, 9, 1, 7, 8], [7, 9, 6, 8, 3, 1, 5, 2, 4], [2, 5, 4, 9, 8, 7, 3, 6, 1], [1, 3, 9, 4, 5, 6, 2, 8, 7], [6, 8, 7, 1, 2, 3, 9, 4, 5], [5, 7, 8, 3, 9, 2, 4, 1, 6], [9, 1, 3, 6, 4, 8, 7, 5, 2], [4, 6, 2, 7, 1, 5, 8, 3, 9]] why Value is not what was expected??? many random test in ruby seems uncorrect.
[[3, 0, 0, 0, 0, 5, 0, 0, 8], [0, 0, 1, 0, 6, 0, 5, 0, 0], [4, 2, 0, 1, 0, 0, 0, 0, 7], [0, 0, 3, 0, 8, 0, 0, 5, 0], [7, 0, 2, 0, 0, 0, 0, 0, 1], [0, 4, 0, 0, 2, 0, 7, 0, 0], [6, 0, 0, 0, 0, 3, 0, 7, 5], [0, 0, 7, 0, 4, 0, 3, 0, 0], [9, 0, 0, 5, 0, 0, 0, 0, 4]] [[3, 9, 6, 4, 7, 5, 1, 2, 8], [8, 7, 1, 9, 6, 2, 5, 4, 3], [4, 2, 5, 1, 3, 8, 6, 9, 7], [1, 6, 3, 7, 8, 4, 9, 5, 2], [7, 8, 2, 6, 5, 9, 4, 3, 1], [5, 4, 9, 3, 2, 1, 7, 8, 6], [6, 1, 4, 2, 9, 3, 8, 7, 5], [2, 5, 7, 8, 4, 6, 3, 1, 9], [9, 3, 8, 5, 1, 7, 2, 6, 4]] Value is not what was expected
thanks for notice where i am wrong
The first solution is not unique, this one works too:
I suspect the other may be the same case.
according to kata description: Note: The minimum of givens required to create a unique (with no multiple solutions) sudoku game is 17.
so the first one should have unique solution, for it has 29 givens (>17), why there are multiple solutions?
original puzzle is valid, and solvable, and > 17 givens, so it should pass the test. maybe the description is somewhere wrong
That condition is necessary but not enough. You can be sure that with less than 17 you can't have unique solutions, but as you see, with 29 givens you still can have multiple solutions.
Oh, I am so careless about the condition, yes, it is a necessary but not enough condition. Thanks! It is really a hard kata
Nice kata, a good exercise for me.
My solution occasionally solves "invalid" boards generated randomly.
Python version updated with random tests and more tests about validation of the grids too.
Java translation
Consistent with the ruby version. Please, someone, review and approve.
Ruby translation!
.
thx!
This comment has been hidden.
On my computer I was able to pass the given test case within a second, but somehow when I copy the code and attempt the testcase here I get a timeout error. The only reason I can think of is I'm using python 3.6, but here only 3.4 is available. But still, that should not make such a huge efficiency difference right?
This comment has been hidden.
Nope, your solution actually have solved all the sudokus correctly ;-)
Is around 3500 ms a good speed? Has anyone done better?
That's nice. I didn't use any particular sudoku specific algo but get to <6000ms, which I imagine is not bad. I know there is a dancing link algo which might apply to this kind of problems as well.
theDarkBright, I just ran your solution now (twice) and it ran for 4.7 secs. Maybe server load plays a role. I ran mine also, and it runs in 0.7 secs. So there is still room for improvement. I also don't think my implementation is the most optimal one.
I got pretty similar result ~ 3.6s
Thanks for the nice kata. I have a question. Does this kata requires some sort of optimized algorithm? Because my solution works but fails due to the long run. My algorithm is a straigh forward approach. At first I eliminate the obviously wrong candidates for each cell and then I try all the remaining combinations. I would appriciate a hint ;)
There is way to optimize a brutal force approach (with it's being a brutal force approach) Try to think how the runtime is calculated and where you might be able to limit it
Nice kata. However, it lets solutions pass that do not raise an error when a puzzle has multiple solutions. I have submitted two solutions to this kata, one that does not check for multiple solutions and just returns the first it finds, and a second that raises an error when the puzzle has two or more solutions. Both pass the tests, although the first shouldn't have.
The only difference between the two solutions is that the first calls
some()
where the second callsunique()
.seems goood, now.
This comment has been hidden.
not an issue
This comment has been hidden.
This comment has been hidden.
not anymore. ;)
Need a hint on how to detect if a puzzle has multiple solutions?
edit: it looks like backtracking cannot detect this kind of issues, and it's not fast enough either...
I used an optimized backtracking, So it is possible. ;) Think where the runtime grows and how to limit it
I seem to have the same optimation problem using backtracking =/ I have adjusted my solution to:
Hi f.rodrigues,
My solution works on the simple test case then when I try to submit I get:
Passes on Invalid puzzles Passes on Unsolvable puzzle Passes on More than one answer Fails (I think) on Should solve an easy puzzle - not sure, if this is the same as the first test case then I should pass
This is as far as the output goes but I get a StdError suggesting that my code does not handle this last test case.
Are you able to share this last test case?
Thanks
This comment has been hidden.
Oh, didn't see that kata.
In that case, I'd extend it if I were you, maybe make it so that it identifes when you cannot solve the puzzle as well! :)
Sound good, I'll work on it. My solution already does that, It will be easy to adapt.
I noticed that the kata you linked treats sudokus with multiple solutions as valid.
Made changes, try your answer again, this time it should fail. :)
It did fail, for a short while! But all fixed now! :)
P.S. I like the comments in your submission, especially the title, masterpiece indeed! :)
They are not exactly the same. My sudoku solver passes the other kata, but this one for some reason does not pass :(
Feel free to provided test cases that would fail invalid solutions.
Since I'm not a native english speaker it may contain some errors, please provide the correct way and I'll change it!
Thanks.
Hi f.rodrigues,
My solution works on the simple test case then when I try to submit I get:
Passes on Invalid puzzles Passes on Unsolvable puzzle Passes on More than one answer Fails (I think) on Should solve an easy puzzle - not sure, if this is the same as the first test case then I should pass
This is as far as the output goes but I get a StdError suggesting that my code does not handle this last test case.
Are you able to share this last test case?
Thanks