|
| 1 | +# -- exercise |
| 2 | +# incorrect |
| 3 | +class LegionCommander: |
| 4 | + def __init__(self, name): |
| 5 | + self.name = name |
| 6 | + self.skills = [] |
| 7 | + |
| 8 | + def add_skill(self, skill_name, damage): |
| 9 | + skill = {"skill_name": skill_name, "damage": damage} |
| 10 | + self.skills.append(skill) |
| 11 | + |
| 12 | + def use_skill(self, skill_name): |
| 13 | + for skill in self.skills: |
| 14 | + if skill["skill_name"] == skill_name: |
| 15 | + print(f"{self.name} uses {skill_name} dealing {skill['damage']} damage") |
| 16 | + return |
| 17 | + print(f"{self.name} does not have the skill {skill_name}") |
| 18 | + |
| 19 | +legion = LegionCommander("Legion Commander") |
| 20 | +legion.add_skill("Overwhelming Odds", 100) |
| 21 | +legion.add_skill("Press the Attack", 50) |
| 22 | +legion.use_skill("Overwhelming Odds") |
| 23 | +legion.use_skill("Duel") |
| 24 | + |
| 25 | +# If we want to add a new skill, we have to modify the class. |
| 26 | +legion.add_skill("Duel", 150) |
| 27 | +legion.use_skill("Duel") |
| 28 | + |
| 29 | + |
| 30 | +# correct |
| 31 | +class Skill: |
| 32 | + def use(self): |
| 33 | + raise NotImplementedError("This method should be overridden by subclasses") |
| 34 | + |
| 35 | +class OverwhelmingOdds(Skill): |
| 36 | + def use(self, hero_name): |
| 37 | + print(f"{hero_name} uses Overwhelming Odds dealing 100 damage") |
| 38 | + |
| 39 | +class PressTheAttack(Skill): |
| 40 | + def use(self, hero_name): |
| 41 | + print(f"{hero_name} uses Press the Attack healing 50 health") |
| 42 | + |
| 43 | +class Duel(Skill): |
| 44 | + def use(self, hero_name): |
| 45 | + print(f"{hero_name} uses Duel dealing 150 damage") |
| 46 | + |
| 47 | +class LegionCommander: |
| 48 | + def __init__(self, name): |
| 49 | + self.name = name |
| 50 | + self.skills = {} |
| 51 | + |
| 52 | + def add_skill(self, skill_name, skill): |
| 53 | + self.skills[skill_name] = skill |
| 54 | + |
| 55 | + def use_skill(self, skill_name): |
| 56 | + skill = self.skills.get(skill_name) |
| 57 | + if skill: |
| 58 | + skill.use(self.name) |
| 59 | + else: |
| 60 | + print(f"{self.name} does not have the skill {skill_name}") |
| 61 | + |
| 62 | +print("----------------------------------------------------------------") |
| 63 | +legion = LegionCommander("Legion Commander") |
| 64 | +legion.add_skill("Overwhelming Odds", OverwhelmingOdds()) |
| 65 | +legion.add_skill("Press the Attack", PressTheAttack()) |
| 66 | +legion.use_skill("Overwhelming Odds") |
| 67 | +legion.use_skill("Duel") |
| 68 | + |
| 69 | +# We can add new skills without modifying the LegionCommander class |
| 70 | +legion.add_skill("Duel", Duel()) |
| 71 | +legion.use_skill("Duel") |
| 72 | + |
| 73 | + |
| 74 | +# -- extra challenge |
| 75 | +import math |
| 76 | + |
| 77 | +class Operation: |
| 78 | + def calculate(self, a, b): |
| 79 | + raise NotImplementedError("Subclasses should implement this method") |
| 80 | + |
| 81 | +class Addition(Operation): |
| 82 | + def calculate(self, a, b): |
| 83 | + return a + b |
| 84 | + |
| 85 | +class Subtraction(Operation): |
| 86 | + def calculate(self, a, b): |
| 87 | + return a - b |
| 88 | + |
| 89 | +class Multiplication(Operation): |
| 90 | + def calculate(self, a, b): |
| 91 | + return a * b |
| 92 | + |
| 93 | +class Division(Operation): |
| 94 | + def calculate(self, a, b): |
| 95 | + if b == 0: |
| 96 | + raise ValueError("Division by zero is not allowed") |
| 97 | + return a / b |
| 98 | + |
| 99 | +class Power(Operation): |
| 100 | + def calculate(self, a, b): |
| 101 | + return math.pow(a, b) |
| 102 | + |
| 103 | +class Calculator: |
| 104 | + def __init__(self): |
| 105 | + self.operations = {} |
| 106 | + |
| 107 | + def add_operation(self, operator, operation): |
| 108 | + self.operations[operator] = operation |
| 109 | + |
| 110 | + def calculate(self, operator, a, b): |
| 111 | + if operator in self.operations: |
| 112 | + return self.operations[operator].calculate(a, b) |
| 113 | + else: |
| 114 | + raise ValueError(f"Operation '{operator}' not supported") |
| 115 | + |
| 116 | +print("----------------------------------------------------------------") |
| 117 | +calculator = Calculator() |
| 118 | + |
| 119 | +# add basic operations |
| 120 | +calculator.add_operation('+', Addition()) |
| 121 | +calculator.add_operation('-', Subtraction()) |
| 122 | +calculator.add_operation('*', Multiplication()) |
| 123 | +calculator.add_operation('/', Division()) |
| 124 | + |
| 125 | +a, b = 10, 5 |
| 126 | +print(f"Addition: {a} + {b} = {calculator.calculate('+', a, b)}") |
| 127 | +print(f"Subtraction: {a} - {b} = {calculator.calculate('-', a, b)}") |
| 128 | +print(f"Multiplication: {a} * {b} = {calculator.calculate('*', a, b)}") |
| 129 | +print(f"Division: {a} / {b} = {calculator.calculate('/', a, b)}") |
| 130 | + |
| 131 | +# add new operation - power |
| 132 | +calculator.add_operation('^', Power()) |
| 133 | +print(f"Power: {a} ^ {b} = {calculator.calculate('^', a, b)}") |
0 commit comments