fork download
  1. n = 5
  2. size = n * n
  3.  
  4. #-----JUST SOME TEST DATA--------------------------------------
  5. import string, itertools, random
  6. original = [''.join(t) for t in itertools.product(string.letters[:n], repeat=2)]
  7. #random.shuffle(original)
  8.  
  9. current = list(original)
  10. random.shuffle(current)
  11.  
  12. def printMatrix(matrix):
  13. for i in range(n):
  14. print(matrix[i * n : (i + 1) * n])
  15.  
  16. print("original:")
  17. printMatrix(original)
  18. print("current:")
  19. printMatrix(current)
  20. #---------------------------------------------------------------------
  21.  
  22. # cache orignal matrix token indices in dictionary
  23. # this dictionary is used by evaluate function
  24. indexInOriginal = {}
  25. for index, value in enumerate(original):
  26. indexInOriginal[value] = index
  27.  
  28. #---------------------------------------------------------------------
  29.  
  30. def indexOf(row, column):
  31. return row * n + column
  32.  
  33. def coordinatesOf(index):
  34. return (index / n, index % n)
  35.  
  36. def distanceModuloN(a, b):
  37. diff = abs(a - b)
  38. return min(diff, n - diff)
  39.  
  40. def cyclicDistanceSquared(indexA, indexB):
  41. ax, ay = coordinatesOf(indexA)
  42. bx, by = coordinatesOf(indexB)
  43. dx = distanceModuloN(ax, bx)
  44. dy = distanceModuloN(ay, by)
  45. return dx * dx + dy * dy
  46.  
  47.  
  48. def evaluate(original, current):
  49. result = 0
  50. print('-' * 68)
  51. print("{0:^20}|{1:^31}|{2:^15}".format("token pair", "distance squared", "product"))
  52. print("{0:^20}|{1:^15}|{2:^15}|".format("", "current", "original"))
  53. print('-' * 68)
  54. for i in range(size):
  55. for j in range(i + 1, size):
  56. distanceSquaredInCurrent = cyclicDistanceSquared(i, j)
  57. distanceSquaredInOriginal = cyclicDistanceSquared(indexInOriginal[current[i]], indexInOriginal[current[j]])
  58. product = distanceSquaredInCurrent * distanceSquaredInOriginal
  59. result += product
  60. print("{0:^20}|{1:^15}|{2:^15}|{3:^15}".format(
  61. (current[i], current[j]),
  62. distanceSquaredInCurrent,
  63. distanceSquaredInOriginal,
  64. product))
  65. print('-' * 68)
  66. print("{0:52}|{1:^15}".format("", result))
  67. # result -= lowerBound[n]
  68. return result
  69.  
  70. evaluate(original, current)
Success #stdin #stdout 0.01s 10164KB
stdin
Standard input is empty
stdout
original:
['aa', 'ab', 'ac', 'ad', 'ae']
['ba', 'bb', 'bc', 'bd', 'be']
['ca', 'cb', 'cc', 'cd', 'ce']
['da', 'db', 'dc', 'dd', 'de']
['ea', 'eb', 'ec', 'ed', 'ee']
current:
['ad', 'be', 'eb', 'da', 'bb']
['ca', 'dd', 'ce', 'cb', 'ab']
['cd', 'bd', 'db', 'ec', 'cc']
['aa', 'dc', 'de', 'ea', 'ba']
['ae', 'bc', 'ac', 'ee', 'ed']
--------------------------------------------------------------------
     token pair     |       distance squared        |    product    
                    |    current    |   original    |
--------------------------------------------------------------------
    ('ad', 'be')    |       1       |       2       |       2       
    ('ad', 'eb')    |       4       |       5       |      20       
    ('ad', 'da')    |       4       |       8       |      32       
    ('ad', 'bb')    |       1       |       5       |       5       
    ('ad', 'ca')    |       1       |       8       |       8       
    ('ad', 'dd')    |       2       |       4       |       8       
    ('ad', 'ce')    |       5       |       5       |      25       
    ('ad', 'cb')    |       5       |       8       |      40       
    ('ad', 'ab')    |       2       |       4       |       8       
    ('ad', 'cd')    |       4       |       4       |      16       
    ('ad', 'bd')    |       5       |       1       |       5       
    ('ad', 'db')    |       8       |       8       |      64       
    ('ad', 'ec')    |       8       |       2       |      16       
    ('ad', 'cc')    |       5       |       5       |      25       
    ('ad', 'aa')    |       4       |       4       |      16       
    ('ad', 'dc')    |       5       |       5       |      25       
    ('ad', 'de')    |       8       |       5       |      40       
    ('ad', 'ea')    |       8       |       5       |      40       
    ('ad', 'ba')    |       5       |       5       |      25       
    ('ad', 'ae')    |       1       |       1       |       1       
    ('ad', 'bc')    |       2       |       2       |       4       
    ('ad', 'ac')    |       5       |       1       |       5       
    ('ad', 'ee')    |       5       |       2       |      10       
    ('ad', 'ed')    |       2       |       1       |       2       
    ('be', 'eb')    |       1       |       8       |       8       
    ('be', 'da')    |       4       |       5       |      20       
    ('be', 'bb')    |       4       |       4       |      16       
    ('be', 'ca')    |       2       |       2       |       4       
    ('be', 'dd')    |       1       |       5       |       5       
    ('be', 'ce')    |       2       |       1       |       2       
    ('be', 'cb')    |       5       |       5       |      25       
    ('be', 'ab')    |       5       |       5       |      25       
    ('be', 'cd')    |       5       |       2       |      10       
    ('be', 'bd')    |       4       |       1       |       4       
    ('be', 'db')    |       5       |       8       |      40       
    ('be', 'ec')    |       8       |       8       |      64       
    ('be', 'cc')    |       8       |       5       |      40       
    ('be', 'aa')    |       5       |       2       |      10       
    ('be', 'dc')    |       4       |       8       |      32       
    ('be', 'de')    |       5       |       4       |      20       
    ('be', 'ea')    |       8       |       5       |      40       
    ('be', 'ba')    |       8       |       1       |       8       
    ('be', 'ae')    |       2       |       1       |       2       
    ('be', 'bc')    |       1       |       4       |       4       
    ('be', 'ac')    |       2       |       5       |      10       
    ('be', 'ee')    |       5       |       4       |      20       
    ('be', 'ed')    |       5       |       5       |      25       
    ('eb', 'da')    |       1       |       2       |       2       
    ('eb', 'bb')    |       4       |       4       |      16       
    ('eb', 'ca')    |       5       |       5       |      25       
    ('eb', 'dd')    |       2       |       5       |      10       
    ('eb', 'ce')    |       1       |       8       |       8       
    ('eb', 'cb')    |       2       |       4       |       8       
    ('eb', 'ab')    |       5       |       1       |       5       
    ('eb', 'cd')    |       8       |       8       |      64       
    ('eb', 'bd')    |       5       |       8       |      40       
    ('eb', 'db')    |       4       |       1       |       4       
    ('eb', 'ec')    |       5       |       1       |       5       
    ('eb', 'cc')    |       8       |       5       |      40       
    ('eb', 'aa')    |       8       |       2       |      16       
    ('eb', 'dc')    |       5       |       2       |      10       
    ('eb', 'de')    |       4       |       5       |      20       
    ('eb', 'ea')    |       5       |       1       |       5       
    ('eb', 'ba')    |       8       |       5       |      40       
    ('eb', 'ae')    |       5       |       5       |      25       
    ('eb', 'bc')    |       2       |       5       |      10       
    ('eb', 'ac')    |       1       |       2       |       2       
    ('eb', 'ee')    |       2       |       4       |       8       
    ('eb', 'ed')    |       5       |       4       |      20       
    ('da', 'bb')    |       1       |       5       |       5       
    ('da', 'ca')    |       5       |       1       |       5       
    ('da', 'dd')    |       5       |       4       |      20       
    ('da', 'ce')    |       2       |       2       |       4       
    ('da', 'cb')    |       1       |       2       |       2       
    ('da', 'ab')    |       2       |       5       |      10       
    ('da', 'cd')    |       8       |       5       |      40       
    ('da', 'bd')    |       8       |       8       |      64       
    ('da', 'db')    |       5       |       1       |       5       
    ('da', 'ec')    |       4       |       5       |      20       
    ('da', 'cc')    |       5       |       5       |      25       
    ('da', 'aa')    |       8       |       4       |      32       
    ('da', 'dc')    |       8       |       4       |      32       
    ('da', 'de')    |       5       |       1       |       5       
    ('da', 'ea')    |       4       |       1       |       4       
    ('da', 'ba')    |       5       |       4       |      20       
    ('da', 'ae')    |       5       |       5       |      25       
    ('da', 'bc')    |       5       |       8       |      40       
    ('da', 'ac')    |       2       |       8       |      16       
    ('da', 'ee')    |       1       |       2       |       2       
    ('da', 'ed')    |       2       |       5       |      10       
    ('bb', 'ca')    |       2       |       2       |       4       
    ('bb', 'dd')    |       5       |       8       |      40       
    ('bb', 'ce')    |       5       |       5       |      25       
    ('bb', 'cb')    |       2       |       1       |       2       
    ('bb', 'ab')    |       1       |       1       |       1       
    ('bb', 'cd')    |       5       |       5       |      25       
    ('bb', 'bd')    |       8       |       4       |      32       
    ('bb', 'db')    |       8       |       4       |      32       
    ('bb', 'ec')    |       5       |       5       |      25       
    ('bb', 'cc')    |       4       |       2       |       8       
    ('bb', 'aa')    |       5       |       2       |      10       
    ('bb', 'dc')    |       8       |       5       |      40       
    ('bb', 'de')    |       8       |       8       |      64       
    ('bb', 'ea')    |       5       |       5       |      25       
    ('bb', 'ba')    |       4       |       1       |       4       
    ('bb', 'ae')    |       2       |       5       |      10       
    ('bb', 'bc')    |       5       |       1       |       5       
    ('bb', 'ac')    |       5       |       2       |      10       
    ('bb', 'ee')    |       2       |       8       |      16       
    ('bb', 'ed')    |       1       |       8       |       8       
    ('ca', 'dd')    |       1       |       5       |       5       
    ('ca', 'ce')    |       4       |       1       |       4       
    ('ca', 'cb')    |       4       |       1       |       4       
    ('ca', 'ab')    |       1       |       5       |       5       
    ('ca', 'cd')    |       1       |       4       |       4       
    ('ca', 'bd')    |       2       |       5       |      10       
    ('ca', 'db')    |       5       |       2       |      10       
    ('ca', 'ec')    |       5       |       8       |      40       
    ('ca', 'cc')    |       2       |       4       |       8       
    ('ca', 'aa')    |       4       |       4       |      16       
    ('ca', 'dc')    |       5       |       5       |      25       
    ('ca', 'de')    |       8       |       2       |      16       
    ('ca', 'ea')    |       8       |       4       |      32       
    ('ca', 'ba')    |       5       |       1       |       5       
    ('ca', 'ae')    |       4       |       5       |      20       
    ('ca', 'bc')    |       5       |       5       |      25       
    ('ca', 'ac')    |       8       |       8       |      64       
    ('ca', 'ee')    |       8       |       5       |      40       
    ('ca', 'ed')    |       5       |       8       |      40       
    ('dd', 'ce')    |       1       |       2       |       2       
    ('dd', 'cb')    |       4       |       5       |      20       
    ('dd', 'ab')    |       4       |       8       |      32       
    ('dd', 'cd')    |       2       |       1       |       2       
    ('dd', 'bd')    |       1       |       4       |       4       
    ('dd', 'db')    |       2       |       4       |       8       
    ('dd', 'ec')    |       5       |       2       |      10       
    ('dd', 'cc')    |       5       |       2       |      10       
    ('dd', 'aa')    |       5       |       8       |      40       
    ('dd', 'dc')    |       4       |       1       |       4       
    ('dd', 'de')    |       5       |       1       |       5       
    ('dd', 'ea')    |       8       |       5       |      40       
    ('dd', 'ba')    |       8       |       8       |      64       
    ('dd', 'ae')    |       5       |       5       |      25       
    ('dd', 'bc')    |       4       |       5       |      20       
    ('dd', 'ac')    |       5       |       5       |      25       
    ('dd', 'ee')    |       8       |       2       |      16       
    ('dd', 'ed')    |       8       |       1       |       8       
    ('ce', 'cb')    |       1       |       4       |       4       
    ('ce', 'ab')    |       4       |       8       |      32       
    ('ce', 'cd')    |       5       |       1       |       5       
    ('ce', 'bd')    |       2       |       2       |       4       
    ('ce', 'db')    |       1       |       5       |       5       
    ('ce', 'ec')    |       2       |       8       |      16       
    ('ce', 'cc')    |       5       |       4       |      20       
    ('ce', 'aa')    |       8       |       5       |      40       
    ('ce', 'dc')    |       5       |       5       |      25       
    ('ce', 'de')    |       4       |       1       |       4       
    ('ce', 'ea')    |       5       |       5       |      25       
    ('ce', 'ba')    |       8       |       2       |      16       
    ('ce', 'ae')    |       8       |       4       |      32       
    ('ce', 'bc')    |       5       |       5       |      25       
    ('ce', 'ac')    |       4       |       8       |      32       
    ('ce', 'ee')    |       5       |       4       |      20       
    ('ce', 'ed')    |       8       |       5       |      40       
    ('cb', 'ab')    |       1       |       4       |       4       
    ('cb', 'cd')    |       5       |       4       |      20       
    ('cb', 'bd')    |       5       |       5       |      25       
    ('cb', 'db')    |       2       |       1       |       2       
    ('cb', 'ec')    |       1       |       5       |       5       
    ('cb', 'cc')    |       2       |       1       |       2       
    ('cb', 'aa')    |       8       |       5       |      40       
    ('cb', 'dc')    |       8       |       2       |      16       
    ('cb', 'de')    |       5       |       5       |      25       
    ('cb', 'ea')    |       4       |       5       |      20       
    ('cb', 'ba')    |       5       |       2       |      10       
    ('cb', 'ae')    |       8       |       8       |      64       
    ('cb', 'bc')    |       8       |       2       |      16       
    ('cb', 'ac')    |       5       |       5       |      25       
    ('cb', 'ee')    |       4       |       8       |      32       
    ('cb', 'ed')    |       5       |       8       |      40       
    ('ab', 'cd')    |       2       |       8       |      16       
    ('ab', 'bd')    |       5       |       5       |      25       
    ('ab', 'db')    |       5       |       4       |      20       
    ('ab', 'ec')    |       2       |       2       |       4       
    ('ab', 'cc')    |       1       |       5       |       5       
    ('ab', 'aa')    |       5       |       1       |       5       
    ('ab', 'dc')    |       8       |       5       |      40       
    ('ab', 'de')    |       8       |       8       |      64       
    ('ab', 'ea')    |       5       |       2       |      10       
    ('ab', 'ba')    |       4       |       2       |       8       
    ('ab', 'ae')    |       5       |       4       |      20       
    ('ab', 'bc')    |       8       |       2       |      16       
    ('ab', 'ac')    |       8       |       1       |       8       
    ('ab', 'ee')    |       5       |       5       |      25       
    ('ab', 'ed')    |       4       |       5       |      20       
    ('cd', 'bd')    |       1       |       1       |       1       
    ('cd', 'db')    |       4       |       5       |      20       
    ('cd', 'ec')    |       4       |       5       |      20       
    ('cd', 'cc')    |       1       |       1       |       1       
    ('cd', 'aa')    |       1       |       8       |       8       
    ('cd', 'dc')    |       2       |       2       |       4       
    ('cd', 'de')    |       5       |       2       |      10       
    ('cd', 'ea')    |       5       |       8       |      40       
    ('cd', 'ba')    |       2       |       5       |      10       
    ('cd', 'ae')    |       4       |       5       |      20       
    ('cd', 'bc')    |       5       |       2       |      10       
    ('cd', 'ac')    |       8       |       5       |      40       
    ('cd', 'ee')    |       8       |       5       |      40       
    ('cd', 'ed')    |       5       |       4       |      20       
    ('bd', 'db')    |       1       |       8       |       8       
    ('bd', 'ec')    |       4       |       5       |      20       
    ('bd', 'cc')    |       4       |       2       |       8       
    ('bd', 'aa')    |       2       |       5       |      10       
    ('bd', 'dc')    |       1       |       5       |       5       
    ('bd', 'de')    |       2       |       5       |      10       
    ('bd', 'ea')    |       5       |       8       |      40       
    ('bd', 'ba')    |       5       |       4       |      20       
    ('bd', 'ae')    |       5       |       2       |      10       
    ('bd', 'bc')    |       4       |       1       |       4       
    ('bd', 'ac')    |       5       |       2       |      10       
    ('bd', 'ee')    |       8       |       5       |      40       
    ('bd', 'ed')    |       8       |       4       |      32       
    ('db', 'ec')    |       1       |       2       |       2       
    ('db', 'cc')    |       4       |       2       |       8       
    ('db', 'aa')    |       5       |       5       |      25       
    ('db', 'dc')    |       2       |       1       |       2       
    ('db', 'de')    |       1       |       4       |       4       
    ('db', 'ea')    |       2       |       2       |       4       
    ('db', 'ba')    |       5       |       5       |      25       
    ('db', 'ae')    |       8       |       8       |      64       
    ('db', 'bc')    |       5       |       5       |      25       
    ('db', 'ac')    |       4       |       5       |      20       
    ('db', 'ee')    |       5       |       5       |      25       
    ('db', 'ed')    |       8       |       5       |      40       
    ('ec', 'cc')    |       1       |       4       |       4       
    ('ec', 'aa')    |       5       |       5       |      25       
    ('ec', 'dc')    |       5       |       1       |       5       
    ('ec', 'de')    |       2       |       5       |      10       
    ('ec', 'ea')    |       1       |       4       |       4       
    ('ec', 'ba')    |       2       |       8       |      16       
    ('ec', 'ae')    |       8       |       5       |      40       
    ('ec', 'bc')    |       8       |       4       |      32       
    ('ec', 'ac')    |       5       |       1       |       5       
    ('ec', 'ee')    |       4       |       4       |      16       
    ('ec', 'ed')    |       5       |       1       |       5       
    ('cc', 'aa')    |       2       |       8       |      16       
    ('cc', 'dc')    |       5       |       1       |       5       
    ('cc', 'de')    |       5       |       5       |      25       
    ('cc', 'ea')    |       2       |       8       |      16       
    ('cc', 'ba')    |       1       |       5       |       5       
    ('cc', 'ae')    |       5       |       8       |      40       
    ('cc', 'bc')    |       8       |       1       |       8       
    ('cc', 'ac')    |       8       |       4       |      32       
    ('cc', 'ee')    |       5       |       8       |      40       
    ('cc', 'ed')    |       4       |       5       |      20       
    ('aa', 'dc')    |       1       |       8       |       8       
    ('aa', 'de')    |       4       |       5       |      20       
    ('aa', 'ea')    |       4       |       1       |       4       
    ('aa', 'ba')    |       1       |       1       |       1       
    ('aa', 'ae')    |       1       |       1       |       1       
    ('aa', 'bc')    |       2       |       5       |      10       
    ('aa', 'ac')    |       5       |       4       |      20       
    ('aa', 'ee')    |       5       |       2       |      10       
    ('aa', 'ed')    |       2       |       5       |      10       
    ('dc', 'de')    |       1       |       4       |       4       
    ('dc', 'ea')    |       4       |       5       |      20       
    ('dc', 'ba')    |       4       |       8       |      32       
    ('dc', 'ae')    |       2       |       8       |      16       
    ('dc', 'bc')    |       1       |       4       |       4       
    ('dc', 'ac')    |       2       |       4       |       8       
    ('dc', 'ee')    |       5       |       5       |      25       
    ('dc', 'ed')    |       5       |       2       |      10       
    ('de', 'ea')    |       1       |       2       |       2       
    ('de', 'ba')    |       4       |       5       |      20       
    ('de', 'ae')    |       5       |       4       |      20       
    ('de', 'bc')    |       2       |       8       |      16       
    ('de', 'ac')    |       1       |       8       |       8       
    ('de', 'ee')    |       2       |       1       |       2       
    ('de', 'ed')    |       5       |       2       |      10       
    ('ea', 'ba')    |       1       |       4       |       4       
    ('ea', 'ae')    |       5       |       2       |      10       
    ('ea', 'bc')    |       5       |       8       |      40       
    ('ea', 'ac')    |       2       |       5       |      10       
    ('ea', 'ee')    |       1       |       1       |       1       
    ('ea', 'ed')    |       2       |       4       |       8       
    ('ba', 'ae')    |       2       |       2       |       4       
    ('ba', 'bc')    |       5       |       4       |      20       
    ('ba', 'ac')    |       5       |       5       |      25       
    ('ba', 'ee')    |       2       |       5       |      10       
    ('ba', 'ed')    |       1       |       8       |       8       
    ('ae', 'bc')    |       1       |       5       |       5       
    ('ae', 'ac')    |       4       |       4       |      16       
    ('ae', 'ee')    |       4       |       1       |       4       
    ('ae', 'ed')    |       1       |       2       |       2       
    ('bc', 'ac')    |       1       |       1       |       1       
    ('bc', 'ee')    |       4       |       8       |      32       
    ('bc', 'ed')    |       4       |       5       |      20       
    ('ac', 'ee')    |       1       |       5       |       5       
    ('ac', 'ed')    |       4       |       2       |       8       
    ('ee', 'ed')    |       1       |       1       |       1       
--------------------------------------------------------------------
                                                    |     5305