Python Forum
a 'simple' program, hard as .... to understand
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a 'simple' program, hard as .... to understand
#2
It's recursive, so let's start at the bottom of the recursion:

if n == 0:
    return [[]]
That will get returned to solve with n = 1 at this point:

smaller_solutions = solve(n - 1) # n - 1 = 0
Then we get to that horrible list comprehension that I think precludes showing this example to beginners:

return [solution + [(n, i + 1)] for i in xrange(BOARD_SIZE) for solution in smaller solutions if not under_attach(i + 1, solution)]
Let's simplify that with the values we know:

return [solution + [(1, i + 1)] for i in xrange(8) for solution in [[]] if not under_attack(i + 1, solution)]
Now, in the second for loop in the horrible list comprehension, we have only one item. So let's simplify that out as well:

return [[] + [(1, i + 1)] for i in xrange(8) if not under_attack(i + 1, [])]
If we look at under_attack with an empty list for the queens parameter, we can see that the loop will not execute and it will always return False. That means that solve with n = 1 will return:

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8)]
So it starts out by putting a queen in each column of the first row. This is exactly why i sucks as a variable name. If i was instead col, it would be clearer that the program is iterating over columns.

So this will be returned to solve with n = 2 and passed to the horrible list comprehension. That will iterate over each queen in the first row, and figure out where we can place queens in the second row so that it does not attack the queen in the first row. Again, note the bad variable name n. If it was instead named row, which it was it represents, it would be clearer what is going on in the program.

This continues up the recursion chain, figuring out where in each row you place a queen such that it doesn't attack the previously placed queens. If there are no places to validly place a queen for a given sub-solution, that sub-solution does not get passed up to the next level of the recursion chain.

And, of course, if they'd bothered to put some comments in their code I wouldn't have to write this mess.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: a 'simple' program, hard as .... to understand - by ichabod801 - Dec-03-2016, 01:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  trying to understand a string literal in a basic server program CompleteNewb 4 2,103 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  Suggestions for a simple data analysis program t4keheart 0 1,782 Mar-08-2021, 03:45 PM
Last Post: t4keheart
  logging in simple program Inkanus 1 1,707 Dec-18-2020, 02:36 PM
Last Post: snippsat
  Newbie needs help with a simple program feynarun 3 2,264 Jan-15-2020, 01:17 PM
Last Post: DeaD_EyE
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,154 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Hard time trying to have clean MySQL to CSV program PierreSoulier 2 2,761 Jul-20-2018, 07:52 AM
Last Post: PierreSoulier
  How hard is this? Phillips45 0 2,106 Jun-11-2018, 05:49 PM
Last Post: Phillips45
  help with simple program juanb007 2 2,728 Dec-07-2017, 02:15 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020