今天笔试有一份挺好的笔试试卷,现将其整理成档,有问题处欢迎大家评论~~![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
1、用python实现对目标文件夹的遍历和打印。
import os def filelist(direct,file): fileName=os.listdir(direct) for dirName in fileName: fileName=os.path.join(direct,dirName) if os.path.isdir(fileName): filelist(os.path.join(direct,fileName),fileName) else: print(dirName) return file filelist('E:\新建文件夹',[])
2、用python实现一个自定义的双向链表,要求支持insert,delete,update,find等方法。
class Node(object): def __init__(self,value): self.value=value self.next=None self.prev=None class Linkchain(object): def __init__(self): self.head=Node('0') def append(self,value): #get chain tail len=self.getLen() p=self.getNode(len) apendNode=Node(value) p.next=apendNode apendNode.prev=p apendNode.next=None def getLen(self): p=self.head i=0 while p.next: p=p.next i=i+1 return i def getNode(self,n): p=self.head for i in range(0,n): p=p.next return p def insert(self,n,value): #get the place of parameter n p=self.getNode(n) temp=p.prev '''consider the two pole of the chain''' insertNode=Node(value) temp.next=insertNode insertNode.prev=temp insertNode.next=p p.prev=insertNode print("insert the value %s in the place of %s"%(value,n)) def delete(self,n): p=self.getNode(n) (p.prev).next=p.next p.next.prev=p.prev print("delete at the place of %s"%n) def update(self): '''have no idea about this function ''' pass def find(self,value): #according to the value,find the place p=self.head for i in range(0,self.getLen()): p=p.next if p.value ==value: print('find the value %s is on the place of %s' %(value,i+1)) def printChain(self): p=self.head i=0 while p.next: p=p.next i=i+1 print(" "+str(p.value)) p=Linkchain() p.append(20) p.append(14) p.append(72) p.append(88) p.append(95) p.printChain() p.insert(4,103) p.printChain() p.delete(5) p.printChain() p.find(103) 此题中的update函数未实现,求助,有会的大神吗?
3、写出下面代码的输出
def f(x,I=[]): print("the parameter I is %s"%I) for i in range(x): I.append(i*i) print(I) f(2) f(3,[3,2,1]) f(3)
这题我答错了,本以为是挺简单的,现在还是想不明白,求大神解救~~
the parameter I is [] [0, 1] the parameter I is [3, 2, 1] [3, 2, 1, 0, 1, 4] the parameter I is [0, 1] [0, 1, 0, 1, 4]这是答案,为什么f(3)的列表会是[0,1],默认的空列表为什么会记忆上一次的结果4、写出下列代码的输出
class A(object): def go(self): print("go A go!") def stop(self): print("stop A stop!") def pause(self): raise Exception("not implemented") class B(A): def go(self): super(B,self).go() print("go B go!") class C(A): def go(self): super(C,self).go() print("go C go") def stop(self): super(C,self).stop() print("stop C stop") class D(B,C): def go(self): super(D,self).go() print("go D go!") def stop(self): super(D,self).stop() print("stop D stop") def pause(self): print("wait D wait!") class E(B,C):pass a=A() b=B() c=C() d=D() e=E() print("AAAAAA") a.go() print('BBBBBB') b.go() print("CCCCCC") c.go() print("DDDDDD") d.go() print("EEEEEE") e.go() a.stop() a.stop() c.stop() d.stop() e.stop() a.pause() b.pause() c.pause() d.pause() e.pause()
运行结果为:主要考察super继承,父类只调用一次,继承调用顺序,从继承列表右向左
AAAAAA
go A go!
BBBBBB
go A go!
go B go!
CCCCCC
go A go!
go C go
DDDDDD
go A go!
go C go
go B go!
go D go!
EEEEEE
go A go!
go C go
go B go!
stop A stop!
stop A stop!
stop A stop!
stop C stop
stop A stop!
stop C stop
stop D stop
stop A stop!
stop C stop
Traceback (most recent call last):
File "D:\Program Files\python\Scripts\herritage.py", line 49, in <module>
a.pause()
File "D:\Program Files\python\Scripts\herritage.py", line 7, in pause
raise Exception("not implemented")
Exception: not implemented
5、解释python中的装饰器decorator,并通过装饰器对入参进行类型检查(如果传入参数中有int,则打印“invalid value”,并退出)
import itertools def chtype(type): def _chtype(fun): def _deco(*args): print(str(type)) if len(type) != len(args): raise Exception('args error') for arg, arg_type in itertools.zip_longest(args, type): print(str(arg)+"sdsd"+str(arg_type)) if not isinstance(arg, arg_type):
print("invalid value") raise TypeError('%s is not the type of %s' % (arg, arg_type)) fun(*args) return _deco return _chtype @chtype((str,int)) def login(name, passwd): print('login ok') login('skycrab','hjj') login('skycrab',22)
6、解释一下yield,并实现一个斐波拉契数生成器。
先来一个return版的
def fabric(n): if (1==n)or(2==n): return 1 else: return fabric(n-1)+fabric(n-2) print(str(fabric(7)))换成yield试试,事实证明yield俺还是没弄清楚
def fib(max): n, a, b = 0, 0, 1 while n < max: print(str(b)) yield b a, b = b, a + b n = n + 1 return b next(fib(2)) next(fib(5)) next(fib(7)) next(fib(9))迭代器的性质还有待深掘~
7、A)使用python实现一个N叉树,每个节点记录一个[1,10]范围内的随机浮点数
B)实现一个算法对这个N叉数进行过滤,要求最后生成树中不存在小于1的叶子节点
8、实现一个线程安全(thread-safe)的单例模式(singleton)
9、A)解释一下对python多线程的理解
B)为什么Python中使用多线程会使性能下降?
C)Python中还有哪些其他并行编程模式?
10、解释一下上下文管理器ContextManager,并实现一个简单的ContextManager用来读取文件。
二、选做题
11、写一段示例代码,解释一下元类metaclass。
12、设计一个id生成服务(给出思路即可):
A)为所有请求返回一个全局唯一,并趋势有序的id
B)能够承受每秒10万次的请求
C)服务器异常不会影响id的生成