day10-homework

  1. 写一个匿名函数,判断指定的年是否是闰年
is_leap = lambda year: year % 4 == 0 and year % 100 != 0 or year %   400 == 0
  1. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def reverse_order(list1: list):
    return list1[::-1]
  1. 写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)
    例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def index_iwsyt(list1: list, target):
    index_list = []
    result = ','
    for index in range(len(list1)):
        if list1[index] == target:
            index_list.append(str(index))
    if index_list:
        return result.join(index_list)
    return -1
  1. 写一个函数,能够将一个字典中的键值对添加到另外一个字典中(不使用字典自带的update方法)
def update_iwsyt(dict1: dict, dict2: dict):
    for key1 in dict1:
        dict2[key1] = dict1[key1]
    return dict2
  1. 写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;所有的大写字母转换成小写字母(不能使用字符串相关方法)
def conversion(str1: str):
    result = ''
    for char in str1:
        if 'a' <= char <= 'z':
            result += chr(ord(char)-32)
        elif 'A' <= char <= 'z':
            result += chr(ord(char)+32)
        else:
            result += char
    return result
  1. 实现一个属于自己的items方法,可以将指定的字典转换成列表。列表中的元素是小的列表,里面是key和value (不能使用字典的items方法)
    例如:{'a':1, 'b':2} 转换成 [['a', 1], ['b', 2]]
def item_iwsyt(dict1: dict):
    list1 = []
    for key in dict1:
        list1.append([key, dict1[key]])
    return list1
  1. 用递归函数实现,逆序打印一个字符串的功能:
    例如:reverse_str('abc') -> 打印 ‘cba’
def reverse_str(str1: str):
    if len(str1) == 1:
        return str1
    return str1[-1]+reverse_str(str1[:len(str1)-1])
  1. 编写一个递归函数,求一个数的n次方
def power(num: int, n: int):
    if n == 1:
        return num
    return power(num, (n-1))*num
  1. 写一个可以产生学号的生成器, 生成的时候可以自定制学号数字位的宽度和学号的开头
    例如:
    study_id_creater('py',5) -> 依次产生: 'py00001', 'py00002', 'py00003',....
    study_id_creater('test',3) -> 依次产生: 'test001', 'test002', 'test003',...
def study_id_creator(str1: str, n: int):
    for num in range(1, 10**n):
        nums = (n - len(str(num))) * '0' + str(num)
        yield str1+nums


creator1 = study_id_creator('py', 5)
  1. 编写代码模拟达的鼠的小游戏,假设一共有5个洞口,老鼠在里面随机一个洞口;人随机打开一个洞口,如果有老鼠,代表抓到了 如果没有,继续打地鼠;但是地鼠会跳到其他洞口
import random


def fight_rats():
    ground_hole = '12345'
    new_hole = ''
    mouse = random.choice(ground_hole)
    while True:
        human = random.choice('12345')
        if mouse == human:
            yield '抓到了'
        else:
            yield '没抓到'
            for x in ground_hole:
                if x != mouse:
                    new_hole += x
            mouse = random.choice(new_hole)


fight = fight_rats()
print(next(fight))
  1. 编写一个函数,计算一个整数的各位数的平方和
    例如: sum1(12) -> 5 sum1(123) -> 14
def sum1(n: int):
    sum2 = 0
    while True:
        if n // 10:
            sum2 += (n % 10) ** 2
            n //= 10
        else:
            sum2 += n ** 2
            return sum2
  1. 楼梯有n阶台阶,上楼可以一步上1阶,也可以一步上2阶,编程序计算共有多少种不同的走法?需求: 编制一个返回值为整型的函数Fib(n),用于获取n阶台阶的走法(挣扎一下)
def Fib(n: int):
    n1 = 1
    n2 = 2
    count = 0
    if n == 1 or n == 2:
        return n
    while True:
        sum1 = n1 + n2
        n2, n1 = sum1, n2
        count += 1
        if count == n-2:
            return sum1
  1. 写一个函数对指定的数分解因式
    例如: mab(6) —> 打印: 2 3 mab(3) -> 1 3 mab(12) -> 2 2 3
def mab(num: int):
    list1 = []
    str1 = ' '
    x = 2
    if num == 1:
        return num
    else:
        while True:
            if num % x == 0:
                list1.append(str(x))
                num //= x
            else:
                x += 1
                if x > num:
                    break
        if len(list1) == 1:
            list1.insert(0, '1')
            return str1.join(list1)
        return str1.join(list1)
  1. 写一个函数判断指定的数是否是回文数
    123321是回文数 12321是回文数 525是回文数
def is_palindrome(num: int):
    if str(num) == str(num)[::-1]:
        return True
    return False
  1. 写一个函数判断一个数是否是丑数(自己百度丑数的定义)
def is_ugly(num: int):
    list1 = [2, 3, 5]
    x = 2
    while True:
        if num % x == 0:
            if x in list1:
                num //= x
                x = 2
            else:
                return False
        else:
            x += 1
            if x > num:
                return True
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.编写函数,求1 + 2 + 3 +…N的和 2.编写一个函数,求多个数中的最大值 3.编写一个函数,实现摇骰子...
    不语sun阅读 255评论 0 0
  • question1. 写一个匿名函数,判断指定的年是否是闰年 question2. 写一个函数将一个指定的列表中的...
    3ae1c50960af阅读 158评论 0 0
  • 1. 编写函数,求1 + 2 + 3 +…N的和 2.编写一个函数,求多个数中的最大值 3.编写一个函数,实现摇骰...
    ham731阅读 324评论 0 0
  • 1.编写函数,求1+2+3+…N的和. 2.编写一个函数,求多个数中的最大值 3.编写一个函数,实现摇骰子的功能,...
    Z_JoonGi阅读 334评论 0 0
  • 编写函数,求1+2+3+…N的和 编写一个函数,求多个数中的最大值 编写一个函数,实现摇骰子的功能,打印N个骰子的...
    _C__C_阅读 144评论 0 0