吐血整理函数习题第二弹
里面穿插着有点面试题,都是自己打出来的,难免有点错误欢迎指出错误# 1.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
#方法一
# def func(s):
# lis = []
# for i in range(len(s)):
# if i%2 ==1:
# lis.append(li)
# return lis
# li = (1,2,3,4,5)
# print(func(li))
#方法二多此一举我为了回顾下拆分组合还有类型调用
# def func(*args):
# args1 = list(args)
# return args1
# li = (1,2,3,4,5)
# print(func(*li))
# 方法二简便方法 也是通用方法
# def func(s):
# lis = s
# lis1 = list(lis)
# return lis1
# s = (1,2,3,4,5)
# print(func(s))
# 2.写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
# def func(s):
# if len(s)>5:
# return True
# else :
# return False
# s = '12377775'
# print(func(s))
# #这两个一句话就能出来
# def func(s):
# num = 0
# for i in s:
# num += 1
# return True if num > 5 else False
# 3.写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
# def func(s):
# if len(s)>2:
# s = s[:2]
# return s
# s =
# print(func(s))
# def list_back(user):
# result=[]
# if len(user)>2:
# result.append(user)
# result.append(user)
# return result
# a=
# print(list_back(a))
# 4.写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
# def func(i):
# num = 0
# num1 = 0
# num2= 0
# num3 = 0
# for s in i:
# if s.isdigit():
# num += 1
# elif s.isalpha():
# num1 += 1
# elif s.isspace():
# num2 += 1
# else:
# num3 += 1
# return num,num1,num2,num3
#
# s = input('请输入内容')
# a,b,c,d = func(s)
# print(a,b,c,d)
# print('数字为{}个,字母为{}个,空格为{}个,其他为{}个'.format(a,b,c,d))
# 一、习题讲解
#
# 1、写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组。例如:[(‘红心’,2), (‘草花’,2), …(‘黑桃,‘A’)]
# lis = []
# lis1 = []
# dic = {}
# s = ('方块','梅花','红桃','黑桃')
# num1 = ('J','Q','K','A')
# for i in range(2,11):
# lis1.append(i)
# for j in num1:
# lis1.append(j)
# for i in lis1:
# dic = dic.fromkeys(s,i)
# for x indic.items():
# lis.append(x)
# print(lis)
# 方法二
# def func():
# lis = []
# lis1 = []
# s = ('方块','梅花','红桃','黑桃')
# num1 = ('J','Q','K','A')
# for i in range(2,11):
# lis1.append(i)
# for j in num1:
# lis1.append(j)
# for i in lis1:
# for x ins:
# lis.append((x,i))
# return lis
# print(func())
# 2、写函数,传入n个数,返回字典:{‘max’:最大值,’min’:最小值}。例如: min_max(2, 5, 7, 8, 4),返回: {‘max’:8,’min’:2}
# def dic_num(*args):
# dic = {}
# args = list(args)
# args.sort()
# dic['max'] = args[-1]
# dic['min'] = args
# return dic
# s = (1,2,5,6,7,8)
# print(dic_num(*s))
# 方法二
# def dic_num(*args):
# num_min = args
# num_max = args
# for i in args:
# if num_max < i:
# num_max = i
# if num_min > i:
# num_min = i
# return {'max':num_max,'min':num_min}
# s = (1,2,5,6,7,8)
# print(dic_num(*s))
# 3、写函数,专门计算图形的面积。其中嵌套函数,计算圆的面积,正方形的面积和长方形的面积
#
# 调用函数area(‘圆形’, 圆半径)返回圆的面积
# 调用函数area(‘正方形’, 边长)返回正方形的面积
# 调用函数area(‘长方形’, 长,宽)返回长方形的面积
# #方法一:
# import math
# def func(name,*args):
# def area_yuan(args):
# return args * args * math.pi
# def area_fang(args):
# return args/4 * args/4
# def area_chang(x,y):#必须接收两个值
# return x * y
# if name == '长方形':
# return area_chang(*args)
# if name == '正方形':
# return area_fang(*args)
# if name == '圆形':
# return area_yuan(*args)
# print(func('长方形', 3, 4))
# print(func('圆形', 3))
# print(func('正方形', 4))
# 方法二
# import math
# def area(name,*args):
# def areas_rectangle(x,y):
# return x*y
#
# def area_square(x):
# return x**2
#
# def area_round(r):
# return math.pi*r*r
#
# if name=="圆形":
# returnarea_round(*args)
# elif name=="正方形":
# return area_square(*args)
# elif name=="长方形":
# return areas_rectangle(*args)
#
# print(area('长方形', 3, 4))
# print(area('圆形', 3))
# print(area('正方形', 3))
# 复制代码
# 复制代码
# #方法二(eval)
# import math
# def area(name,*args):
# def rectangle(x,y):
# return x*y
#
# def square(x):
# return x**2
#
# def round(r):
# return math.pi*r*r
# if name in locals():
# return eval(name)(*args)
# print(area('rectangle',13,2))
# 4、写函数,传入一个参数n,返回n的阶乘。例如: cal(7) 计算7 * 6 * 5 * 4 * 3 * 2 * 1
# def cal(num):
# y = 1
# for i in range(num,1,-1):#可以是0或1
# y = y*i
# return y
# print(cal(7))
# 5、斐波那契数列:,每个元素为前两个元素之和
#
# (1)当要求长度小于20的时候:
# l =
# while len(l) < 20:
# l.append(l[-1]+l[-2])
# print(l)
#
# l =
# while l[-1] < 20000:
# l.append(l[-2]+l[-1])
# print(l)
#####################################################################
# 二、函数装饰器
#
# 在不修改原函数的内容及调用方式的前提下,对原函数进行功能扩展
#
# 1、计算函数执行时间实例
#
# import time
# def timmer(f):
# def inner(*args,**kwargs):
# start = time.time()
# set = f(*args,**kwargs)
# print(time.time()-start)
# return set
# return inner
# @timmer
# def func(x,y):
# time.sleep(0.1) #沉睡0.1秒 系统方便计数
# print('hollow world')
# return x**y
# print(func(4,6))
# 2、打印当前正在调用函数名的实例
#
# def fun(f):
# def inner(*args,**kwargs):
# set = f(*args,**kwargs)
# with open('函数调用实例','a',encoding='utf-8')as f1:
# f1.write(f.__name__ + '|')
# print(f.__name__)
# return set
# return inner
# @fun
# def func():
# print('wahha')
# func()
# @fun
# def funcf():
# print('efwef')
# funcf()
#编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
# flag=False
# def login(func):
# def inner(*args,**kwargs):
# global flag
# while not flag:
# name=input('请输入用户名:')
# pwd=input('请输入密码:')
# with open('file','r',encoding='utf-8') as f:
# for line in f:
# lst=line.strip().split('|')
# if name==lst and pwd==lst:
# print('登陆成功’)
# flag=True
# whlie flag:
# ret=func(*args,**kwargs)
# return ret
# return inner
# @login
# def name1():
# print('你好吗?')
# @login
# def name2():
# print('我还好’)
# name1()
# name2()
不错谢谢楼主分享 楼主分析的很细 kk1212 发表于 2017-12-31 17:59
楼主分析的很细
谢谢{:1_919:},元旦身为狗狗的我没事做就索性我爱学习 元旦身为狗狗的我没事做就索性我爱学习 感谢分享 多谢 老哥 走心了{:1_893:} 感谢楼主分享……。
收藏了……多做题才能巩固所学
页:
[1]