def main(): # Tests first a random input, then a sorted input for testType in ["Random", "Sorted"]: # Displays if random or sorted print(testType.title().center(25 + (12 * (3)) + 1, "=") + "\n") # Uses HOF to determine the testSet if testType == "Random": testSet = getRandomList else: testSet = lambda x: list(range(x)) # Displays all three metrics as described for this lab for test in ["time", "swaps", "comparisons"]: # Invokes compare for all sorts compare([ "Selection", "Bubble", "Quick", "Merge", ], [selectionSort, bubbleSort, quickSort, mergeSort], [10, 100, 1000, 10000], dataSet=testSet, counter={ "comparisons": 0, "swaps": 0 }, compareType=test) print()
def similarity(self): sizeM = len(self.sequenceS) sizeN = len(self.sequenceT) for i in range(1, sizeM + 1): for j in range(1, sizeN + 1): maxValue = max( self.matrix[i - 1, j] + values["GAP"], self.matrix[i - 1, j - 1] + tls.compare(self.sequenceS[i - 1], self.sequenceT[j - 1]), self.matrix[i, j - 1] + values["GAP"], 0) self.matrix[i, j] = maxValue if (maxValue > self.optimalValue): self.optimalValue = maxValue self.positionOptimalValue = [i, j] return self.matrix
def check_new_chapters(url): """check if there is new chapter and save new chapters in json form into noval.json """ html, chapters = request_get(url, a_xpath) # 获取章节 chpt_infos = [] for chapter in chapters: chpt_url = chapter.xpath("./@href") chpt_name = chapter.xpath("./text()") chpt_infos.append([chpt_url, chpt_name]) # 对比并保存新的 小说章节 title = html.xpath("//div[@class='title']") if title: save_filename = title[0] + '.json' else: title = url.split('/')[-1] # 查询新章节 if os.path.exists(save_filename): with open(save_filename, 'r') as info_f: old_chpt_infos = json.loads(info_f.read()) new_chpt_infos = compare(old=old_chpt_infos, new=chpt_infos) # todo: download chapter according to new_chpt_infos if new_chpt_infos: for chpt_url, chpt_name in new_chpt_infos: # 查看url 是否以 '/' 结尾 base_url = url.split('index.html')[0] if base_url[-1] == '/': chapter_link = base_url + chpt_url else: chapter_link = base_url + '/' + chpt_url noval_filename = download_chapter(title, chapter_link) # send email to user send_email(noval_filename) # 删除 os.remove(noval_filename) with open(save_filename, 'w') as f: chpt_json = json.dumps(chpt_infos) f.write(chpt_json)
def align(self): sequenceS = self.sequenceS sequenceT = self.sequenceT alignS = "" alignT = "" lenS = self.positionOptimalValue[0] lenT = self.positionOptimalValue[1] while (lenS >= 0 and lenT >= 0): if (lenT - 1 < 0 and lenS - 1 < 0): break if (lenS - 1 < 0): scoreUp = None scoreDiag = None if (lenT - 1 < 0): scoreLeft = None scoreDiag = None score = self.matrix[lenS, lenT] scoreDiag = self.matrix[lenS - 1, lenT - 1] scoreUp = self.matrix[lenS - 1, lenT] scoreLeft = self.matrix[lenS, lenT - 1] if (scoreDiag != None and score == scoreDiag + tls.compare(sequenceS[lenS - 1], sequenceT[lenT - 1])): alignS = sequenceS[lenS - 1] + alignS alignT = sequenceT[lenT - 1] + alignT lenS = lenS - 1 lenT = lenT - 1 continue if (scoreLeft != None and score == scoreLeft + values["GAP"]): alignS = "-" + alignS alignT = sequenceT[lenT - 1] + alignT lenT = lenT - 1 continue else: alignS = sequenceS[lenS - 1] + alignS alignT = "-" + alignT lenS = lenS - 1 continue return (alignS, alignT)
def _check_state(self, test_outputs, ins): val_l = [test_outputs[k] for k in self.out_names] return compare(val_l, ins, False)
def main(): compare(["Brute", "Fancy"], [lambda x: nQueensBrute(x, True), lambda x: nQueensFancy(x, True)], [4, 5, 6, 7, 8, 9])
def advance_state(self, o): s = self.cur_state next_state = [ # S*0 ( (compare(s, "0000")) or (compare(s, "0001") and compare(o, "000010")) or (compare(s, "0010")) or (compare(s, "0110"))), # S*1 ( (compare(s, "0001") and compare(o, "10-011")) or (compare(s, "0001") and compare(o, "000000")) or (compare(s, "0010") and not o[3]) or (compare(s, "0110"))), # S*2 ( (compare(s, "0001") and compare(o, "000000")) or (compare(s, "0010") and o[3]) or (compare(s, "0011")) or (compare(s, "0110"))), # S*3 (compare(s, "0001") and compare(o, "000--0") and (o[2] ^ o[1])) ] self.cur_state = next_state
def get_output(self): s = self.cur_state return { 'RegDst': True, 'RegWrite': compare(s, "-111") or compare(s, "-100"), 'ALUSrcA': s[3] or s[1], 'MemRead': compare(s, "-011") or compare(s, "0000"), 'MemWrite': compare(s, "-101"), 'MemToReg': False, 'lorD': s[0], 'IRWrite': compare(s, "0000"), 'PCWrite': compare(s, "1--1") or compare(s, "0000"), 'PCWriteCond': compare(s, "1--0"), 'AluOP1': s[2], 'AluOP0': s[3], 'AluSrcB1': s[0] or compare(s, "-01-"), 'AluSrcB0': compare(s, "0-0-"), 'PCSrc1': s[0], 'PCSrc0': compare(s, "1--0") }
def similarity(self): sizeM = len(self.sequenceS) sizeN = len(self.sequenceT) for i in range(0, sizeM+1): self.matrix[i, 0] = i*values["GAP"] for j in range(0, sizeN+1): self.matrix[0, j] = j*values["GAP"] for i in range(1, sizeM+1): for j in range(1, sizeN+1): self.matrix[i, j] = max(self.matrix[i-1, j] + values["GAP"], self.matrix[i-1, j-1] + tls.compare(self.sequenceS[i-1], self.sequenceT[j-1]), self.matrix[i, j-1] + values["GAP"]) return self.matrix