Python专业课课堂练习的一些参考——2022/10/18

第一题

定义一个函数用于实现两个列表对应元素相加,注意:列表中元素都为数字

方法一:

List1 = eval(input(''))
List2 = eval(input(''))
def ConnectList(a, b):
    List3 = []
    if len(List1) > len(List2):
        for x in range(len(List1)):  
            List3.append(List1[x] + List2[x])
        print(List3)
    if len(List1) >= len(List2):
        for x in range(len(List1)): 
            List3.append(List2[x] + List1[x])
        print(List3)
ConnectList(List1, List2)

第二题

定义一个函数实现列表的排序方法,类似:list.sort ()

List = eval(input(''))
def ListSort(c):
    Result = []
    if len(c) < 2:
        return c
    else:
        for i in range(len(c)):
            Result.append(min(c))
            c.remove(min(c))   
        return c

进阶:增加升降序功能

List = eval(input(''))
def ListSort(c, reverse = False):
    Result = []
    if len(c) < 2:
        return c
    else:
        for i in range(len(c)):
            Result.append(min(c))
            c.remove(min(c))   
        if reverse == False:
            return Result
        else:
            return Result[::-1]

第三题

利用函数递归调用n!

Num = eval(input(''))
def Factorial(n):
    if n == 1 or n == 0:
        return 1
    else:
        return n * Factorial(n-1)
print(Factorial(Num))

第四题

定义一个函数计算组合数C(n,m)

N = eval(input('下面的:'))
M = eval(input('上面的:'))
def Factorial(n):
    if n == 1 or n == 0:
        return 1
    else:
        return (n * Factorial(n-1))
print(Factorial(N) / (Factorial(N-M) * Factorial(M)))

第五题

定义一个函数实现对一个整数的因式分解

 

都看到这里了,帮忙给作者点上个小小的👍吧

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值