943,659 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 517
  • Python RSS
Apr 7th, 2009
0

Why is my recursive function not outputting anything?

Expand Post »
python Syntax (Toggle Plain Text)
  1. #!/usr/local/bin/python
  2. #Scott Landau
  3. #CS 380
  4. #Assignment 1 Programming Portion in Python
  5. #Created - 4/6/09
  6. #Last Edited - 4/7/09
  7.  
  8. #n is going to be equal to 4 for this queens problem.
  9. n = 4
  10.  
  11. #Assigning the empty list to initialState.
  12. initialState = []
  13.  
  14. #Making an allDirections list.
  15. allDirections = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]
  16.  
  17. #declare the list 'state' which represents the number of non threatening
  18. #queens that have been placed on the board.
  19. state = []
  20.  
  21. def goal(state):
  22. if (len(state) == n):
  23. return True
  24. else:
  25. return False
  26.  
  27. #defining allRules list which the contents of which will be created in next function.
  28. allRules = []
  29. #defining all the rules for an nxn board. assigning these rules to allRules
  30. def makeRules(n):
  31. for i in range(1, (n+1), 1):
  32. for j in range(1, (n+1), 1):
  33. allRules.append([i,j])
  34. return
  35.  
  36. #making a rule list. this list will probably be refined in future assignments but it will contain the one rule that is applicable in the current state. for #now we will just make it an empty list.
  37. rule = []
  38. #this function will apply the rule that is in the rule list to the given state, which just appends it to the state list.
  39. def applyRule(rule,state):
  40. state.append(rule)
  41. return
  42.  
  43. #returns true if the row or column of 'pos' is out of bounds.
  44. def outOfBounds(pos):
  45. if ((pos[0] < 1) or (pos[0] > n) or (pos[1] < 1) or (pos[1] > n)):
  46. return True
  47. else:
  48. return False
  49.  
  50. #function that will determine which positions queens cannot be placed into because they might eventually run into another queen already on the board.
  51. def blocked(pos,state,direction):
  52. newpos = [(pos[0]+direction[0]),(pos[1]+direction[1])]
  53. for k in range(0, (len(state)), 1):
  54. if (newpos == state[k]):
  55. return True
  56. print "True"
  57. elif outOfBounds(newpos):
  58. return False
  59. print "False"
  60. else:
  61. blocked(newpos,state,direction)
  62.  
  63. state = [[1,2],[2,0]]
  64. direction = [1,0]
  65. pos = [1,0]
  66. blocked(pos,state,direction)

That last function there is the one I am having problems with, I think. This is the N-Queens chess board problem, with a 4x4 board, where you have to place n queens so none of them can attack each other. This is just the basics to the whole problem but I just want to have a working "blocked" function. I thought my functions theory was correct, along with the code, but I tried putting test print lines in there to see if it was outputting anything and I am getting no output.

Any help?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Apr 9th, 2009
1

Re: Why is my recursive function not outputting anything?

You're having an entity vs value comparison issue:

python Syntax (Toggle Plain Text)
  1. a = [[1,2],[2,0]]
  2. b = [2,0]
  3. aa = a[1]
  4. # at this point, b = [2,0] and aa = [2,0]
  5. # but aa == b returns False
  6.  
  7. # aa[0] == b[0] returns True
  8. # aa[1] == b[1] returns True

Python is comparing the list entities and not the list values.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Apr 9th, 2009
0

Re: Why is my recursive function not outputting anything?

If the coordinates were tuple (instead of lists) python would compare the values correctly:

python Syntax (Toggle Plain Text)
  1. a = [(1,2),(2,0)]
  2. b = (2,0)
  3. aa = a[1]
  4. # now aa == b returns True
  5.  
  6. #alternatively, though probably slightly less efficient, you could convert the coordinates to tuples at the point of comparison
  7. # tuple(aa) == tuple(b) would return True in the first example code.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Apr 9th, 2009
0

Re: Why is my recursive function not outputting anything?

For your example, the position is found, i.e. True is returned. Consider using "in" when comparing the two lists as that is the preferred method. The following illustrates this.
Python Syntax (Toggle Plain Text)
  1. a = [ [0,1], [0,2], [1,2] ]
  2. b = [0, 2,]
  3. c = [2, 0]
  4.  
  5. if b in a:
  6. print "b found"
  7. else:
  8. print "b not found"
  9.  
  10. if c in a:
  11. print "c found"
  12. else:
  13. print "c not found"
What output were you expecting from the program?. The program does not print "true" because you return, or exit the function on the line before the print statement (Doh). This modified program shows that the position is indeed found.
Python Syntax (Toggle Plain Text)
  1. #n is going to be equal to 4 for this queens problem.
  2. n = 4
  3.  
  4. #Assigning the empty list to initialState.
  5. initialState = []
  6.  
  7. #Making an allDirections list.
  8. allDirections = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]
  9.  
  10. #declare the list 'state' which represents the number of non threatening
  11. #queens that have been placed on the board.
  12. state = []
  13.  
  14. def goal(state):
  15. if (len(state) == n):
  16. return True
  17. else:
  18. return False
  19.  
  20. #defining allRules list which the contents of which will be created in next function.
  21. allRules = []
  22. #defining all the rules for an nxn board. assigning these rules to allRules
  23. def makeRules(n):
  24. for i in range(1, (n+1), 1):
  25. for j in range(1, (n+1), 1):
  26. allRules.append([i,j])
  27. return
  28.  
  29. #making a rule list. this list will probably be refined in future assignments but it will contain the one rule that is applicable in the current state. for #now we will just make it an empty list.
  30. rule = []
  31. #this function will apply the rule that is in the rule list to the given state, which just appends it to the state list.
  32. def applyRule(rule,state):
  33. state.append(rule)
  34. return
  35.  
  36. #returns true if the row or column of 'pos' is out of bounds.
  37. def outOfBounds(pos):
  38. if ((pos[0] < 1) or (pos[0] > n) or (pos[1] < 1) or (pos[1] > n)):
  39. return True
  40. else:
  41. return False
  42.  
  43. #function that will determine which positions queens cannot be placed into because they might eventually run into another queen already on the board.
  44. def blocked(pos,state,direction):
  45. newpos = [(pos[0]+direction[0]),(pos[1]+direction[1])]
  46. ## for k in range(0, (len(state)), 1):
  47. ## if (newpos == state[k]):
  48. print "looking for", newpos
  49. print state, "\n"
  50. if newpos in state:
  51. return True
  52. print "True"
  53. elif outOfBounds(newpos):
  54. return False
  55. print "False"
  56. else:
  57. blocked(newpos,state,direction)
  58.  
  59. state = [[1,2],[2,0]]
  60. direction = [1,0]
  61. pos = [1,0]
  62. ##
  63. ## added a variable that contains the value returned by the function
  64. result = blocked(pos,state,direction)
  65. if result == True:
  66. print "position found"
  67. elif result == False:
  68. print "position not found"
  69. else: ## "None" (nothing returned)
  70. print "position added"
Last edited by woooee; Apr 9th, 2009 at 4:31 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: So, list or tuple or other?
Next Thread in Python Forum Timeline: Error msg, when doing a function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC