代码拉取完成,页面将自动刷新
#定义一个英雄类(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}.")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。