Why is my recursive function not outputting anything?

Reply

Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Why is my recursive function not outputting anything?

 
0
  #1
Apr 7th, 2009
  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?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Why is my recursive function not outputting anything?

 
1
  #2
Apr 9th, 2009
You're having an entity vs value comparison issue:

  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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Why is my recursive function not outputting anything?

 
0
  #3
Apr 9th, 2009
If the coordinates were tuple (instead of lists) python would compare the values correctly:

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Why is my recursive function not outputting anything?

 
0
  #4
Apr 9th, 2009
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.
  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.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC