摘要:def tostr(data,base): str='0123456789ABCDEF' if data<base: return str[data] else: return tostr(data//base,base)+tostr(data%base,base)print(tostr(75,15
阅读全文
摘要:from pythonds.basic.stack import Stackdef parchecker(SymbolString): s=Stack() balance=True index=0 while index<len(SymbolString) and balance: if Symbo
阅读全文
摘要:from pythonds.basic.stack import Stackdef parchecker(SymbolString): s=Stack() balance=True index=0 for i in range(len(SymbolString)): if SymbolString[
阅读全文
摘要:乱序字符串检查:指一个字符串只是另一个字符串的重新排列。例如,'heart' 和 'earth' 就是乱序字符串。 最优的解题思路是:两个含26个0元素的列表,遍历字符串,统计字符串中每个字符在列表中的个数,然后检验两个列表是否相等,若相等则为乱序字符串
阅读全文
摘要:1.list.append() #在列表后面追加元素 2.list.extend() #在列表后面追加另一个列表的元素 3.list.pop(index) #弹出指定位置的元素 4.list.sort(reverse=False) #列表元素升序 #reverse=True为降序 5.list.co
阅读全文
摘要:class LogicGate(): def __init__(self,n): self.name=n def getname(self): return self.name def getoutput(self): output=self.out() return outputclass Bin
阅读全文
摘要:def gcd(m,n): if n==0: print("n is not equal 0!") else: while m%n!=0: new=m m=n n=new%n return nclass Fraction(): def __init__(self,m,n): self.m=m sel
阅读全文
摘要:def gcd(m,n): if n==0: #考虑特殊情况 print("n is not equal 0!") else: while m%n!=0: new=m m=n n=new%n return nprint(gcd(2,0))
阅读全文
摘要:1.元素较少时算法的运行速度非常快,但随着元素数量的增加,速度会变的非茶慢。 2.涉及所有组合问题通常是NP完全问题 3.不能将问题分成小问题,必须考虑各种情况,这可能是NP问题 4.如果问题涉及序列(如旅行商问题中的城市序列)且难以解决,它可能是NP问题 5.如果问题涉及集合(如广播电台集合)且难
阅读全文
摘要:graph={}graph['start']={} #定义图中的各个邻居节点graph['start']['a']=6graph['start']['b']=-1graph['a']={}graph['a']['end']=1graph['b']={}graph['b']['a']=3graph['
阅读全文
摘要:from collections import dequegraph={}graph['you']=["alice", "bob", "claire"]graph["bob"] = ["anuj", "peggy"]graph["alice"] = ["peggy"]graph["claire"]
阅读全文
摘要:def merge(a,b): c=[] h=j=0 while (h<len(a))and(j<len(b)): if a[h] < b[j]: c.append(a[h]) h+=1 else: c.append(b[j]) j+=1 if h==len(a): ...
阅读全文
摘要:#arr 有序数组#m 需要比较的值#left 左边最小值索引#right 右边最大值索引def bin(arr,m,left,right): # left=0 # right=len(arr)-1 try: midian=(left+right+1)//2 #python取整 #midian=in
阅读全文
摘要:def quicksort(arr): if len(arr)<2: return arr #基线条件 else: base=arr[0] #递归条件 less=[i for i in arr[1:] if i<=base] greater=[i for i in arr[1:] if i>base
阅读全文
摘要:def findsmallest(arr): smallest=arr[0] smallest_index=0 for i in range(1,len(arr)): #smallest_index+=1 if arr[i]<=smallest: smallest=arr[i] smallest_i
阅读全文
摘要:def bin_search(list,item): low=0 high=len(list)-1 while low<=high: #4 mid = round(((low + high) / 2)+0.1,0) #1 #mid=(low + high) / 2 guess=list[int(mi
阅读全文