Skip to content

Commit a78c59e

Browse files
committed
solutions
1 parent 28de4e3 commit a78c59e

File tree

30 files changed

+987
-2713
lines changed

30 files changed

+987
-2713
lines changed

0_intro/01_hello/solution.py

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

0_intro/01_hello/starter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def add(a, b):
5151
Returns:
5252
Sum of a and b
5353
"""
54-
pass
54+
return a + b
5555

5656

5757
def multiply(a, b):
@@ -65,4 +65,4 @@ def multiply(a, b):
6565
Returns:
6666
Product of a and b
6767
"""
68-
pass
68+
return a * b

1_principles/01_grasp/04_low_coupling/solution.py

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

1_principles/01_grasp/04_low_coupling/starter.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ def save(self, player: str, score: int) -> str:
2828
# Metoda save_score(player, score) zwraca: "Saved score {score} for {player}"
2929

3030
class ScoreService:
31-
pass
31+
def __init__(self):
32+
self.database = Database()
33+
34+
def save_score(self, player: str, score: int) -> str:
35+
"""Pośrednik - izoluje Game od Database"""
36+
self.database.connect()
37+
return self.database.save(player, score)
3238

3339

3440
# TODO: Zaimplementuj Game
@@ -40,7 +46,14 @@ class ScoreService:
4046
# Low Coupling: Game nie zna Database, tylko ScoreService (pośrednik)
4147

4248
class Game:
43-
pass
49+
def __init__(self, score_service: ScoreService):
50+
"""Dependency Injection - redukuje sprzężenie"""
51+
self.score_service = score_service
52+
53+
def finish_game(self, player: str, score: int) -> str:
54+
"""Game używa tylko ScoreService, nie zna Database"""
55+
result = self.score_service.save_score(player, score)
56+
return f"Game finished. {result}"
4457

4558

4659
# GRASP Low Coupling:
@@ -55,8 +68,8 @@ class Game:
5568
# Korzyść: Zmiana Database nie wpływa na Game
5669

5770

58-
# Przykład użycia - odkomentuj gdy zaimplementujesz:
59-
# if __name__ == "__main__":
60-
# service = ScoreService()
61-
# game = Game(service)
62-
# print(game.finish_game("Alice", 150))
71+
# Przykład użycia
72+
if __name__ == "__main__":
73+
service = ScoreService()
74+
game = Game(service)
75+
print(game.finish_game("Alice", 150))

1_principles/02_solid/02_ocp/solution.py

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

1_principles/02_solid/02_ocp/starter.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,75 @@
2121
100.5
2222
"""
2323

24+
from abc import ABC, abstractmethod
25+
2426

2527
# TODO: Zaimplementuj interfejs Shape
2628
# Klasa abstrakcyjna z metodą calculate_area()
2729

28-
class Shape:
29-
pass
30+
class Shape(ABC):
31+
@abstractmethod
32+
def calculate_area(self):
33+
"""Metoda abstrakcyjna - każdy kształt musi ją zaimplementować"""
34+
pass
3035

3136

3237
# TODO: Zaimplementuj Circle
3338
# Przyjmuje radius w konstruktorze
3439
# Dziedziczy po Shape
3540
# Pole = π * r² (użyj 3.14 dla π)
3641

37-
class Circle:
38-
pass
42+
class Circle(Shape):
43+
def __init__(self, radius: float):
44+
self.radius = radius
45+
46+
def calculate_area(self) -> float:
47+
return 3.14 * self.radius ** 2
3948

4049

4150
# TODO: Zaimplementuj Square
4251
# Przyjmuje side w konstruktorze
4352
# Dziedziczy po Shape
4453
# Pole = side²
4554

46-
class Square:
47-
pass
55+
class Square(Shape):
56+
def __init__(self, side: float):
57+
self.side = side
58+
59+
def calculate_area(self) -> float:
60+
return self.side ** 2
4861

4962

5063
# TODO: Zaimplementuj Triangle
5164
# Przyjmuje base i height w konstruktorze
5265
# Dziedziczy po Shape
5366
# Pole = (base * height) / 2
5467

55-
class Triangle:
56-
pass
68+
class Triangle(Shape):
69+
def __init__(self, base: float, height: float):
70+
self.base = base
71+
self.height = height
72+
73+
def calculate_area(self) -> float:
74+
return (self.base * self.height) / 2
5775

5876

5977
# TODO: Zaimplementuj AreaCalculator
6078
# Metoda total_area(shapes) przyjmuje listę kształtów
6179
# i zwraca sumę ich pól używając polimorfizmu
6280

6381
class AreaCalculator:
64-
pass
82+
def total_area(self, shapes: list) -> float:
83+
"""Polimorfizm - każdy kształt wie jak obliczyć swoje pole"""
84+
return sum(shape.calculate_area() for shape in shapes)
6585

6686

6787
# OCP: Open for extension, Closed for modification
6888
# Nowy kształt = nowa klasa Shape, zero zmian w AreaCalculator
6989

7090

71-
# Przykład użycia - odkomentuj gdy zaimplementujesz:
72-
# if __name__ == "__main__":
73-
# shapes = [Circle(5), Square(4), Triangle(3, 4)]
74-
# calculator = AreaCalculator()
75-
# print(f"Total area: {calculator.total_area(shapes)}")
91+
# Przykład użycia
92+
if __name__ == "__main__":
93+
shapes = [Circle(5), Square(4), Triangle(3, 4)]
94+
calculator = AreaCalculator()
95+
print(f"Total area: {calculator.total_area(shapes)}")

0 commit comments

Comments
 (0)