Skip to content

Commit 8ee02c5

Browse files
committed
新增解題練習,python練習oop
1 parent 2cc860d commit 8ee02c5

12 files changed

Lines changed: 107 additions & 42 deletions

python/index.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from operator import le
1212
from typing import List
1313
import math
14-
14+
from collections import Counter
1515

1616
# from numpy import diff
1717
# module
@@ -23,16 +23,22 @@
2323

2424
# oop
2525
# from file name(module) import the class.
26-
from oop.Animals import Animals
27-
from oop.Tortoises import Tortoises
26+
from oop.Animal import Animal
27+
from oop.Tortoise import Tortoise
2828
from oop.Lion import Lion
2929

30-
greek = Tortoises("福氣","7個月大","veggie","地中海型陸龜")
30+
greek = Tortoise("福氣",7,"veggie","地中海型陸龜")
3131
greek.eat()
32+
greek.hibernation()
3233
greek.environment()
34+
greek.attack()
3335

3436
lion = Lion("獅子","未知","肉食")
35-
lion.environment()
37+
lion.eat()
38+
# lion.attack()
39+
40+
# print(lion.food)
41+
# lion.environment()
3642

3743
# from oop.Fruit import Fruit
3844
# from oop.Melon import Melon
@@ -296,3 +302,4 @@ def isPrime(element:int):
296302
# print(closestPrimes(left,right))
297303

298304

305+

python/leetcode/list/3005.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
from collections import Counter
3+
4+
def maxFrequencyElements(nums: List[int]) -> int:
5+
'''
6+
3005. Count Elements With Maximum Frequency
7+
8+
計算每個元素出現的次數,找出最大出現次數的為何,若value符合最大出現次數,則加總該次數
9+
'''
10+
ans = 0
11+
occurences = Counter(nums)
12+
# 取得Counter後的最大value
13+
maxValue = max(occurences.values())
14+
for item, count in occurences.items():
15+
if count == maxValue:
16+
ans += count
17+
18+
return ans
19+
nums = [1,2,2,3,1,4]
20+
# 4
21+
print(maxFrequencyElements(nums))

python/oop/Animal.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from abc import ABC, abstractmethod
2+
class Animal(ABC):
3+
4+
def __init__(self,name:str,age:int,food:list[str]):
5+
self.name = name
6+
self.age = age
7+
self.food = food
8+
self.__health_level = 100 # 私有屬性
9+
10+
# methods
11+
def eat(self):
12+
'''
13+
動物進食
14+
'''
15+
print(f"{self.name} is {''.join(self.food)}")
16+
17+
def hibernation(self):
18+
'''
19+
冬眠
20+
'''
21+
print(f"{self.name}會冬眠嗎")
22+
23+
def get_sick(self):
24+
'''
25+
動物生病,健康值下降
26+
'''
27+
self.__health_level -= 20
28+
print(f"{self.name}的健康值下降了。現在是: {self.__health_level}.")
29+
30+
31+
# abstract方法:所有動物必須具備的
32+
@abstractmethod
33+
def attack(self):
34+
"""抽象方法:定義攻擊行為"""
35+
pass
36+
37+
@abstractmethod
38+
def affend(self):
39+
"""抽象方法:定義防禦行為"""
40+
pass

python/oop/Animals.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

python/oop/Behavior.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import ABC, abstractmethod
2+
class Behavior(ABC):
3+
4+
# abstract
5+
@abstractmethod
6+
def attack():
7+
pass
8+
9+
@abstractmethod
10+
def affend():
11+
pass

python/oop/Lion.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
from oop.Animals import Animals
1+
from oop.Animal import Animal
22

3-
class Lion(Animals):
3+
class Lion(Animal):
44
def __init__(self, name, age, food):
55
super().__init__(name, age, food)
6-
7-
def eat(self):
8-
return super().eat()
96

107
def environment(self):
11-
print("溫暖")
8+
print("溫暖")
9+
10+
# 覆寫父類別的方法
11+
def eat(self):
12+
print(f"{self.name} 正在快速撕咬 {''.join(self.food)}。")
13+
14+
# abstract
15+
def affend(self):
16+
print("同攻擊")
17+
18+
def attack(self):
19+
print("咬抓")
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
from oop.Animals import Animals
1+
from oop.Animal import Animal
22

3-
class Tortoises(Animals):
3+
class Tortoise(Animal):
44

55
def __init__(self, name, age, food,speciceType:str):
66
super().__init__(name, age, food)
77
self.speciceType = speciceType
88

9-
def eat(self):
10-
return super().eat()
119

12-
13-
# 覆寫父類別的方法
1410
def environment(self):
1511
'''
1612
環境
@@ -28,8 +24,10 @@ def environment(self):
2824
except NameError:
2925
print("eror")
3026

31-
def hibernation(self):
32-
'''
33-
冬眠
34-
'''
35-
print("if it's really cold")
27+
28+
# 抽象
29+
def attack(self):
30+
print("衝撞")
31+
32+
def affend(self):
33+
print("縮")
1.98 KB
Binary file not shown.
342 Bytes
Binary file not shown.
423 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)