day8 作业

1.函数将指定的列表逆序

def list_reverse(list):
    return list[::-1]
print(list_reverse([1,2,3,4,5,6]))

2.函数提出字符串所有奇数位字符

def get_char(str1):
    list2=[]
    for x  in range(0,len(str1)):
        if x%2!=0:
            list2.append(str1[x])
    print(list2)
get_char('asd2122asd')

3.匿名函数判断是否闰年

result = lambda x:x%4==0 and (x%100==0 and x%400!=0)
print(result(2008))

4.递归打印等腰三角形

def my_print3(n):
    if n==1:
        print(' '*(5-1),'*')
        return None

    my_print3(n-1)
    print(' '*(5-n),'*'*(2*n-1))
my_print3(3)
my_print3(4)

5.写函数,判断列表长度,若长度大于2,存列表前两个内容并返回

def check_len(list):
    list1 = []
    if len(list)>2:
        list1.append(list[0])
        list1.append(list[1])
    return list1
print(check_len([1,2,3,4,5]))

6.写函数求斐波那契数列第10个数

def my_fb(n):
    if n==1:
        return 0
    if n==2:
        return 1
    return my_fb(n-1)+my_fb(n-2)
print(my_fb(10))

7.写函数求平均值,最高分

def my_list_max(list):
    max = list[0]
    for item in  list:
        if item>max:
            max = item
    return max
def my_list_avg(list):
    sum1 = 0
    for item in list:
        sum1+=item
    return sum1/len(list)
def my_operation(char):
    if char =='max':
        return my_list_max
    if char=='avg':
        return my_list_avg
print(my_operation('max')([1,2,3,4,5,6,7]))
print(my_operation('avg')([67,34,86,78,98]))

9.写函数检查传入列表或元组奇数位并返回

def check_list(list):
    list1 = []
    for x in range(0,len(list)):
        if x%2!=0:
            list1.append(list[x])
    return list1

def check_tuple(tuple1):
    list2 =[]
    for x in range(0,len(tuple1)):
        if x%2!=0:
            list2.append(tuple1[x])
    tuple2 = tuple(list2)
    return tuple2

def choose_operation(char):
    if char=='list':
        return check_list
    if char=='tuple':
        return check_tuple
print(choose_operation('list')([7,6,5,4,3,2,1]))
print(choose_operation('tuple')((1,2,3,4,5,6,7)))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.写一个函数,求1+2+3+...+N 结果: 2.求多个数中的最大值 结果: 3.编写一个函数,实现色子功能,...
    HavenYoung阅读 209评论 0 3
  • 1、编写一个函数,求1+2+3+...+N。 2、编写一个函数,求多个数中的最大值。 3、编写一个函数,实现摇色子...
    七夜_174e阅读 428评论 0 5
  • 1.写一个程序 a.用一个变量来保存一个班级的学生信息(姓名,学号,成绩(英语,美术,体育,数学),年龄)b.给这...
    RurouniKenshin阅读 311评论 0 12
  • 一、快捷键 ctr+b 执行ctr+/ 单行注释ctr+c ...
    o_8319阅读 5,868评论 2 16
  • 从某个角度来说,R中的list对应的是Python中的dictionary。但list是能够利用序号index的,...
    天火燎原天阅读 208评论 0 0