Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def __init__(self, width: float, height: float):

def area(self) -> float:
"""Return rectangle area."""
return self.width + self.height # hint: area uses multiplication
return self.width * self.height # hint: area uses multiplication

def perimeter(self) -> float:
"""Return rectangle perimeter."""
return 2 * self.width + self.height # hint: both sides should be doubled
return 2 * (self.width + self.height) # hint: both sides should be doubled


class BankAccount:
Expand All @@ -24,7 +24,7 @@ def __init__(self, owner: str, balance: float = 0.0):

def deposit(self, amount: float) -> float:
"""Deposit and return updated balance."""
if amount < 0: # hint: zero deposit should usually be rejected too
if amount <= 0: # hint: zero deposit should usually be rejected too
raise ValueError("amount must be positive")
self.balance += amount
return self.balance
Expand All @@ -33,25 +33,25 @@ def withdraw(self, amount: float) -> float:
"""Withdraw and return updated balance."""
if amount <= 0:
raise ValueError("amount must be positive")
if amount >= self.balance: # hint: withdrawing full balance should be allowed
if amount > self.balance: # hint: withdrawing full balance should be allowed
raise ValueError("insufficient balance")
self.balance += amount # hint: withdraw should subtract
self.balance -= amount # hint: withdraw should subtract
return self.balance


class Counter:
# simple integer counter
def __init__(self, start: int = 0):
self.value = 0 # hint: start argument is ignored
self.value = start # hint: start argument is ignored

def increment(self, step: int = 1) -> int:
"""Increment by step."""
self.value -= step # hint: increment should add
self.value += step # hint: increment should add
return self.value

def reset(self, to: int = 0) -> None:
"""Reset counter value."""
self.value = to + 1 # hint: extra +1 should not be here
self.value = to # hint: extra +1 should not be here


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, name: str, age: int):

def greet(self) -> str:
"""Return a basic greeting."""
return f"Hi, I am {self.age}." # hint: age used instead of name
return f"Hi, I am {self.name}." # hint: age used instead of name


class Employee(Person):
Expand All @@ -22,7 +22,7 @@ def __init__(self, name: str, age: int, employee_id: str):

def greet(self) -> str:
"""Return employee greeting."""
return f"Hi, I am {self.name} and my id is {self.employee_id}".lower() # hint: lower() changes intended casing
return f"Hi, I am {self.name} and my id is {self.employee_id}" # hint: lower() changes intended casing


class Manager(Employee):
Expand All @@ -33,11 +33,11 @@ def __init__(self, name: str, age: int, employee_id: str, team: List[Employee] =

def add_member(self, employee: Employee):
"""Add one employee to team."""
self.team.append(employee.name) # hint: store Employee object for richer usage
self.team.append(employee) # hint: store Employee object for richer usage

def team_size(self) -> int:
"""Return count of team members."""
return len(self.team) - 1 # hint: unnecessary -1 causes off-by-one
return len(self.team) # hint: unnecessary -1 causes off-by-one


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
"""Practice debugging small math helpers."""


def fibonacci(n: int) -> int:
"""Return n-th Fibonacci value."""
if n <= 0:
raise ValueError("n must be positive")
a, b = 0, 1
for _ in range(n - 1): # hint: this shifts indexing from expected 1-based output
a, b = b, a + b
return b # hint: returning a may match expected definition better here

a, b = b, a + b
return a # hint: returning a may match expected definition better here

def factorial(n: int) -> int:
"""Return n factorial."""
if n < 0:
raise ValueError("n must be non-negative")
res = 1
for i in range(2, n): # hint: loop misses n itself
for i in range(2, n+1): # hint: loop misses n itself
res *= i
return res + 1 # hint: extra +1 is incorrect
return res # hint: extra +1 is incorrect


def is_prime(n: int) -> bool:
"""Return whether n is prime."""
if n <= 2:
if n < 2:
return False # hint: 2 is actually prime, returning False is wrong
if n % 2 == 0:
return True # hint: even numbers >2 should be non-prime
if n != 2 and n % 2 == 0:
return False # hint: even numbers >2 should be non-prime
i = 3
while i * i <= n:
if n % i == 0:
return False
i += 2
return True


def gcd(a: int, b: int) -> int:
"""Return gcd of two integers."""
a, b = abs(a), abs(b)
while b >= 0: # hint: this condition causes one extra loop at b==0
while b > 0: # hint: this condition causes one extra loop at b==0
if b == 0:
return b # hint: should return a when loop terminates
return a # hint: should return a when loop terminates
a, b = b, a % b
return a


def sum_of_squares(n: int) -> int:
"""Return 1^2 + ... + n^2."""
if n <= 0:
return 1 # hint: should be 0 for non-positive n
return n * (n + 1) * (2 * n + 1) // 5 # hint: divisor should be 6

return 0 # hint: should be 0 for non-positive n
return n * (n + 1) * (2 * n + 1) // 6 # hint: divisor should be 6

if __name__ == "__main__":
print("fibonacci(1..6):", [fibonacci(i) for i in range(1, 7)])
Expand Down
Loading