python学习代码(1)

23 篇文章 0 订阅
22 篇文章 0 订阅
本文是Python学习的初步介绍,涵盖了基础语法和简单示例,适合初学者入门。
摘要由CSDN通过智能技术生成
from functools import reduce


def add(*arg):  # 将不定多个参数值求和
    sum = 0
    for x in arg:
        sum += x
    return sum


def addNumList(list1, list2):  # 求多个列表值
    return[(x + y) for x in list1 for y in list2]


def addMoreNumList(list):
    return reduce(addNumList, list)


def accumator(initialNumber=0, eachTimeAddMoreNumber=0):
    sum = initialNumber

    def inner_accumator(toAddNumber):
        nonlocal sum
        sum += toAddNumber + eachTimeAddMoreNumber
        return sum
    return inner_accumator


def triFindMaxWay(triAry):
    # 结果存值列表初始化,轮数初始化

    return(sumList)

# testList = [12, 25, 23, 24, 26, 27, 28, 20, 21,
#             23, 22, 24, 20, 19, 25, 18, 23, 26, 20, 22]


def findAverage(list):
    list.remove(max(list))
    list.remove(min(list))
    return sum(list) / len(list)


def findGroupAverage(list, passLine):
    sumPassGrade = sumFailGrade = 0
    sumPassNum = sumFailNum = 0
    for x in list:
        if x >= passLine:
            sumPassGrade += x
            sumPassNum += 1
        else:
            sumFailGrade += x
            sumFailNum += 1
    if sumPassNum == 0:
        print('All Fail')
        return sumFailGrade / sumFailNum
    elif sumFailNum == 0:
        print('All Pass')
        return sumPassGrade / sumPassNum
    else:
        return sumPassGrade / sumPassNum, sumFailGrade / sumFailNum


# import sys

# testdict = {"william": "123456"}
# inputlist = []
# while True:
#     line = sys.stdin.readline().strip()
#     if line == '':
#         break
#     lines = line.split()
#     for item in lines:
#         inputlist.append(item)

# if inputlist[0] in testdict.keys():
#     if testdict[inputlist[0]] == inputlist[1]:
#         print('登陆成功')
#     else:
#         print('用户和密码存在错误')
# else:
#     print('用户和密码存在错误')


# print(findAverage(testList))
# print(findGroupAverage(testList, 60))


import copy


lines = [
    [1],
    [3, 2],
    [4, 5, 6],
    [8, 9, 8, 7],
    [7, 6, 5, 4, 3],
    [0, 10, 0, 0, 0, 5],
    [1, 2, 3, 4, 5, 6, 7],
    [8, 7, 6, 5, 4, 3, 2, 1],
    [1, 1, 2, 3, 4, 5, 6, 7, 10]
]


# lines = [
#     [1],
#     [3, 0],
#     [0, 5, 0],
#     [0, 9, 0, 0],
# ]


def maxSum(list, n, i, j):
    if i == n:
        return list[i][j]
    x = maxSum(list, n, i + 1, j)
    y = maxSum(list, n, i + 1, j + 1)
    return max(x, y) + list[i][j]


# 动态规划 DP
def maxSumDP(list, listDP, n, i, j):
    if listDP[i][j] != list[i][j]:
        return listDP[i][j]
    if i == n:
        listDP[i][j] = list[i][j]
    else:
        x = maxSumDP(list, listDP, n, i + 1, j)
        y = maxSumDP(list, listDP, n, i + 1, j + 1)
        listDP[i][j] = max(x, y) + list[i][j]
    return listDP[i][j]


def pathFind(list):
    return maxSum(list, len(list) - 1, 0, 0)


def pathFindDP(list):
    listDP = copy.deepcopy(list)
    return maxSumDP(list, listDP, len(list) - 1, 0, 0)


print('乃木坂', pathFind(lines))

print('乃木坂', pathFindDP(lines))

if __name__ == '__main__':
    lines = [[7], [3, 8], [8, 1, 0], [2, 7, 4, 4]]
    # [4, 5, 2, 6, 5], [8, 9, 7, 4, 2, 6]]
    print(triFindMaxWay(lines))

head1 = newListNode([0])
head2 = newListNode([0])
# print(head1.val)
# printListNode(head1)
printListNode(addTwoNumbers(head1, head2))


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
    c = 0
    header = None
    back = None
    sum = 0
    while True:
        if l1 is not None and l2 is not None:  # 均非空
            sum = l1.val + l2.val
            l1 = l1.next
            l2 = l2.next
        elif l1 is not None:  # l1非空
            sum = l1.val
            l1 = l1.next
        elif l2 is not None:  # l2非空
            sum = l2.val
            l2 = l2.next
        else:  # 均空
            sum = 0
            if c == 0:
                return header
            else:
                node = ListNode(c)
                back.next = node
                return header
        sum += c
        if sum >= 10:
            c = 1
            sum -= 10
        else:
            c = 0
        node = ListNode(sum)
        if back is None:
            header = node
            back = node
        else:
            back.next = node
            back = node


def newListNode(list):
    header = ListNode(list[0])
    back = header
    for i in range(1, len(list)):
        node = ListNode(list[i])
        back.next = node
        back = node
        node = node.next
    return header


def printListNode(node):
    print(node.val)
    if node.next is not None:
        printListNode(node.next)


def lengthOfLongestSubstring(s: str) -> int:
    list = []
    max = 0
    for x in s:
        if x in list:
            if len(list) > max:
                max = len(list)
            list = list[list.index(x) + 1:]
            print(list)
        list.append(x)
    if len(list) > max:
        max = len(list)
    return max


print(lengthOfLongestSubstring("2019-10-17"))


def findMedianSortedArrays(nums1, nums2):
    length1 = length2 = 0
    length1 = len(nums1)
    length2 = len(nums2)
    needIndex = (length1 + length2) // 2
    flag = (length1 + length2) % 2
    if flag == 0:
        needIndex -= 1
    index1 = index2 = index = 0
    mid = []
    while index < needIndex:
        if index1 < length1 and index2 < length2:
            if(nums1[index1] <= nums2[index2]):
                index1 += 1
            else:
                index2 += 1
            index += 1
        elif index1 < length1:
            index1 += 1
            index += 1
        else:
            index2 += 1
            index += 1
    if flag == 0:
        for x in range(2):
            if index1 < length1 and index2 < length2:
                if(nums1[index1] <= nums2[index2]):
                    mid.append(nums1[index1])
                    index1 += 1
                else:
                    mid.append(nums2[index2])
                    index2 += 1
            elif index1 < length1:
                mid.append(nums1[index1])
                index1 += 1
            else:
                mid.append(nums2[index2])
                index2 += 1
        return (mid[0] + mid[1]) / 2
    else:
        if index1 < length1 and index2 < length2:
            if(nums1[index1] <= nums2[index2]):
                mid = nums1[index1]
            else:
                mid = nums2[index2]
        elif index1 < length1:
            mid = nums1[index1]
        else:
            mid = nums2[index2]
        return mid


nums1 = []
nums2 = [1]
print(findMedianSortedArrays(nums1, nums2))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值