forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into count-character
- Loading branch information
Showing
7 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
A module for generating Fibonacci sequences. | ||
Module contents: | ||
- generate_fibonacci: Generates Fibonacci sequence up to n terms. | ||
The Fibonacci sequence is a series of numbers where each number is the sum of the two | ||
preceding ones, starting with 0 and 1. | ||
Author: Fahed Daibes | ||
Created: 12-Jan-2025 | ||
""" | ||
|
||
|
||
def generate_fibonacci(n: int) -> list: | ||
""" | ||
Generates the first n terms of the Fibonacci sequence. | ||
Parameters: | ||
- n (int): The number of terms to generate. | ||
Returns: | ||
- list: A list containing the first n terms of the Fibonacci sequence. | ||
Raises: | ||
- AssertionError: If the input is not a positive integer. | ||
Examples: | ||
>>> generate_fibonacci(0) | ||
[] | ||
>>> generate_fibonacci(1) | ||
[0] | ||
>>> generate_fibonacci(5) | ||
[0, 1, 1, 2, 3] | ||
>>> generate_fibonacci("five") | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: Input must be a non-negative integer. | ||
""" | ||
# Defensive check | ||
assert isinstance(n, int) and n >= 0, "Input must be a non-negative integer." | ||
|
||
if n == 0: | ||
return [] | ||
if n == 1: | ||
return [0] | ||
|
||
sequence = [0, 1] | ||
for _ in range(2, n): | ||
sequence.append(sequence[-1] + sequence[-2]) | ||
|
||
return sequence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# !/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
gcd.py | ||
This module provides a function to calculate the greatest common divisor (GCD) | ||
of two integers using the Euclidean algorithm. | ||
Author: Fahed Daibes | ||
Date: Jan 12 2025 | ||
Group: ET6-foundations-group-16 | ||
""" | ||
|
||
|
||
def gcd(a: int, b: int) -> int: | ||
""" | ||
Calculates the greatest common divisor (GCD) of two integers. | ||
Parameters: | ||
- a (int): The first integer. | ||
- b (int): The second integer. | ||
Returns: | ||
- int: The GCD of the two numbers. | ||
Raises: | ||
- AssertionError: If either input is not an integer. | ||
Examples: | ||
>>> gcd(48, 18) | ||
6 | ||
>>> gcd(101, 103) | ||
1 | ||
>>> gcd(0, 25) | ||
25 | ||
>>> gcd(0, 0) | ||
0 | ||
>>> gcd("eight", 16) | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: Both inputs must be integers. | ||
""" | ||
# Defensive check | ||
assert isinstance(a, int) and isinstance(b, int), "Both inputs must be integers." | ||
|
||
while b != 0: | ||
a, b = b, a % b | ||
|
||
return abs(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
A module for checking if a number is positive. | ||
Module contents: | ||
- is_positive: Determines if a given number is positive. | ||
Author: Faisal Minawi | ||
Created: 2025-01-08 | ||
""" | ||
|
||
|
||
def is_positive(n: float) -> bool: | ||
"""Determines if a given number is positive. | ||
A number is considered positive if it is greater than 0. | ||
Raises: | ||
TypeError: If the input is not a real number (int or float). | ||
Parameters: | ||
n: float or int, the number to check. | ||
Returns: | ||
bool: True if the number is greater than 0, False otherwise. | ||
Examples: | ||
>>> is_positive(5) | ||
True | ||
>>> is_positive(-3) | ||
False | ||
>>> is_positive(0) | ||
False | ||
>>> is_positive(3.14) | ||
True | ||
>>> is_positive(-2.5) | ||
False | ||
>>> is_positive(True) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Input must be a real number (int or float) | ||
""" | ||
if isinstance(n, bool): | ||
raise TypeError("Input must be a real number (int or float)") | ||
if not isinstance(n, (int, float)): | ||
raise TypeError("Input must be a real number (int or float)") | ||
return n > 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
test_fibonacci.py | ||
This module contains unit tests for the `generate_fibonacci` function, which generates | ||
the Fibonacci sequence up to a given number of terms. | ||
Author: Fahed Daibes | ||
Date: Jan 12 2025 | ||
Group: ET6-foundations-group-16 | ||
Tests: | ||
- Valid inputs for various sequence lengths. | ||
- Edge cases, such as n=0 and n=1. | ||
- Invalid inputs, such as negative numbers and non-integer types. | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.fibonacci import generate_fibonacci | ||
|
||
|
||
def assert_fibonacci(expected, n): | ||
""" | ||
Custom assertion function to compare the result of generate_fibonacci with the expected value. | ||
Parameters: | ||
- expected (list): The expected Fibonacci sequence. | ||
- n (int): The input number of terms. | ||
Raises: | ||
- AssertionError: If the result of generate_fibonacci does not match the expected value. | ||
""" | ||
result = generate_fibonacci(n) | ||
assert result == expected, f"Expected {expected} for {n}, but got {result}." | ||
|
||
|
||
class TestFibonacci(unittest.TestCase): | ||
""" | ||
This test class contains unit tests for the `generate_fibonacci` function. | ||
Each test uses only one assertion with the custom `assert_fibonacci` function. | ||
""" | ||
|
||
def test_zero_terms(self): | ||
"""Test case for zero terms.""" | ||
assert_fibonacci([], 0) | ||
|
||
def test_one_term(self): | ||
"""Test case for one term.""" | ||
assert_fibonacci([0], 1) | ||
|
||
def test_five_terms(self): | ||
"""Test case for five terms.""" | ||
assert_fibonacci([0, 1, 1, 2, 3], 5) | ||
|
||
def test_large_sequence(self): | ||
"""Test case for a larger sequence.""" | ||
assert_fibonacci([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], 10) | ||
|
||
def test_negative_input(self): | ||
"""Test case for negative input.""" | ||
with self.assertRaises(AssertionError): | ||
generate_fibonacci(-3) | ||
|
||
def test_invalid_string(self): | ||
"""Test case for an invalid string input.""" | ||
with self.assertRaises(AssertionError): | ||
generate_fibonacci("ten") | ||
|
||
def test_invalid_float(self): | ||
"""Test case for an invalid float input.""" | ||
with self.assertRaises(AssertionError): | ||
generate_fibonacci(3.5) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# !/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
test_gcd.py | ||
This module contains unit tests for the `gcd` function, which calculates the | ||
greatest common divisor (GCD) of two integers. | ||
Author: Fahed Daibes | ||
Date: Jan 12 2025 | ||
Group: ET6-foundations-group-16 | ||
Tests: | ||
- Valid inputs, including positive, negative, and zero values. | ||
- Invalid inputs, such as non-integer types. | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.gcd import gcd | ||
|
||
|
||
def assert_gcd(expected, a, b): | ||
""" | ||
Custom assertion function to compare the result of gcd with the expected value. | ||
Parameters: | ||
- expected (int): The expected GCD. | ||
- a (int): The first input integer. | ||
- b (int): The second input integer. | ||
Raises: | ||
- AssertionError: If the result of gcd does not match the expected value. | ||
""" | ||
result = gcd(a, b) | ||
assert result == expected, ( | ||
f"Expected {expected} for gcd({a}, {b}), but got {result}." | ||
) | ||
|
||
|
||
class TestGCD(unittest.TestCase): | ||
""" | ||
This test class contains unit tests for the `gcd` function. | ||
Each test uses only one assertion with the custom `assert_gcd` function. | ||
""" | ||
|
||
def test_gcd_positive_numbers(self): | ||
"""Test case for two positive numbers.""" | ||
assert_gcd(6, 48, 18) | ||
|
||
def test_gcd_coprime_numbers(self): | ||
"""Test case for two coprime numbers.""" | ||
assert_gcd(1, 101, 103) | ||
|
||
def test_gcd_one_zero(self): | ||
"""Test case for one number being zero.""" | ||
assert_gcd(25, 0, 25) | ||
|
||
def test_gcd_both_zero(self): | ||
"""Test case for both numbers being zero.""" | ||
assert_gcd(0, 0, 0) | ||
|
||
def test_gcd_negative_numbers(self): | ||
"""Test case for negative numbers.""" | ||
assert_gcd(6, -48, -18) | ||
|
||
def test_gcd_mixed_signs(self): | ||
"""Test case for one positive and one negative number.""" | ||
assert_gcd(6, 48, -18) | ||
|
||
def test_invalid_string_input(self): | ||
"""Test case for a string input.""" | ||
with self.assertRaises(AssertionError): | ||
gcd("forty-eight", 18) | ||
|
||
def test_invalid_float_input(self): | ||
"""Test case for a float input.""" | ||
with self.assertRaises(AssertionError): | ||
gcd(48.5, 18) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.