Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- master = [1,3,9,8,3,4,5]
- addition = [3,4,5,7,8]
- def merge(master, addition):
- n = 1
- while n < len(master):
- if master[-n:] == addition[:n]:
- return master + addition[n:]
- n += 1
- return master + addition
- def merge(a, b):
- max_offset = len(b) # can't overlap with greater size than len(b)
- for i in reversed(range(max_offset+1)):
- # checks for equivalence of decreasing sized slices
- if a[-i:] == b[:i]:
- break
- return a + b[i:]
- 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]},
- {'a': [9, 1, 1, 8, 7], 'b': [8, 6, 7], 'result': [9, 1, 1, 8, 7, 8, 6, 7]}]
- all(merge(test['a'], test['b']) == test['result'] for test in test_data)
- def merge(a, b):
- if a[-1] not in b:
- return a + b
- ...
- def merge(a, b):
- while True:
- try:
- idx = b.index(a[-1]) + 1 # leftmost occurrence of a[-1] in b
- except ValueError: # a[-1] not in b
- return a + b
- if a[-idx:] == b[:idx]:
- return a + b[:idx]
- a = [1,2,3,4,1,2,3,4]
- b = [3,4,1,2,3,4,5,6]
- # result should be [1,2,3,4,1,2,3,4,5,6], but
- # this algo produces [1,2,3,4,1,2,3,4,1,2,3,4,5,6]
- def merge(a, b):
- results = []
- while True:
- try:
- idx = b.index(a[-1]) + 1 # leftmost occurrence of a[-1] in b
- except ValueError: # a[-1] not in b
- results.append(a + b)
- break
- if a[-idx:] == b[:idx]:
- results.append(a + b[:idx])
- return min(results, key=len)
- >>> a = [1, 3, 9, 8, 3, 4, 5]
- >>> b = [3, 4, 5, 7, 8]
- >>> matches = (i for i in xrange(len(b), 0, -1) if b[:i] == a[-i:])
- >>> i = next(matches, 0)
- >>> a + b[i:]
- [1, 3, 9, 8, 3, 4, 5, 7, 8]
- master = [1,3,9,8,3,4,5]
- addition = [3,4,5,7,8]
- def merge(master, addition):
- first = addition[0]
- n = max(len(master) - len(addition), 1) # (1)
- while 1:
- try:
- n = master.index(first, n) # (2)
- except ValueError:
- return master + addition
- if master[-n:] == addition[:n]:
- return master + addition[n:]
- n += 1
- def merge(master, addition):
- overlap_lens = (i + 1 for i, e in enumerate(addition) if e == master[-1])
- for overlap_len in overlap_lens:
- for i in range(overlap_len):
- if master[-overlap_len + i] != addition[i]:
- break
- else:
- return master + addition[overlap_len:]
- return master + addition
- def longest_common_substring(s1, s2):
- m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
- longest, x_longest = 0, 0
- for x in xrange(1, 1 + len(s1)):
- for y in xrange(1, 1 + len(s2)):
- if s1[x - 1] == s2[y - 1]:
- m[x][y] = m[x - 1][y - 1] + 1
- if m[x][y] > longest:
- longest = m[x][y]
- x_longest = x
- else:
- m[x][y] = 0
- return x_longest - longest, x_longest
- master = [1,3,9,8,3,4,5]
- addition = [3,4,5,7,8]
- s, e = longest_common_substring(master, addition)
- if e - s > 1:
- print master[:s] + addition
- master = [9, 1, 1, 8, 7]
- addition = [8, 6, 7]
- s, e = longest_common_substring(master, addition)
- if e - s > 1:
- print master[:s] + addition
- else:
- print master + addition
- [1, 3, 9, 8, 3, 4, 5, 7, 8]
- [9, 1, 1, 8, 7, 8, 6, 7]
- def merge(master, addition):
- for n in xrange(1, len(master)):
- if master[-n:] == addition[:n]:
- return master + addition[n:]
- return master + addition
- def merge(master, addition):
- indices = [len(master) - i for i, x in enumerate(master) if x == addition[0]]
- for n in indices:
- if master[-n:] == addition[:n]:
- return master + addition[n:]
- return master + addition
- 1234123141234
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 3579
- 1234123141234
- | | |
- | | 3579
- | 3579
- 3579
- def join_two_lists(a, b):
- index = 0
- for i in xrange(len(b), 0, -1):
- #if everything from start to ith of b is the
- #same from the end of a at ith append the result
- if b[:i] == a[-i:]:
- index = i
- break
- return a + b[index:]
- import pandas as pd, numpy as np
- trainCompIdMaps = pd.DataFrame( { "compoundId": np.random.permutation( range(800) )[0:80], "partition": np.repeat( "train", 80).tolist()} )
- testCompIdMaps = pd.DataFrame( {"compoundId": np.random.permutation( range(800) )[0:20], "partition": np.repeat( "test", 20).tolist()} )
- # row-wise concatenation for two pandas
- compoundIdMaps = pd.concat([trainCompIdMaps, testCompIdMaps], axis=0)
- mergedCompIds = np.array(compoundIdMaps["compoundId"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement