美文网首页
day008 作业 7-25

day008 作业 7-25

作者: Yehao_ | 来源:发表于2018-07-25 20:46 被阅读0次

1.写⼀个函数将⼀个指定的列表中的元素逆序(例如[1, 2, 3] -> [3, 2, 1])(注意:不要使⽤列表⾃带的逆序函数)

def reverse_list(list_name):
    new_list = []
    for element in list_name[::-1]:
        new_list.append(element)
    return new_list

print(reverse_list([1, 2, 3]))

Output:
[3, 2, 1]

2.写⼀个函数,提取出字符串中所有奇数位上的字符

def get_char_in_odd(string):
    new_str = ''
    for char in string[1::2]:  # 获取奇数位上的元素
        new_str += char  # 将奇数位上的所有字符拼接在一起
    return new_str

print(get_char_in_odd('68ftguyhb*#$%^&'))

Output:
8tuh*$^

3.写⼀个匿名函数,判断指定的年是否是闰年

is_leap_year = lambda year: True if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else False
print(is_leap_year(2000))

Output:
True

4.使⽤递归打印:
n = 3的时候
@
@@@
@@@@@
n = 4的时候:
@
@@@
@@@@@
@@@@@@@

5.写函数,检查传⼊列表的⻓度,如果⼤于2,那么仅保留前两个⻓度的内容,并将新内容返回给调⽤者。

def check_length(list):
    new_list = []
    if len(list) > 2:
        new_list.extend(list[0:2])
    else:
        new_list = list
    return new_list

print(check_length([1, 2, 3]))

Output:
[1, 2]

6.写函数,利⽤递归获取斐波那契数列中的第 10 个数,并将该值返回给调⽤者。

def fib(n=10):
    if n <= 1:
        return n
    else:
        return fib(n-1) + fib(n-2)
print(fib())

Output:
55

7.写⼀个函数,获取列表中的成绩的平均值,和最⾼分

def get_avg_and_max(scores):
    sum_score = 0
    avg_score = 0
    max_score = 0
    for score in scores:
        if max_score < score:
            max_score = score
        else:
            pass
        sum_score += score
    avg_score = sum_score / len(scores)
    return avg_score, max_score
avg_score, max_score = get_avg_and_max([75, 97, 87])
print(avg_score, max_score)

Output:
86.33333333333333 97

8.写函数,检查获取传⼊列表或元组对象的所有奇数位索引对应的元素,并将其作为新的列表返回给调用者

def get_odd_ele(seq):
    new_list = []
    new_list = list(seq[1::2])
    return new_list

seq1 = (1, 5, 6)
seq2 = [1, 2, 5, 7]
print(get_odd_ele(seq1))
print(get_odd_ele(seq2))

Output:
[5]
[2, 7]

相关文章

  • day008 作业 7-25

    1.写⼀个函数将⼀个指定的列表中的元素逆序(例如[1, 2, 3] -> [3, 2, 1])(注意:不要使⽤列表...

  • BM运营官-【仪式感】DAY008-by Bella

    DAY008 by Bella作业内容: 仪式感能帮助更快的进入角色,比方说我们的开营仪式和结营仪式就是一种仪式感...

  • 7-25

    69.4kg 晨跑十公里。 昨晚十一点半左右睡着的,三点去了洗手间,并且跑步前我上厕所大号了,对体重影响功不可没。...

  • 7-25

    今天是轮岗学习的第三天,赶上他讲话,头一天上了12小时的班,第二天被车间主任和班长一顿批,第三天大早上六点空腹去吸...

  • 7-25

    周末本来猪队友说要回来,其实很想去外面玩一趟的,但他临时又回不来,可是我的心啊,真的太需要放松了。 周六看到小区群...

  • DAY008

    I like to visit the website of the international school, ...

  • Day008

    Last night was a total lunar eclipse, we can see all kind...

  • Day008

    I should focus on my work every day ,and make a clear dis...

  • Day008

    其实我好久之前就看见男神了,就是我们宿舍开学从网上买的洗衣机送到的那天我就看见他啦,从二栋那个楼梯口下来,我的天,...

  • 商业计划书(BP)应该包含哪些点?看 BP 的人最想从中得到什么

    问题一 国外现在流行做pitch有的时候都不用很详细的BP,一个executive summary, 7-25页的...

网友评论

      本文标题:day008 作业 7-25

      本文链接:https://www.haomeiwen.com/subject/rypmmftx.html