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
#1
Hi, I'm learning python and I went thru some code that was under the title 'simple python programs' and found this.

BOARD_SIZE = 8

def under_attack(col, queens):
   left = right = col

   for r, c in reversed(queens):
       left, right = left - 1, right + 1

       if c in (left, col, right):
           return True
   return False

def solve(n):
   if n == 0:
       return [[]]

   smaller_solutions = solve(n - 1)

   return [solution+[(n,i+1)]
       for i in xrange(BOARD_SIZE)
           for solution in smaller_solutions
               if not under_attack(i+1, solution)]
for answer in solve(BOARD_SIZE):
   print answer
It was my intention to read thru this and a few other programs to see if I could understand them, gain some insight into how python works. If I failed, I could use pyCharm debugger to walk thru each line step by step.

I have to say, even using the debugger, I find this code cryptic.
Is it just me? I'd like to know if experienced python coders can read this code and within a short time understand it.
It has what I'd term 'generator' terms, and these terms are embedded \ implicit. i.e.

Will I be expected to work with code like this in a junior level python job? Cos if I am, I think I'd better quit now and go back to C !

Didn't someone say 'explicit is best' ( with reference to declaring 'self' to be a parameter passed to itself in instance methods ) ? Python is the most implicit language I tried to learn, except prolog. But where prolog is all implicit, python seems to be a horribly confusing mix of implicit and explicit. Powerful I'm sure, but extremely hard to read.

its the 8 queens on a chess board problem, but knowing this I still couldn't understand it.
Reply


Messages In This Thread
a 'simple' program, hard as .... to understand - by meems - Dec-02-2016, 11:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  trying to understand a string literal in a basic server program CompleteNewb 4 2,105 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  Suggestions for a simple data analysis program t4keheart 0 1,784 Mar-08-2021, 03:45 PM
Last Post: t4keheart
  logging in simple program Inkanus 1 1,713 Dec-18-2020, 02:36 PM
Last Post: snippsat
  Newbie needs help with a simple program feynarun 3 2,272 Jan-15-2020, 01:17 PM
Last Post: DeaD_EyE
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,165 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Hard time trying to have clean MySQL to CSV program PierreSoulier 2 2,766 Jul-20-2018, 07:52 AM
Last Post: PierreSoulier
  How hard is this? Phillips45 0 2,107 Jun-11-2018, 05:49 PM
Last Post: Phillips45
  help with simple program juanb007 2 2,734 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