diff --git a/exercises/binary_search_recursive/solution.py b/exercises/binary_search_recursive/solution.py index c437af8..97235c5 100644 --- a/exercises/binary_search_recursive/solution.py +++ b/exercises/binary_search_recursive/solution.py @@ -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: diff --git a/exercises/case_inverter/solution.py b/exercises/case_inverter/solution.py index 4f8a5db..dc573fa 100644 --- a/exercises/case_inverter/solution.py +++ b/exercises/case_inverter/solution.py @@ -1,19 +1,20 @@ -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: @@ -21,8 +22,8 @@ def test() -> None: 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"), ] diff --git a/exercises/celsius_to_fahrenheit/solution.py b/exercises/celsius_to_fahrenheit/solution.py index fedf347..4012911 100644 --- a/exercises/celsius_to_fahrenheit/solution.py +++ b/exercises/celsius_to_fahrenheit/solution.py @@ -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: diff --git a/exercises/char_frequency/solution.py b/exercises/char_frequency/solution.py index 3a45f7f..a529101 100644 --- a/exercises/char_frequency/solution.py +++ b/exercises/char_frequency/solution.py @@ -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: diff --git a/exercises/count_vowels/solution.py b/exercises/count_vowels/solution.py index e03c7cf..2b36a42 100644 --- a/exercises/count_vowels/solution.py +++ b/exercises/count_vowels/solution.py @@ -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 diff --git a/exercises/factorial_iterative/solution.py b/exercises/factorial_iterative/solution.py index 78bca9d..1e2fa84 100644 --- a/exercises/factorial_iterative/solution.py +++ b/exercises/factorial_iterative/solution.py @@ -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.""" diff --git a/exercises/factorial_tail/solution.py b/exercises/factorial_tail/solution.py index e661ccc..a3ce695 100644 --- a/exercises/factorial_tail/solution.py +++ b/exercises/factorial_tail/solution.py @@ -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: diff --git a/exercises/fast_exponentiation/solution.py b/exercises/fast_exponentiation/solution.py index 596fd5a..dfd8760 100644 --- a/exercises/fast_exponentiation/solution.py +++ b/exercises/fast_exponentiation/solution.py @@ -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: diff --git a/exercises/fibonacci/solution.py b/exercises/fibonacci/solution.py index b85766d..6e4eda3 100644 --- a/exercises/fibonacci/solution.py +++ b/exercises/fibonacci/solution.py @@ -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) diff --git a/exercises/fibonacci_tail/solution.py b/exercises/fibonacci_tail/solution.py index 1dbb03b..e2f5886 100644 --- a/exercises/fibonacci_tail/solution.py +++ b/exercises/fibonacci_tail/solution.py @@ -1,9 +1,15 @@ -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: @@ -11,6 +17,7 @@ def test() -> None: 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!") diff --git a/exercises/hello_world/solution.py b/exercises/hello_world/solution.py index 19b776e..e65fc9d 100644 --- a/exercises/hello_world/solution.py +++ b/exercises/hello_world/solution.py @@ -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: diff --git a/exercises/linear_search/solution.py b/exercises/linear_search/solution.py index ad4cb38..d21c6b6 100644 --- a/exercises/linear_search/solution.py +++ b/exercises/linear_search/solution.py @@ -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: diff --git a/exercises/list_average/solution.py b/exercises/list_average/solution.py index 48fdf47..2620325 100644 --- a/exercises/list_average/solution.py +++ b/exercises/list_average/solution.py @@ -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: diff --git a/exercises/list_mode/solution.py b/exercises/list_mode/solution.py index 304c15a..a67b5ef 100644 --- a/exercises/list_mode/solution.py +++ b/exercises/list_mode/solution.py @@ -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: diff --git a/exercises/matrix_add/solution.py b/exercises/matrix_add/solution.py index 53deb25..17deab9 100644 --- a/exercises/matrix_add/solution.py +++ b/exercises/matrix_add/solution.py @@ -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) diff --git a/exercises/matrix_mul/solution.py b/exercises/matrix_mul/solution.py index 14f34cd..b03d2c8 100644 --- a/exercises/matrix_mul/solution.py +++ b/exercises/matrix_mul/solution.py @@ -1,6 +1,32 @@ import ast from pathlib import Path +def columna_i(mat, i): + columna = [] + for fila in mat: + columna.append(fila[i]) + return columna + +def producto_escalar(fila_a, fila_b): + escalar = 0 + for i in range(len(fila_a)): + escalar += fila_a[i] + fila_b[i] + return escalar + +def matrix_mul(mat_a, mat_b): + columnas_b = len(mat_b[0]) + mat_c = [] + + for fila_a in mat_a: + fila_c = [] + for i in range(columnas_b): + columna_b = columna_i(mat_b, i) + escalar = producto_escalar(fila_a, columna_b) + fila_c.append(escalar) + mat_c.append(fila_c) + return mat_c + + def test_exercise_solutions_have_asserts() -> None: """Verify that all exercise solution stubs have a test() function with asserts.""" root = Path(__file__).parent.parent diff --git a/exercises/matrix_trace/solution.py b/exercises/matrix_trace/solution.py index 8c1f733..7bc3786 100644 --- a/exercises/matrix_trace/solution.py +++ b/exercises/matrix_trace/solution.py @@ -1,4 +1,4 @@ -SUBMIT = False +SUBMIT = True def matrix_trace(mat: list[list[int | float]]) -> int | float: @@ -9,6 +9,7 @@ def matrix_trace(mat: list[list[int | float]]) -> int | float: return trace + def test() -> None: """Simple self-test for Matrix Trace.""" cases = [ diff --git a/exercises/min_max_list/solution.py b/exercises/min_max_list/solution.py index fa46d23..81dfe33 100644 --- a/exercises/min_max_list/solution.py +++ b/exercises/min_max_list/solution.py @@ -1,18 +1,22 @@ -SUBMIT = False +SUBMIT = True def min_max_list(_items: list[int]) -> tuple[int, int] | None: - """Find both min and max values in a list by iterating once. - - Example usage: - >>> min_max_list([3, 1, 4, 1, 5, 9, 2, 6, 5]) - (1, 9) - >>> min_max_list([10]) - (10, 10) - >>> min_max_list([]) - None - """ - return (0, 0) + """Find both min and max values in a list by iterating once.""" + + if not _items: + return None + + menor = _items[0] + mayor = menor + + for i in range(1, len(_items)): + if _items[i] < menor : + menor = _items[i] + if _items[i] > mayor: + mayor = _items[i] + + return (menor, mayor) def test() -> None: diff --git a/exercises/palindrome/solution.py b/exercises/palindrome/solution.py index 7664e28..7048bd0 100644 --- a/exercises/palindrome/solution.py +++ b/exercises/palindrome/solution.py @@ -1,17 +1,21 @@ -SUBMIT = False +SUBMIT = True def palindrome(_text: str) -> bool: # noqa: ARG001 - """Checks if a string is a palindrome. - - Example usage: - >>> palindrome("racecar") - True - >>> palindrome("hello") - False - """ - return False + """Checks if a string is a palindrome.""" + new_text = [] + for char in _text: + new_text.append(char) + i, j = 0, len(new_text)-1 + + while i < j: + if new_text[i] != new_text[j]: + return False + i +=1 + j -= 1 + return True + def test() -> None: diff --git a/exercises/personalized_greeting/solution.py b/exercises/personalized_greeting/solution.py index aed3a12..18dbd35 100644 --- a/exercises/personalized_greeting/solution.py +++ b/exercises/personalized_greeting/solution.py @@ -1,17 +1,10 @@ -SUBMIT = False +SUBMIT = True def personalized_greeting(_name: str, _age: int) -> str: # noqa: ARG001 - """Returns a personalized greeting message. - - Example usage: - >>> personalized_greeting("Alice", 25) - 'Hello Alice, you are 25 years old' - >>> personalized_greeting("Bob", 30) - 'Hello Bob, you are 30 years old' - """ - return "" + """Returns a personalized greeting message.""" + return f'Hello {_name}, you are {_age} years old' def test() -> None: diff --git a/exercises/poly_add/solution.py b/exercises/poly_add/solution.py index c6932aa..feb69b5 100644 --- a/exercises/poly_add/solution.py +++ b/exercises/poly_add/solution.py @@ -1,18 +1,27 @@ -SUBMIT = False +SUBMIT = True def poly_add(poly1: list[int | float], poly2: list[int | float]) -> list[int | float]: """Adds two polynomials.""" - _poly1 = poly1 - _poly2 = poly2 - # Placeholder implementation: Add actual logic here. - # For now, return an empty list to pass initial checks. - return [] - + + if len(poly1) < len(poly2): + temp = [0] * (len(poly2) - len(poly1)) + temp.extend(poly1) + poly1 = temp + + elif len(poly2) < len(poly1): + temp = [0] * (len(poly1) - len(poly2)) + temp.extend(poly2) + poly2 = temp + + new_poly = [] + for i in range(len(poly1)): + new_poly.append(poly1[i]+poly2[i]) + return new_poly def test() -> None: """Simple self-test for Polynomial Addition.""" - cases = [(([1, 2], [3, 4]), [4, 6]), (([1, 0, 1], [1, 1]), [1, 1, 1])] + cases = [(([1, 2], [3, 4]), [4, 6]), (([1, 0, 1], [1, 1]), [1, 1, 2])] for input_data, expected in cases: try: res = poly_add(*input_data) diff --git a/exercises/poly_evaluate/solution.py b/exercises/poly_evaluate/solution.py index b1c4db3..2b0f55b 100644 --- a/exercises/poly_evaluate/solution.py +++ b/exercises/poly_evaluate/solution.py @@ -1,13 +1,13 @@ -SUBMIT = False +SUBMIT = True def poly_evaluate(poly: list[int | float], x: int | float) -> int | float: """Evaluates a polynomial at a given value x using Horner's method.""" - result = 0 - for coeff in reversed(poly): - result = result * x + coeff - return result - + count = 0 + for i in range(len(poly)-1, -1, -1): + count += poly[i] * (x**i) + + return count def test() -> None: """Simple self-test for Polynomial Evaluation.""" diff --git a/exercises/poly_mul/solution.py b/exercises/poly_mul/solution.py index ee4fab3..aa6890f 100644 --- a/exercises/poly_mul/solution.py +++ b/exercises/poly_mul/solution.py @@ -1,12 +1,19 @@ -SUBMIT = False +SUBMIT = True def poly_mul(poly1: list[int | float], poly2: list[int | float]) -> list[int | float]: """Multiplies two polynomials.""" - _poly1 = poly1 - _poly2 = poly2 - # Placeholder implementation - return [] + poly1.reverse() + poly2.reverse() + + grade = len(poly1) + len(poly2) - 1 + new_poly = [0]*grade + + for i, num1 in enumerate(poly1): + for j, num2 in enumerate(poly2): + new_poly[i+j] += num1 * num2 + new_poly.reverse() + return new_poly def test() -> None: diff --git a/exercises/prefix_sums/solution.py b/exercises/prefix_sums/solution.py index 1e3d167..e2a35ff 100644 --- a/exercises/prefix_sums/solution.py +++ b/exercises/prefix_sums/solution.py @@ -1,16 +1,15 @@ -SUBMIT = False +SUBMIT = True def prefix_sums(_numbers: list[int]) -> list[int]: - """Returns the cumulative sums of a list. - - Example usage: - >>> prefix_sums([1, 2, 3]) - [1, 3, 6] - >>> prefix_sums([10, -10, 5]) - [10, 0, 5] - """ - return [] + """Returns the cumulative sums of a list""" + numbers_sum = [] + for i in range(len(_numbers)): + count = _numbers[i] + for j in range(i): + count += _numbers[j] + numbers_sum.append(count) + return numbers_sum def test() -> None: diff --git a/exercises/prime_check/solution.py b/exercises/prime_check/solution.py index 3cb981b..dc5a599 100644 --- a/exercises/prime_check/solution.py +++ b/exercises/prime_check/solution.py @@ -1,19 +1,19 @@ -SUBMIT = False +SUBMIT = True def prime_check(_n: int) -> bool: # noqa: ARG001 - """Checks if a number is prime. - - Example usage: - >>> prime_check(2) - True - >>> prime_check(4) - False - >>> prime_check(17) - True - """ - return False + """Checks if a number is prime""" + if _n == 0 or _n == 1: + return False + raiz = _n**(1/2) + + i = 2 + while i <= raiz: + if _n%i == 0: + return False + i += 1 + return True def test() -> None: diff --git a/exercises/recursive_min_max/solution.py b/exercises/recursive_min_max/solution.py index c9fcfce..aee4a5b 100644 --- a/exercises/recursive_min_max/solution.py +++ b/exercises/recursive_min_max/solution.py @@ -1,11 +1,22 @@ -SUBMIT = False - +SUBMIT = True def recursive_min_max( lst: list[int], low: int = 0, high: int | None = None ) -> tuple[int, int]: """Recursive Min/Max""" - pass + if high == None: + high = len(lst) + + if low == high-1: + min_value = lst[low] + max_value = min_value + return min_value, max_value + + mid = (low + high) // 2 + min_value = min(recursive_min_max(lst, low, mid)[0], recursive_min_max(lst, mid, high)[0]) + max_value = max(recursive_min_max(lst, low, mid)[1], recursive_min_max(lst, mid, high)[1]) + + return min_value, max_value def test() -> None: diff --git a/exercises/simple_interest/solution.py b/exercises/simple_interest/solution.py index 944bc7f..81dd7f1 100644 --- a/exercises/simple_interest/solution.py +++ b/exercises/simple_interest/solution.py @@ -1,17 +1,10 @@ -SUBMIT = False +SUBMIT = True def simple_interest(_principal: float, _rate: float, _time: float) -> float: # noqa: ARG001 - """Calculates simple interest using the formula: I = (P * R * T) / 100. - - Example usage: - >>> simple_interest(1000, 5, 2) - 100.0 - >>> simple_interest(500, 3.5, 1) - 17.5 - """ - return 0.0 + """Calculates simple interest using the formula: I = (P * R * T) / 100.""" + return _principal * _rate * _time / 100 def test() -> None: diff --git a/exercises/string_reversal/solution.py b/exercises/string_reversal/solution.py index 9f36e58..db7c773 100644 --- a/exercises/string_reversal/solution.py +++ b/exercises/string_reversal/solution.py @@ -1,13 +1,14 @@ -SUBMIT = False +SUBMIT = True def string_reversal(s: str) -> str: # noqa: ARG001 """ Reverses a string. """ - # Add a basic assert to the stub to satisfy the test - assert True, "Placeholder assert for string reversal" # Placeholder assert - return "" + new_s = '' + for char in s: + new_s = char + new_s + return new_s def test() -> None: diff --git a/exercises/sum_list_recursive/solution.py b/exercises/sum_list_recursive/solution.py index d041b78..07d7759 100644 --- a/exercises/sum_list_recursive/solution.py +++ b/exercises/sum_list_recursive/solution.py @@ -1,4 +1,4 @@ -SUBMIT = False +SUBMIT = True def sum_list_recursive(lst: list[int]) -> int: diff --git a/exercises/sum_two_numbers/solution.py b/exercises/sum_two_numbers/solution.py index 6bed6c8..894e77d 100644 --- a/exercises/sum_two_numbers/solution.py +++ b/exercises/sum_two_numbers/solution.py @@ -1,19 +1,10 @@ -SUBMIT = False +SUBMIT = True def sum_two_numbers(_a: int, _b: int) -> int: # noqa: ARG001 - """Returns the sum of two integers. - - Example usage: - >>> sum_two_numbers(3, 4) - 7 - >>> sum_two_numbers(-1, 1) - 0 - >>> sum_two_numbers(0, 0) - 0 - """ - return 0 + """Returns the sum of two integers.""" + return _a + _b def test() -> None: diff --git a/exercises/variable_swap/solution.py b/exercises/variable_swap/solution.py index cceb784..fb70377 100644 --- a/exercises/variable_swap/solution.py +++ b/exercises/variable_swap/solution.py @@ -1,17 +1,11 @@ from typing import Any -SUBMIT = False +SUBMIT = True def variable_swap(a: Any, b: Any) -> tuple[Any, Any]: - """Swaps the values of a and b. - - Example usage: - >>> variable_swap(5, 10) - (10, 5) - >>> variable_swap("hello", "world") - ('world', 'hello') - """ + """Swaps the values of a and b.""" + a,b = b,a return a, b diff --git a/exercises/word_count/solution.py b/exercises/word_count/solution.py index 41c9a8d..63240ad 100644 --- a/exercises/word_count/solution.py +++ b/exercises/word_count/solution.py @@ -1,9 +1,12 @@ -SUBMIT = False +SUBMIT = True def word_count(s: str) -> int: """Counts the number of words in a string.""" - return len(s.split()) + + num = s.split() + num = len(num) + return num def test() -> None: