博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一些简单的Python测试题(个人参考答案)
阅读量:7196 次
发布时间:2019-06-29

本文共 4765 字,大约阅读时间需要 15 分钟。

hot3.png

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

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

hello,tom
[timecosts:3.81469726562e-06s]

  code:

import timedef 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. 写一个函数, 将驼峰命名法字符串转成下划线命名字符串(需考虑各类编码中常见的命名)

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 position11CustomerB:order changed to12CustomerC:order changed to13CustomerD:order changed to14

code:

from random import randintclass 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,62,8的路线和最小值为4-2-814 1,2,34,5,67,8,9的路线和最小值为1-2-3-6-921

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

code:

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

from random import randintdef 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 = 3matrix = make_matrix(dim)extend_matrix(matrix,dim)display_matrix(matrix)tree_obj = Tree(matrix,dim)tree_obj.make_tree()

转载于:https://my.oschina.net/weiwubunengxiao/blog/549308

你可能感兴趣的文章
docker 配置
查看>>
golang 使用 protobuf 的教程
查看>>
shell中eval命令
查看>>
LaTex的注释
查看>>
23种设计模式(9):访问者模式(转)
查看>>
Android 存储路径选择
查看>>
CentOS 7下OpenLDAP编译安装及配置
查看>>
SpringBoot全局异常处理
查看>>
程序的输出和输出到标准错误中的输出
查看>>
Win8 Metro(C#)数字图像处理--2.37Wallis图象锐化
查看>>
美团外卖映射
查看>>
java 遍历Map的四种方式
查看>>
MyBatisBatchItemWriter Cannot change the ExecutorType when there is an existing transaction
查看>>
Javascript继承1:子类的的原型对象----类式继承
查看>>
Windows如何压缩tar.gz格式
查看>>
(原創) N-Tier開發的一些經驗分享 (.NET) (N-Tier)
查看>>
blog推荐 - 左岸读书
查看>>
LoadRunner 技巧之 脚本设计
查看>>
java反编译工具
查看>>
C#控制台的输入和输出-Console类-从控制台输入
查看>>