-
Notifications
You must be signed in to change notification settings - Fork 0
Add SICP JS solutions #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
31c4419 to
850b67b
Compare
| require(num_queens <= board_size); | ||
| const possible_positions = enum_list(1, board_size); | ||
|
|
||
| const result = accumulate( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to use accumulate, other than the fact that we want to perform the process num_queens times?
If not, can \ should we use for_each ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, accumulate gives us the positions that we have already, with the variable so_far. It is used in checking whether potential new rows and columns can be added.
09edcc3 to
726cf39
Compare
This PR adds two solutions for the n-queens puzzle using the Source 3 non-deterministic language. Each of them makes use of the generate-and-test paradigm.
The first (
queens_slow) uses non-determinism for generating both the row and column for each position. It does so in an optimized manner, testing the corresponding condition as soon as the choice is generated, thereby reducing the search space. However, this is not enough to yield a quick solution when N = 8. This solution also gives repeated solutions (i.e different permutations that have all the same positions) upon backtracking.The second (
queens_fast) uses non-determinism in picking only the row and not the column of each position, with the columns being fixed. This further reduces the search space, yielding a quick solution when N = 8.Example:
