一. 选择题解析
答案:ABBBD

  1. 以下选项中不符合 Python 语言变量命名规则的是 ( )

A. I
B. 3_1 (不符合第二条)
C. _AI
D. TempStr

解析:

  • 系统关键字不能做变量名使用,例如classdef
  • 变量名可以包括字母、数字、下划线,但是数字不能做为开头。例如:name1是合法变量名,而1name就不可以。
  • 除下划线之外,其它符号不能做为变量名使用
  • Python的变量名是区分大小写的

二. 编程题解析

(1)

class ParkingSystem:
    """
    最容易想到的解决方案
    """

    def __init__(self, big: int, medium: int, small: int):
        self.big = big
        self.medium = medium
        self.small = small

    def addCar(self, carType: int) -> bool:
        if carType == 1:
            if self.big - 1 >= 0:
                self.big -= 1
                return True
            return False
        elif carType == 2:
            if self.medium - 1 >= 0:
                self.medium -= 1
                return True
            return False
        else:
            if self.small - 1 >= 0:
                self.small -= 1
                return True
            return False


class ParkingSystem:
    """
    稍微高级点的解决方案
    使用列表处理数据
    """
    def __init__(self, big, medium, small):
        self.park = [0, big, medium, small]

    def addCar(self, carType):
        if self.park[carType] == 0:
            return False
        self.park[carType] -= 1
        return True

# 杨光同学答案,可供参考
class ParkingSystem:
    def __init__(self,big:int,medium: int,small: int):
        self.big = big
        self.medium = medium
        self.small = small
    def addCar(self,carType: int) -> bool:
        if carType == 3 :
            if int(self.small) >0:
                self.small = int(self.small)-1
                return True
        elif carType  == 2 :
            if int(self.medium) > 0 :
                self.medium = int(self.medium)-1
                return True
        elif carType == 1 :
            if int(self.big) > 0 :
                self.big = int(self.big)-1
                return True
        return False

big = input("大车位有:")
medium = input("中等车位有:")
small = input("小车位有:")
Parking = ParkingSystem(big,medium,small)
while (1):
    type = input("请输入车的尺寸(’1‘,’2‘,’3‘,输入‘q'退出):")
    if type == 'q':
        break
    if Parking.addCar(int(type)):
        print("请进入停车场!")
    else :
        print("车位不足!请另寻去处!")

(2)

# 答案来自杨光
class Animal:
    def __init__(self,name:str):
        self.name = name

    def do_bark(self):
        print(f"{self.name}:哈哈哈...")

    def do_action(self):
        print(f"{self.name} 笑了.")

class Cat(Animal):
    def __init__(self,name:str):
        super().__init__(name)

    def do_bark(self):
        print(f"{self.name.title()}:喵喵喵...")

    def do_action(self):
        print(f"{self.name.title()}在睡觉.")

class Dog(Animal):
    def __init__(self,name:str):
        super().__init__(name)

    def do_bark(self):
        print(f"{self.name.title()}:旺旺旺...")

    def do_action(self):
        print(f"{self.name.title()}在吐舌头.")

class Duck(Animal):
    def __init__(self,name:str):
        super().__init__(name)

    def do_bark(self):
        print(f"{self.name.title()}:嘎嘎嘎...")

    def do_action(self):
        print(f"{self.name.title()}在游泳.")
#方案2,来自寇戈同学作答,使用虚函数,要求子类强制重写父类函数
class Animal:
    def __init__(self, name: str):
        self.name = name

    def do_bark(self):
        return

    def do_action(self):
        return


class Wolf(Animal):
    def __init__(self, name: str):
        super().__init__(name)
        self.barksound = "呜呜呜"
        self.action = "向电线杆撒尿"

    def do_bark(self):
        print(f"{self.name}发出了'{self.barksound}'的叫声")

    def do_action(self):
        print(f"{self.name}做出了{self.action}的动作")


class Cat(Animal):
    def __init__(self, name: str):
        super().__init__(name)
        self.barksound = "喵喵喵"
        self.action = "从聊大实验楼A1201跳下"

    def do_bark(self):
        print(f"{self.name}发出了'{self.barksound}'的叫声")

    def do_action(self):
        print(f"{self.name}做出了{self.action}的动作")


class Dog(Animal):
    def __init__(self, name: str):
        super().__init__(name)
        self.barksound = "汪汪汪"
        self.action = "拆掉****出题的电脑"

    def do_bark(self):
        print(f"{self.name}发出了'{self.barksound}'的叫声")

    def do_action(self):
        print(f"{self.name}做出了{self.action}的动作")


class Duck(Animal):
    def __init__(self, name: str):
        super().__init__(name)
        self.barksound = "***不雅警告***"
        self.action = "站街"

    def do_bark(self):
        print(f"{self.name}发出了'{self.barksound}'的叫声")

    def do_action(self):
        print(f"{self.name}做出了{self.action}的动作")


littleDuck = Duck("杨光")
littleDuck.do_bark()
littleDuck.do_action()

(3)

# 答案取自杨光同学的作答
num = list(input("输入(以‘,’隔开)nums = ").lstrip('[').rstrip(']').split(','))
n = input("输入 n= ")
nums = []
for i in range(int(n)):
    nums.append(int(num[i]))
    nums.append(int(num[int(n)+i]))
print(nums)

(4)

 #答案来源
'''
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/maximum-population-year/solution/ren-kou-zui-duo-de-nian-fen-by-leetcode-5m7r4/
'''
    def maximumPopulation( logs: List[List[int]]) -> int:
        delta = [0] * 101   # 变化量
        offset = 1950   # 起始年份与起始下标之差
        for b, d in logs:
            delta[b-offset] += 1
            delta[d-offset] -= 1
        mx = 0   # 人口数量最大值
        res = 0   # 最大值对应的最小下标
        curr = 0   # 每一年的人口数量
        # 前缀和
        for i in range(101):
            curr += delta[i]
            if curr > mx:
                mx = curr
                res = i
        return res + offset   # 转回对应的年份

(5)

# 大家都做出来,不做解析
最后修改:2022 年 04 月 08 日
如果觉得我的文章对你有用,请随意赞赏