python笔试编程题_python_编程面试题
使用遞歸方法對一個數組求最大值和最小值
"""用遞歸算法求解一個數組的最大值和最小值
思路:
1、首先假設這個列表只有1個元素或兩個元素
2、再考慮超過兩個元素的情況,將該列表從中間位置一分為二
3、然后遞歸調用該函數"""
defmyMaxMin(L,start,end):'''遞歸來得到數組的最大值和最小值'''
if end - start <= 1:#基準點的情況
return(max(L[start],L[end]),min(L[start],L[end]))else:
max1, min1= myMaxMin(L,start,(start+end)//2)#求前半部分的
max2, min2 = myMaxMin(L,(start+end)//2+1,end)#求后半部分的
returnmax(max1,max2),min(min1,min2)defmaxMin(L):assert(type(L) == type([]) and len(L) >0)
maxV, minV= myMaxMin(L,0,len(L)-1)print(maxV,minV)returnmaxV, minV
L= [1,3,5,6,7,8,5,7,8,-9]assert(maxMin(L) == (8,-9))
使用遞歸的方法求出10個球的顏色可能性打印出來(顏色只有黑、白兩種)
'''有10個球,每個球有兩種顏色選擇,黑與白,使用遞歸的方法把這10個球的顏色可能性打印出來
1、將這10個球放入到一個列表中,全部用0表示,0表示白,1表示黑
2、找基準點(程序結束的條件)start = end,打印結果'''count=0defperm(L,start,end):if start == end:#基準點,表示已經給每個球都賦值了一個顏色
print(L)globalcount
count+= 1
else:
perm(L,start+1,end)
L[start]= (L[start]+1)%2#實現1與0的互換
perm(L,start+1,end)
L=[0,0,0,0,0,0,0,0,0,0]
res=perm(L,0,len(L))print(count)#1024,2**10
使用兩個棧實現一個隊列
'''使用兩個棧實現一個隊列
隊列:a,b,c -->c,b,a'''
classQueueWithStacks:def __init__(self):
self.s1=[]
self.s2=[]defpush(self,e):
self.s1.append(e)defpop(self):if len(self.s2) ==0:while len(self.s1) >0:
t=self.s1.pop()
self.s2.append(t)assert len(self.s2) > 0,'隊列已經為空'#此時說明隊列沒有元素
returnself.s2.pop()
q=QueueWithStacks()
q.push('a')
q.push('b')
q.push('c')print(q.pop())print(q.pop())print(q.pop())print(q.pop())#AssertionError: 隊列已經為空
實現單例模式
classSingleton:__instance =None
@classmethoddefget_instance(cls):if cls.__instance:return cls.__instance
else:
cls.__instance =Singleton()return cls.__instanceobj1=Singleton.get_instance()print(obj1) #<__main__.Singleton object at 0x7eff2ce22b70>
obj2 =Singleton.get_instance()print(obj2) #<__main__.Singleton object at 0x7eff2ce22b70>
classSingleton(object):def __new__(cls, *args, **kwargs):if not hasattr(cls,'_instance'):
cls._instance= super(Singleton,cls).__new__(cls)returncls._instance
s1=Singleton()
s2=Singleton()print(s1 == s2) # True
閉包
deffoo():return [lambda x: i+x for i in range(4)]print([x(3) for x infoo()])#將上面的程序改寫,以便更好的理解
deffoo():
function_list=[]for i in range(4):
function_list+= [lambda x: i+x]return function_list #返回一個存有四個函數的列表
#x其實就是表示x=lambda x: i+x,所以x(3)就表示lambda 3: i+3,只不過此處用到了閉包的概念:i是foo函數的局部變量,x是foo的內嵌函數#內嵌函數引用外部函數的變量i,當foo函數執行完畢返回一個函數列表時,i的值已經是3了,所以當x函數中的i就是3,所以結果就是[3+3,3+3,3+3,3+3]
print([x(3) for x in foo()])
編譯一個目錄,找出該目錄下的所有文件
importos
file_list=[]deftraversal_directory(dir):for child inos.listdir(dir):
parent_child=os.path.join(dir, child)ifos.path.isdir(parent_child):
traversal_directory(parent_child)else:
file_list.append(parent_child)returnfile_listif __name__ == "__main__":for file in traversal_directory("slideshow"):print(file)
is與==的區別
"""==符號判斷的是字面值
is判斷的是對象的地址
==符號為True則用is判斷也必定為True
is判斷為True時用==符號判斷未必是True"""
classA:
@staticmethoddef __eq__(var):returnTrueif __name__ == "__main__":print(A() == 1) # Trueprint(A() is 1) # Falseprint("A()的id",id(A()))print("1的id",id(1))
python與C語言在運行效率上哪個更快,并編寫代碼驗證
求第45個斐波那契數
importtimedeffun(n):if n <= 2:return 1
return fun(n-1) + fun(n-2)
start=time.time()
res= fun(45)
end=time.time()
tm= end -startprint(tm) # 287.7714354991913,大約5分鐘print(res)
#include #include#include //導入sleep函數
long fib(intn){if(n <= 2){return 1;
}else{return fib(n-1) + fib(n-2);
}
}intmain(){
unsignedlong start = time(0);long res = fib(45);
unsignedlong end = time(0);double time = end -start;
printf("time:%f\n",time); //大約5秒鐘
printf("res:%ld\n", res);return 0;
}
總結
以上是生活随笔為你收集整理的python笔试编程题_python_编程面试题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python处理表格数据教程_用Pyth
- 下一篇: websocket python爬虫_p