python⼩测试答案_⼀些简单的Python测试题(个⼈参考答
案)
1.请编写python代码,需求是要打印1-1亿之内的偶数。
print([i for i in range(2,100000000L,2)])
2.写⼀个函数, ⽤正则表达式清除字符串中[]和其中的内容。
re.sub('\[\w+\]','',s)
3.请使⽤python代码对下⾯的函数进⾏处理,
php教程 菜鸟def hello(name):
print"hello, %s"%name
在函数被调⽤时打印耗时详情
hello,tom
[timecosts:3.81469726562e-06s]
code:
import time
def time_me(fn):
def _wrapper(*args,**kwargs):
start = time.time()
print(""%(str(fn)))
老驴眼新浪博客
print("")
fn(*args,**kwargs)
print("")
print("[time cost:%ss]"%(time.time()-start))
return _wrapper
4. 写⼀个函数, 将驼峰命名法字符串转成下划线命名字符串(需考虑各类编码中常见的命名)
<  GetItem->get_item
getItem->get_item
doIT    ->do_IT
code: None
5.有⼀个列表:[1, 2, 3, 4...n],n=20;请编写代码打印如下规律的输出:
linux文件创建和删除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):
python基础知识测试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
collapses什么意思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 :
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
#树,深度优先创建+遍历
class Tree:
def __init__(self,matrix,dim):
< = 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])) hild = 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)
hild.val is not None:
self.hild)
return root
def display_tree(self,root):
print(root.val)java编程思想第八版
if root.lchild.val is not None:
self.display_tree(root.lchild)
hild.val  is not None:
self.display_hild)
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) hild.val is not None:
self.path_cost(sum + root.val,''.join([path,str(root.val),'->']),hild) if root.lchild.val is None \
hild.val is None:
self.path_list.append(''.join([path,str(root.val)]))
self.path_costs.append(sum + root.val)
def make_tree(self):
< =  self.)
self.display_)
self.path_cost(0,'',)
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()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。