2018-10-11day10Homework

# 1.写一个函数将一个指定的列表中的元素逆序(如[1, 2, 3] -> [3, 2, 1])(注意:不要使 表自带的逆序函数)
def reverse_list(list1: list):
    """
    将指定的列表逆序
    :param list1: 指定列表
    """
    """
    执行过程:
    list1 = [1, 2, 3]
    遍历下标,index取值范围:0 ~ 2
    index = 0  item = list1.pop(index) = list1.pop(0) = 1--> list1=[2,3] 
               list1.insert(0, 1) --> list1 = [1,2,3]
    index = 1  item = list1.pop(1)=2 --> list1 = [1,3]
               list1.insert(0,2) --> list1 = [2,1,3]
    index = 2  item = list1.pop(2)=3 --> list1 = [2, 1]
               list1.insert(0,3) --> list1 = [3, 2, 1]
    """
    for index in range(len(list1)):

        item = list1.pop(index)
        list1.insert(0, item)


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

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

def odd(str1:str):
    new_str = ''
    for x in str1[1::2]:
        new_str += x
    return new_str
print(odd('21d13ads123'))
结果:'11as2'

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

leap_year = lambda years:bool(not years%4 and years%100)
print(leap_year(2016))
结果:True

使用递归打印:```n = 3的时候
@
@ @ @
@ @ @ @ @
n = 4的时候:
@
@ @ @
@ @ @ @ @
@ @ @ @ @ @ @

def print_at(n):
x = 1
if x == n:
print(n'@')
return
print(x
'@')
print_at(x+1)
print(print_at(6))

5.写函数,利用递归获取斐波那契数列中的第 10 个数,并将该值返回给调用者。
def my_alg(n=10):
sum1 = 1
count1 =1
if count1 == 10:
print(sum1)
return
my_alg(+sum1)
print(my_alg())
6.写一个函数,获取列表中的成绩的平均值,和最高分

def average_max(list1:list):
     x = 0
     max = 0
     sum = 0
     ave = 0
     for x in list1:
         sum += x
         ave = sum/len(list1)
         if x > max:
             max = x
     return max,ave
 print(average_max([1,2,11,3,4,5,6,7,8,9]))
结果:(11, 5.6)

7.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新的列表返回给调用者
def odd2(args,kwargs):
new_list = ''
for x in (
args,**kwargs)[1::2]:
new_list += x
return new_list
print(odd2([1,2,'a'=3,'b'=4]))
8.实现属于自己的字典update方法:用一个字典去更新另一个字典的元素(不能使用自带的update方法)
yt_update(字典1, 字典2)

 def hqf_update(dict1,dict2):
     for index1 in dict1:
         key = index1
         value = dict1[index1]
         dict2[key]=value
     return dict2
 print(hqf_update({'name':'hqf','sex':'man','school':'qinghua'},{'name':'666','age':18}))
结果:{'name': 'hqf', 'age': 18, 'sex': 'man', 'school': 'qinghua'}

9.实现属于自己的items方法:将字典转换成列表,字典中的键值对转换成元祖。(不能使用items方法)
yt_items(字典)
例如:{'a': 1, 'b':2, 'c':3} ---> [('a', 1), ('b', 2), ('c', 3)]

def dict_list(dict1:dict):
   list1 = []
   for index1 in dict1:
       tuple1 = (index1,dict1[index1])
       list1.append(tuple1)
   return list1
print(dict_list({'a': 1, 'b':2, 'c':3}))
结果:[('a', 1), ('b', 2), ('c', 3)]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、快捷键 ctr+b 执行ctr+/ 单行注释ctr+c ...
    o_8319阅读 5,877评论 2 16
  • 8月22日-----字符串相关 2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消...
    future_d180阅读 1,001评论 0 1
  • 相关知识:swift支持方法的重载方法的重载:方法名称相同,但是参数不同。包括:参数的类型不同参数的个数不同 Ma...
    CN_HarrySun阅读 945评论 0 1
  • TA加了我 我通过了TA TA从不留言 直到有天 删了我的TA 重新再加我 我又通过了 TA还是不留言 一如往昔神...
    向日葵爱呀爱太阳阅读 244评论 0 0