fork download
  1. #!/usr/bin/python
  2.  
  3.  
  4. def printListOfList(l):
  5. """Just a nicer print method"""
  6. print("{")
  7. for row in l:
  8. print(row)
  9. print("}")
  10.  
  11. def compute(left, top):
  12. """Use dynamic programming wow!"""
  13. horiz_pen = []
  14. vert_pen = []
  15. diag_pen = []
  16. arrow = []
  17. final = []
  18.  
  19. # Initialze left border
  20. for l in range(len(left)+1):
  21. final.append([])
  22. final[l].append(-2*l)
  23.  
  24. # Initialize top border
  25. for t in range(len(top)+1):
  26. if t != 0:
  27. final[0].append(-2*t)
  28.  
  29. print('Initialized State:')
  30. printListOfList(final)
  31.  
  32. # Work from the top down
  33. for l in range(len(left)):
  34.  
  35. #Penalty lists start empty. We must create an empty list in preparation for the following for loop
  36. horiz_pen.append([])
  37. vert_pen.append([])
  38. diag_pen.append([])
  39. arrow.append([])
  40.  
  41. #Work from the left to the right
  42. for t in range(len(top)):
  43.  
  44. match_point = 1 if left[l] == top[t] else -1
  45.  
  46. horiz_pen[l].append(final[l][t+1] -2)
  47. vert_pen[l].append(final[l+1][t] -2)
  48. diag_pen[l].append(final[l][t] + match_point)
  49.  
  50. mscore = max(horiz_pen[l][t], vert_pen[l][t], diag_pen[l][t])
  51. if mscore == horiz_pen[l][t]:
  52. arrow[l].append('h')
  53. elif mscore == vert_pen[l][t]:
  54. arrow[l].append('v')
  55. elif mscore == diag_pen[l][t]:
  56. arrow[l].append('d')
  57. else:
  58. raise Exception('bad')
  59.  
  60. final[l+1].append(mscore)
  61.  
  62. print('horiz_pen')
  63. printListOfList(horiz_pen)
  64. print('vert_pen')
  65. printListOfList(vert_pen)
  66. print('diag_pen')
  67. printListOfList(diag_pen)
  68. print('final')
  69. printListOfList(final)
  70. print('arrow')
  71. printListOfList(arrow)
  72.  
  73. if __name__ == '__main__':
  74. compute('CGATCG', 'CAAGAC')
  75.  
Success #stdin #stdout 0.02s 9248KB
stdin
Standard input is empty
stdout
Initialized State:
{
[0, -2, -4, -6, -8, -10, -12]
[-2]
[-4]
[-6]
[-8]
[-10]
[-12]
}
horiz_pen
{
[-4, -6, -8, -10, -12, -14]
[-1, -3, -5, -7, -9, -11]
[-3, -2, -4, -4, -6, -8]
[-5, -2, -1, -3, -3, -5]
[-7, -4, -3, -2, -4, -4]
[-9, -6, -5, -4, -3, -3]
}
vert_pen
{
[-4, -1, -3, -5, -7, -9]
[-6, -3, -2, -4, -4, -6]
[-8, -5, -2, -1, -3, -3]
[-10, -7, -4, -3, -2, -4]
[-12, -9, -6, -5, -4, -3]
[-14, -11, -8, -7, -4, -5]
}
diag_pen
{
[1, -3, -5, -7, -9, -9]
[-3, 0, -2, -2, -6, -8]
[-5, 0, 1, -3, -1, -5]
[-7, -4, -1, 0, -2, -2]
[-7, -6, -3, -2, -1, -1]
[-11, -8, -5, -2, -3, -2]
}
final
{
[0, -2, -4, -6, -8, -10, -12]
[-2, 1, -1, -3, -5, -7, -9]
[-4, -1, 0, -2, -2, -4, -6]
[-6, -3, 0, 1, -1, -1, -3]
[-8, -5, -2, -1, 0, -2, -2]
[-10, -7, -4, -3, -2, -1, -1]
[-12, -9, -6, -5, -2, -3, -2]
}
arrow
{
['d', 'v', 'v', 'v', 'v', 'v']
['h', 'd', 'v', 'd', 'v', 'v']
['h', 'd', 'd', 'v', 'd', 'v']
['h', 'h', 'h', 'd', 'v', 'd']
['h', 'h', 'h', 'h', 'd', 'd']
['h', 'h', 'h', 'd', 'h', 'd']
}