daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. # ask the user for an N value
  3. # generate a candidate NQ solution [random.randint(0,n-1) for x in range(n)]
  4. # define a function to count number of conflicts()
  5. # while number of conflicts in NQ > 0
  6.     # randomize (or improve) NQ
  7. # print NQ
  8. # print number of iterations
  9.    
  10. #ask for n value
  11. n = input("Give me a board dimension: ")
  12. n = int(n)
  13. # generate a candidate NQ solution [random.randint(0,n-1) for x in range(n)]
  14. nq = [random.randint(0,n-1) for x in range(n)]
  15.  
  16. print(nq)
  17. # define a function to count number of conflicts()
  18. def count_conflicts( nq ):
  19.     for i in range( len(nq)-1):
  20.         for j in range(i+1,len(nq) ):
  21.             if abs(i-j)==abs(nq[i]-nq[j]):
  22.                 global conflicts
  23.                 conflicts += 1
  24.         return conflicts
  25.  
  26.  
  27. #print(conflicts)
  28. x = count_conflicts(nq)
  29. print(x)
  30.  
  31. # while number of conflicts in NQ > 0
  32. # randomize (or improve) NQ
  33. while (conflicts > 0):
  34.     nq = [random.randint(0,n-1) for x in range(n)]
  35. # print NQ
  36. print(nq)
  37.    
  38. Traceback (most recent call last):
  39.   File "C:/Users/wills/AppData/Local/Programs/Python/Python36-32/lasttry.py", line 25, in <module>
  40.     x = count_conflicts(nq)
  41.   File "C:/Users/wills/AppData/Local/Programs/Python/Python36-32/lasttry.py", line 20, in count_conflicts
  42.     conflicts += 1
  43. NameError: name 'conflicts' is not defined
RAW Paste Data
Top