Post Reply 
Fifteen puzzle solvability, Numworks Python
04-09-2020, 03:06 PM
Post: #7
RE: Fifteen puzzle solvability, Numworks Python
(04-09-2020 01:24 PM)stored Wrote:  May be like this, but not so concise.
Code:
def fifteen(a):
  b = 0
  for i in range(16):
    if a[i] == 0:
      b += (i + 3) // 4
  for i in range(15):
    for j in range(i+1, 16):
      b += (a[j] < a[i]) * (a[j] != 0)
  return b

I like this approach, but it returns the wrong value for selected cases:

[0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1] returns 105, should be 106
[0,14,13,7,15,12,8,6,11,9,5,2,10,4,3,1] returns 86, should be 87
[1,10,11,2,9,8,7,12,0,5,6,13,4,15,14,3] returns 47, should be 48
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Fifteen puzzle solvability, Numworks Python - Don Shepherd - 04-09-2020 03:06 PM



User(s) browsing this thread: 1 Guest(s)