Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. master = [1,3,9,8,3,4,5]
  2. addition = [3,4,5,7,8]
  3.  
  4. def merge(master, addition):
  5. n = 1
  6. while n < len(master):
  7. if master[-n:] == addition[:n]:
  8. return master + addition[n:]
  9. n += 1
  10. return master + addition
  11.  
  12. def merge(a, b):
  13. max_offset = len(b) # can't overlap with greater size than len(b)
  14. for i in reversed(range(max_offset+1)):
  15. # checks for equivalence of decreasing sized slices
  16. if a[-i:] == b[:i]:
  17. break
  18. return a + b[i:]
  19.  
  20. test_data = [{'a': [1,3,9,8,3,4,5], 'b': [3,4,5,7,8], 'result': [1,3,9,8,3,4,5,7,8]},
  21. {'a': [9, 1, 1, 8, 7], 'b': [8, 6, 7], 'result': [9, 1, 1, 8, 7, 8, 6, 7]}]
  22.  
  23. all(merge(test['a'], test['b']) == test['result'] for test in test_data)
  24.  
  25. def merge(a, b):
  26. if a[-1] not in b:
  27. return a + b
  28. ...
  29.  
  30. def merge(a, b):
  31. while True:
  32. try:
  33. idx = b.index(a[-1]) + 1 # leftmost occurrence of a[-1] in b
  34. except ValueError: # a[-1] not in b
  35. return a + b
  36. if a[-idx:] == b[:idx]:
  37. return a + b[:idx]
  38.  
  39. a = [1,2,3,4,1,2,3,4]
  40. b = [3,4,1,2,3,4,5,6]
  41. # result should be [1,2,3,4,1,2,3,4,5,6], but
  42. # this algo produces [1,2,3,4,1,2,3,4,1,2,3,4,5,6]
  43.  
  44. def merge(a, b):
  45. results = []
  46. while True:
  47. try:
  48. idx = b.index(a[-1]) + 1 # leftmost occurrence of a[-1] in b
  49. except ValueError: # a[-1] not in b
  50. results.append(a + b)
  51. break
  52. if a[-idx:] == b[:idx]:
  53. results.append(a + b[:idx])
  54. return min(results, key=len)
  55.  
  56. >>> a = [1, 3, 9, 8, 3, 4, 5]
  57. >>> b = [3, 4, 5, 7, 8]
  58.  
  59. >>> matches = (i for i in xrange(len(b), 0, -1) if b[:i] == a[-i:])
  60. >>> i = next(matches, 0)
  61. >>> a + b[i:]
  62. [1, 3, 9, 8, 3, 4, 5, 7, 8]
  63.  
  64. master = [1,3,9,8,3,4,5]
  65. addition = [3,4,5,7,8]
  66.  
  67. def merge(master, addition):
  68. first = addition[0]
  69. n = max(len(master) - len(addition), 1) # (1)
  70. while 1:
  71. try:
  72. n = master.index(first, n) # (2)
  73. except ValueError:
  74. return master + addition
  75.  
  76. if master[-n:] == addition[:n]:
  77. return master + addition[n:]
  78. n += 1
  79.  
  80. def merge(master, addition):
  81. overlap_lens = (i + 1 for i, e in enumerate(addition) if e == master[-1])
  82. for overlap_len in overlap_lens:
  83. for i in range(overlap_len):
  84. if master[-overlap_len + i] != addition[i]:
  85. break
  86. else:
  87. return master + addition[overlap_len:]
  88. return master + addition
  89.  
  90. def longest_common_substring(s1, s2):
  91. m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
  92. longest, x_longest = 0, 0
  93. for x in xrange(1, 1 + len(s1)):
  94. for y in xrange(1, 1 + len(s2)):
  95. if s1[x - 1] == s2[y - 1]:
  96. m[x][y] = m[x - 1][y - 1] + 1
  97. if m[x][y] > longest:
  98. longest = m[x][y]
  99. x_longest = x
  100. else:
  101. m[x][y] = 0
  102. return x_longest - longest, x_longest
  103.  
  104. master = [1,3,9,8,3,4,5]
  105. addition = [3,4,5,7,8]
  106. s, e = longest_common_substring(master, addition)
  107. if e - s > 1:
  108. print master[:s] + addition
  109.  
  110. master = [9, 1, 1, 8, 7]
  111. addition = [8, 6, 7]
  112. s, e = longest_common_substring(master, addition)
  113. if e - s > 1:
  114. print master[:s] + addition
  115. else:
  116. print master + addition
  117.  
  118. [1, 3, 9, 8, 3, 4, 5, 7, 8]
  119. [9, 1, 1, 8, 7, 8, 6, 7]
  120.  
  121. def merge(master, addition):
  122. for n in xrange(1, len(master)):
  123. if master[-n:] == addition[:n]:
  124. return master + addition[n:]
  125. return master + addition
  126.  
  127. def merge(master, addition):
  128. indices = [len(master) - i for i, x in enumerate(master) if x == addition[0]]
  129. for n in indices:
  130. if master[-n:] == addition[:n]:
  131. return master + addition[n:]
  132. return master + addition
  133.  
  134. 1234123141234
  135. 3579
  136. 3579
  137. 3579
  138. 3579
  139. 3579
  140. 3579
  141. 3579
  142. 3579
  143. 3579
  144. 3579
  145. 3579
  146. 3579
  147. 3579
  148.  
  149. 1234123141234
  150. | | |
  151. | | 3579
  152. | 3579
  153. 3579
  154.  
  155. def join_two_lists(a, b):
  156. index = 0
  157. for i in xrange(len(b), 0, -1):
  158. #if everything from start to ith of b is the
  159. #same from the end of a at ith append the result
  160. if b[:i] == a[-i:]:
  161. index = i
  162. break
  163.  
  164. return a + b[index:]
  165.  
  166. import pandas as pd, numpy as np
  167.  
  168. trainCompIdMaps = pd.DataFrame( { "compoundId": np.random.permutation( range(800) )[0:80], "partition": np.repeat( "train", 80).tolist()} )
  169.  
  170. testCompIdMaps = pd.DataFrame( {"compoundId": np.random.permutation( range(800) )[0:20], "partition": np.repeat( "test", 20).tolist()} )
  171.  
  172. # row-wise concatenation for two pandas
  173. compoundIdMaps = pd.concat([trainCompIdMaps, testCompIdMaps], axis=0)
  174.  
  175. mergedCompIds = np.array(compoundIdMaps["compoundId"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement