Skip to content
Open
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
16 changes: 13 additions & 3 deletions exercises/binary_search_recursive/solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
SUBMIT = False
SUBMIT = True


def binary_search_recursive(
lst: list[int], target: int, low: int = 0, high: int | None = None
lst: list[int], target: int, low: int = 0, high: int = None
) -> int:
"""Recursive Binary Search"""
pass
if high == None:
high = len(lst)

while low < high:
mid = (low + high) // 2
if lst[mid] < target:
return binary_search_recursive(lst, target, mid+1, high)
elif lst[mid] > target:
return binary_search_recursive(lst, target, low, mid)
return mid
return -1


def test() -> None:
Expand Down
21 changes: 11 additions & 10 deletions exercises/case_inverter/solution.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
SUBMIT = False
SUBMIT = True


def case_inverter(s: str) -> str: # noqa: ARG001
"""
Inverts the case of each character in a string.
"""
inverted_s = ""
new_s = ''
for char in s:
if char.islower():
inverted_s += char.upper()
elif char.isupper():
inverted_s += char.lower()
if char.isalpha():
if char.islower():
new_s += char.upper()
else:
new_s += char.lower()
else:
inverted_s += char
return inverted_s
new_s += char
return new_s


def test() -> None:
"""Simple self-test for Case Inverter."""
cases = [
("Hello World!", "hELLO wORLD!"),
("", ""),
("all lower", "ALL UPPER"),
("ALL UPPER", "all lower"),
("all lower", "ALL LOWER"),
("ALL UPPER", "all upper"),
("1234567890 !@#$%^&*()", "1234567890 !@#$%^&*()"),
("Python 3.12", "pYTHON 3.12"),
]
Expand Down
17 changes: 5 additions & 12 deletions exercises/celsius_to_fahrenheit/solution.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
SUBMIT = False
SUBMIT = True


def celsius_to_fahrenheit(_celsius: float) -> float:
# noqa: ARG001
"""Converts Celsius to Fahrenheit using the formula: F = (C * 9/5) + 32.

Example usage:
>>> celsius_to_fahrenheit(0)
32.0
>>> celsius_to_fahrenheit(100)
212.0
>>> celsius_to_fahrenheit(-40)
-40.0
"""
return 0.0
"""Converts Celsius to Fahrenheit using the formula: F = (C * 9/5) + 32."""

F = (_celsius * 9/5) + 32
return F


def test() -> None:
Expand Down
12 changes: 8 additions & 4 deletions exercises/char_frequency/solution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
SUBMIT = False
SUBMIT = True


def char_frequency(s: str) -> dict[str, int]:
"""Counts the frequency of each character in a string."""
counts = {}
if not s:
return {}

d = {}
for char in s:
counts[char] = counts.get(char, 0) + 1
return counts
d.setdefault(char, 0)
d[char] += 1
return d


def test() -> None:
Expand Down
8 changes: 8 additions & 0 deletions exercises/count_vowels/solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import ast
from pathlib import Path

def count_vowels(s: str) -> int:
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
count = 0
for char in s:
if char in vowels:
count += 1
return count

def test_exercise_solutions_have_asserts() -> None:
"""Verify that all exercise solution stubs have a test() function with asserts."""
root = Path(__file__).parent.parent
Expand Down
18 changes: 7 additions & 11 deletions exercises/factorial_iterative/solution.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
SUBMIT = False
SUBMIT = True


def factorial_iterative(_n: int) -> int:
# noqa: ARG001
"""Calculates n! using a loop.

Example usage:
>>> factorial_iterative(0)
1
>>> factorial_iterative(5)
120
"""
return 0

"""Calculates n! using a loop."""
f = 1
while _n > 0:
f *= _n
_n -= 1
return f

def test() -> None:
"""Simple self-test for Iterative Factorial."""
Expand Down
6 changes: 4 additions & 2 deletions exercises/factorial_tail/solution.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
SUBMIT = False
SUBMIT = True


def factorial_tail(n: int, acc: int = 1) -> int:
"""Tail Recursive Factorial"""
pass
if n == 0:
return acc
return factorial_tail(n-1, acc=acc*n)


def test() -> None:
Expand Down
10 changes: 8 additions & 2 deletions exercises/fast_exponentiation/solution.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
SUBMIT = False
SUBMIT = True


def fast_exponentiation(a: int, n: int) -> int:
"""Fast Exponentiation"""
pass
if n == 0:
return 1
elif n % 2 == 0:
b = fast_exponentiation(a, n//2)
return b * b
elif n % 2 == 1:
return a * fast_exponentiation(a, n-1)


def test() -> None:
Expand Down
24 changes: 9 additions & 15 deletions exercises/fibonacci/solution.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
SUBMIT = False
SUBMIT = True


def fibonacci(n: int) -> int:
"""Calculates the n-th Fibonacci number.

Example usage:
>>> fibonacci(0)
0
>>> fibonacci(1)
1
>>> fibonacci(5)
5
>>> fibonacci(10)
55
"""
return 0
"""Calculates the n-th Fibonacci number."""

if n == 0:
return 0
elif n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)


def test() -> None:
"""Simple self-test for Fibonacci."""
cases = {0: 0, 1: 1, 5: 5, 10: 55}
cases = {0: 0, 1: 1, 5: 5, 10: 55, 30: 832040}
for n, expected in cases.items():
try:
res = fibonacci(n)
Expand Down
11 changes: 9 additions & 2 deletions exercises/fibonacci_tail/solution.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
SUBMIT = False
SUBMIT = True


def fibonacci_tail(n: int, a: int = 0, b: int = 1) -> int:
"""Tail Recursive Fibonacci"""
pass
if n == 0:
return a
elif n == 1:
return b

c = a+b
return fibonacci_tail(n-1, b, c)


def test() -> None:
"""Simple self-test for Fibonacci Tail."""
assert fibonacci_tail(0) == 0, f"Expected 0, got {fibonacci_tail(0)}"
assert fibonacci_tail(5) == 5, f"Expected 5, got {fibonacci_tail(5)}"
assert fibonacci_tail(10) == 55, f"Expected 55, got {fibonacci_tail(10)}"
assert fibonacci_tail(50) == 12586269025, f"Expected 12586269025, got {fibonacci_tail(50)}"
print("✅ All tests passed!")


Expand Down
10 changes: 2 additions & 8 deletions exercises/hello_world/solution.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
SUBMIT = False
SUBMIT = True


def hello_world() -> str:
"""Returns the string 'Hello, World!'.

Example usage:
>>> hello_world()
'Hello, World!'
"""
return ""
return 'Hello, World!'


def test() -> None:
Expand Down
18 changes: 6 additions & 12 deletions exercises/linear_search/solution.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
SUBMIT = False
SUBMIT = True


def linear_search(_items: list[int], _target: int) -> int:
"""Find the first index of a target value in a list.

Example usage:
>>> linear_search([10, 20, 30, 40, 50], 30)
2
>>> linear_search([1, 2, 3, 4, 5], 10)
-1
>>> linear_search([], 5)
-1
"""
return 0
"""Find the first index of a target value in a list."""
for index, element in enumerate(_items):
if element == _target:
return index
return -1


def test() -> None:
Expand Down
18 changes: 8 additions & 10 deletions exercises/list_average/solution.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
SUBMIT = False
SUBMIT = True


def list_average(_numbers: list[float]) -> float:
"""Returns the mean of a numeric list.

Example usage:
>>> list_average([1, 2, 3, 4, 5])
2.5
>>> list_average([10, 20, 30])
20.0
"""
return 0.0
"""Returns the mean of a numeric list."""

if not _numbers:
return 0
s = sum(_numbers)
l = len(_numbers)
return s / l


def test() -> None:
Expand Down
24 changes: 14 additions & 10 deletions exercises/list_mode/solution.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from typing import Any

SUBMIT = False
SUBMIT = True


def list_mode(_items: list[Any]) -> Any:
"""Returns the most frequent element in a list.

Example usage:
>>> list_mode([1, 2, 2, 3, 3, 3])
3
>>> list_mode(['a', 'b', 'a'])
'a'
"""
return None
"""Returns the most frequent element in a list."""
if not _items:
return None
elements = {}.fromkeys(_items, 0)

for item in _items:
elements[item] += 1

num = max(elements.values())

for key, value in elements.items():
if value == num:
return key


def test() -> None:
Expand Down
18 changes: 12 additions & 6 deletions exercises/matrix_add/solution.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
SUBMIT = False
SUBMIT = True


def matrix_add(mat1: list[list[int | float]], mat2: list[list[int | float]]) -> list[list[int | float]]:
"""Adds two matrices."""
# Placeholder implementation
_ = mat1
_ = mat2
return []
mat3 = []
temp = [0] * len(mat1[0])
for i in range(len(mat1)):
mat3.append(temp)

for x in range(len(mat1)):
for y in range(len(mat1[0])):
mat3[x][y] = mat1[x][y] + mat2[x][y]

return mat3


def test() -> None:
"""Simple self-test for Matrix Addition."""
cases = [(([1, 2], [3, 4]), [4, 6])] # Example matrices
cases = [(([[1, 2]], [[3, 4]]), [[4, 6]])] # Example matrices
for input_data, expected in cases:
try:
res = matrix_add(*input_data)
Expand Down
Loading
Loading