def lr(s_1, s_2): global memory memory += gso(s_1) + gso(s_2) if not s_1 or not s_2: res = max(len(s_1), len(s_2)) memory += gso(res) return res res = min( lr(s_1, s_2[:-1]) + 1, lr(s_1[:-1], s_2) + 1, lr(s_1[:-1], s_2[:-1]) + int(s_1[-1] != s_2[-1])) memory += gso(res) return res
def ldr(s_1, s_2): global memory memory += gso(s_1) + gso(s_2) if not s_1 or not s_2: res = max(len(s_1), len(s_2)) memory += gso(res) return res res = min( ldr(s_1, s_2[:-1]) + 1, ldr(s_1[:-1], s_2) + 1, ldr(s_1[:-1], s_2[:-1]) + int(s_1[-1] != s_2[-1])) memory += 2 * gso(res) if len(s_1) >= 2 and len( s_2) >= 2 and s_1[-1] == s_2[-2] and s_1[-2] == s_2[-1]: res = min(res, ldr(s_1[:-2], s_2[:-2]) + 1) return res
def merge(fl): ''' At a time, Number of values in number_list would be available Cache/number of file. Try to make this count for each file. ''' while True: ''' WRONG LOGIC Modification:- Take bunch of elements from each file. Insert them in a common heap. Also after fetching the elements, rewrite the file with the remaning element of the chuncked file. ''' indiv_cache = int(cache/len(fl)) numbers_list = [] del_file = [] for file in fl: # open each file with open(file, 'w+') as cf: #chunked file nums = cf.read().split('\n') count = 0 temp = [] # for each file fill temp till it reaches individual cache limit while gso(temp)< indiv_cache: temp.extend(nums[count]) numbers_list.extend(nums[count]) if gso(temp) <= indiv_cache: del_file.append(file);continue # write remaining elements back to file cf.write(nums[count:]) hq.heapify(numbers_list) for i in range(len(numbers_list)): with open('output_file', 'ab+') as of: # output file, unless mode is 'r' whole file is not loaded of.write(hq.heappop(numbers_list)) for df in del_file: files_list.remove(df) if files_list == []: break
def lm(s_1, s_2, return_matrix=False): global memory memory += gso(s_1) + gso(s_2) if not s_1 or not s_2: res = max(len(s_1), len(s_2)) memory += gso(res) return res ls_1 = len(s_1) + 1 ls_2 = len(s_2) + 1 mt = [[i + j for j in range(ls_2)] for i in range(ls_1)] memory += gso(ls_1) + gso(ls_2) + gso(mt) for i in range(1, ls_1): for j in range(1, ls_2): mt[i][j] = min(mt[i - 1][j] + 1, mt[i][j - 1] + 1, mt[i - 1][j - 1] + int(s_1[i - 1] != s_2[j - 1])) if return_matrix: return mt return mt[-1][-1]
def __sizeof__(self): # quick and dirty size function return sum([gso(x) for x in [self.timelapses, self.currentcomposite]])