加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
作业.py 3.00 KB
一键复制 编辑 原始数据 按行查看 历史
zhaobo 提交于 2023-06-07 10:24 . Initial commit
#定义一个英雄类(Hero),每个实例拥有名字(name)和等级(level)属性。
#定义实例方法gank,接收一个字符串参数name,打印“准备去GANK {name}”。如果对方name是“鲁班”,返回True;如果name是“凯”则返回False。
#定义实例方法grow_up,作用是将英雄当前等级+1。
class Hero:
def __init__(self , name , level):
self.name = name
self.level = level
def gank(self , name):
print(f'准备去GANK {name}')
if name == "鲁班":
return True
elif name == "凯":
return False
def grow_up(self):
self.level += 1
if __name__ == "__main__":
da_ji = Hero("妲己",1)
da_ji.grow_up()
da_ji.grow_up()
da_ji.grow_up()
da_ji.grow_up()
print(da_ji.level)
result = da_ji.gank(name="鲁班")
print(result)
s = "bdackmkdbb"
char_count = {}
for c in s:
if c in char_count:
char_count[c] += 1
else:
char_count[c] = 1
second_single_char = None
single_chars = [c for c in char_count if char_count[c] == 1]
if len(single_chars) > 1:
second_single_char = single_chars[1]
print(second_single_char)
def find_longest_repeated_substring(s):
n = len(s)
best_start, best_end, best_len = 0, 0, 0 # 最长重复子串的起点、终点和长度
for i in range(n):
for j in range(i + 1, n):
k = 0
while j + k < n and s[i + k] == s[j + k]:
k += 1
if k > best_len:
best_start = i
best_end = j
best_len = k
return s[best_start:best_start+best_len]
s = "abcaabcabcdabcde"
print(find_longest_repeated_substring(s)) # 输出 "abc"
class Animal:
def __init__(self, name, color, age, gender):
self.name = name
self.color = color
self.age = age
self.gender = gender
def make_sound(self):
print(f"{self.name}会叫")
def run(self):
print(f"{self.name}会跑")
class Cat(Animal):
def __init__(self, name, color, age, gender):
super().__init__(name, color, age, gender)
self.hair = "短毛"
def catch_mouse(self):
print(f"{self.name}会捉老鼠")
def make_sound(self):
print(f"{self.name}喵喵叫")
class Dog(Animal):
def __init__(self, name, color, age, gender):
super().__init__(name, color, age, gender)
self.hair = "长毛"
def watch_house(self):
print(f"{self.name}会看家")
def make_sound(self):
print(f"{self.name}汪汪叫")
if __name__ == '__main__':
cat = Cat("咪咪" , "黑色" , 2 , "母")
cat.catch_mouse()
print(f"{cat.name}的姓名:{cat.name}, 颜色:{cat.color}, 年龄:{cat.age}, 性别:{cat.gender}, 毛发:{cat.hair}, 捉到了老鼠。")
dog = Dog("旺财" , "黄色" , 3 , "公")
dog.watch_house()
print(f"{dog.name}的姓名:{dog.name}, 颜色:{dog.color}, 年龄:{dog.age}, 性别:{dog.gender}, 毛发:{dog.hair}.")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化