您的位置:首页 > 编程语言 > Python开发

一些简单的Python测试题(个人参考答案)

2015-12-22 22:17 399 查看
1.请编写python代码,需求是要打印1-1亿之内的偶数。

print([i for i in range(2,100000000L,2)])


2.写一个函数, 用正则表达式清除字符串中[]和其中的内容。

re.sub('\[\w+\]','',s)


3.请使用python代码对下面的函数进行处理,

def hello(name):
print"hello, %s"%name


在函数被调用时打印耗时详情

<function name:hello>
<function call begin>
hello,tom
<function call end>
[timecosts:3.81469726562e-06s]


code:

import time
def time_me(fn):
def _wrapper(*args,**kwargs):
start = time.time()
print("<function name: %s>"%(str(fn)))
print("<function call begin>")
fn(*args,**kwargs)
print("<function call end>")
print("[time cost:%ss]"%(time.time()-start))
return _wrapper


4. 写一个函数, 将驼峰命名法字符串转成下划线命名字符串(需考虑各类编码中常见的命名)

e.g.  GetItem->get_item
getItem->get_item
doIT    ->do_IT


code: None

5.有一个列表:[1, 2, 3, 4...n],n=20;请编写代码打印如下规律的输出:

1[1*,2,3,4,5]
2[1,2*,3,4,5]
3[1,2,3*,4,5]
4[2,3,4*,5,6]
5[3,4,5*,6,7]
6[4,5,6*,7,8]
...

20[16,17,18,19,20*]


code:

def print_n(n):
if n -2 <= 0  :
val = range(1,6)
if n + 2 >= 20:
val = range(16,21)
else:
val = range(n-2,n+3)
print("%d%s"%(n,str([ str(i) + '*' if i == n else i for i in val])))

for i in range(1,21):
print_n(i)


6. 写一个程序模拟银行排队, 只有一个队伍, 一个用户进入时允许插队(进入队伍任意位置), 但要保证每次导致队伍变更, 队伍中受影响的人都收到通知

CustomerAline up at position11
CustomerB:order changed to12
CustomerC:order changed to13
CustomerD:order changed to14


code:

from random import randint
class customer:
def __init__(self,cus_name,location):
self.name = cus_name
self.loc = location
print("%s line up at position%d"%(self.name,self.loc))
def getLoc(self,):
return self.loc
def getNotice(self,new_loc):
if new_loc != self.loc:
print("%s order changed to%d"%(self.name,new_loc))
self.loc = new_loc

customer_queue = []
for i in ['A','B','C','D','E']:
new_cus_location = randint(0,len(customer_queue))
cus_i = customer(i,new_cus_location)
customer_queue.insert(new_cus_location,cus_i)
for loc,cus in enumerate(customer_queue):
if cus != cus_i :
cus.getNotice(loc)


7.用户系统, 存在相互关注的动作, 当进入某个人的个人主页, 需要展示其粉丝数, 关注数, 粉丝列表以及关注列表. 请简要描述解决方案, 包括db建模/数据层/业务层, 以及应对高并发/关注取关等情况的处理逻辑

None

8.给定一些NxN的矩阵,对于任意的路线,定义其【和】为其线路上所有节点的数字的和,计算从左上角到右下角的路线和最小值。每条路线只能从某一点到其周围(上下左右)的点,不可斜行。 例如,

4,6
2,8的路线和最小值为4-2-814

1,2,3
4,5,6
7,8,9的路线和最小值为1-2-3-6-921


程序只需输出最小和值即可(一个数字)

code:

所有路径和路径和都打印出来了,获得最小值只需要加一句话就好了

from random import randint
def display_matrix(matrix):
for i in matrix:
print(i)
#建立矩阵
def make_matrix(dim):
temp_list = []
for i in range(dim**2):
temp_list.append( randint(1,10))
matrix_ = [temp_list[i:i+dim] for i in range(0,dim**2,dim)]
display_matrix(matrix_)
return matrix_
#将n*n矩阵扩展成(n+1) * (n+1)的矩阵,便于转换成树的时候递归
def extend_matrix(matrix,dim):
[m_i.append(None) for m_i in matrix ]
matrix.append([None,]*(dim+2))
return matrix
#节点
class Node:
def __init__(self,val,index,lchild = None,rchild = None):
self.val = val
self.index = index
self.lchild = lchild
self.rchild = rchild
#树,深度优先创建+遍历
class Tree:
def __init__(self,matrix,dim):
self.root = Node(val = matrix[0][0],index = (0,0))
self.matrix = matrix
self.dim = dim
self.path_costs = []
self.path_list = []
def add(self,root):
root.lchild = Node(val = matrix[root.index[0] + 1][root.index[1]],index = (root.index[0] + 1,root.index[1]))
root.rchild = Node(val = matrix[root.index[0]][root.index[1] + 1],index = (root.index[0],root.index[1] + 1))
if root.lchild.val is not None:
self.add(root.lchild)
if root.rchild.val is not None:
self.add(root.rchild)
return root
def display_tree(self,root):
print(root.val)
if root.lchild.val is not None:
self.display_tree(root.lchild)
if root.rchild.val  is not None:
self.display_tree(root.rchild)
def path_cost(self,sum,path,root):
if root.lchild.val is not None:
self.path_cost(sum + root.val,''.join([path,str(root.val),'->']),root.lchild)
if root.rchild.val is not None:
self.path_cost(sum + root.val,''.join([path,str(root.val),'->']),root.rchild)
if root.lchild.val is None \
and root.rchild.val is None:
self.path_list.append(''.join([path,str(root.val)]))
self.path_costs.append(sum + root.val)
def make_tree(self):
self.root =  self.add(self.root)
self.display_tree(self.root)
self.path_cost(0,'',self.root)
print (self.path_costs)
print (self.path_list)
dim = 3
matrix = make_matrix(dim)
extend_matrix(matrix,dim)
display_matrix(matrix)
tree_obj = Tree(matrix,dim)
tree_obj.make_tree()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 测试题 答案