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 longest_word
- Loading branch information
Showing
8 changed files
with
311 additions
and
13 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
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,35 @@ | ||
""" | ||
Group: ET6-foundations-group-16 | ||
Author:Majd Abualsoud | ||
Date: January 11, 2025 | ||
This module provides a function to calculate the area of a circle given its radius. | ||
""" | ||
|
||
import math | ||
|
||
|
||
def area_of_circle(radius: float) -> float: | ||
""" | ||
Calculate the area of a circle given its radius. | ||
Args: | ||
radius (float): The radius of the circle. Must be a non-negative number. | ||
Returns: | ||
float: The area of the circle. | ||
Raises: | ||
ValueError: If the radius is negative. | ||
Examples: | ||
>>> area_of_circle(1) | ||
3.141592653589793 | ||
>>> area_of_circle(0) | ||
0.0 | ||
>>> area_of_circle(2.5) | ||
19.634954084936208 | ||
""" | ||
if radius < 0: | ||
raise ValueError("The radius must be a non-negative number.") | ||
|
||
return math.pi * radius * radius |
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,59 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for finding the common elements between two input lists. | ||
Created on 11/1/2025 | ||
@author: Mohamed Altayeb | ||
Group: ET foundations group 16 (Matrix) | ||
Module functions: | ||
- common_elements: finds the common elements between two lists. | ||
""" | ||
|
||
|
||
def common_elements(list1: list, list2: list) -> list: | ||
""" | ||
Compares two input lists and returns a list that contains | ||
all the common elements between the two input lists without repeating | ||
the common elements if they are duplicated in the input lists: | ||
Arguments: | ||
list1 (list): the first list we want to compare. | ||
list2 (list): the second list we want to compare. | ||
Returns-> list: A list containing all common elements between two input lists, | ||
the returned list will have the common elements of the first input list | ||
followed by the common elements of the second input list. | ||
Raises: | ||
AssertionError: if inputs are not lists. | ||
>>> common_elements([1,2,3,4,5,6] , [4,5,6,7]) | ||
[4, 5, 6] | ||
>>> common_elements(["c", "a", "r"] , ["c", "a", "t"]) | ||
['c', 'a'] | ||
>>> common_elements([[1,2] , [2,3,4], ["mohamed"]] , [["mohamed"], [1,2,3], [2,3,4], [4,5,6]]) | ||
[[2, 3, 4], ['mohamed']] | ||
""" | ||
# Ensure both inputs are lists | ||
if not isinstance(list1, list) or not isinstance(list2, list): | ||
raise AssertionError("Both inputs must be lists") | ||
|
||
common_list_no_duplicates = [] | ||
|
||
# Iterate through elements of the first list | ||
for element1 in list1: | ||
# Iterate through elements of second list comparing | ||
# each element in the second list to the element in the first list | ||
for element2 in list2: | ||
if element1 == element2 and element1 not in common_list_no_duplicates: | ||
# Add the element to the resultant list | ||
common_list_no_duplicates.append(element1) | ||
|
||
# Return the resultant list with common elements with no duplicated elements | ||
return common_list_no_duplicates |
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,39 @@ | ||
""" | ||
Group: ET6-foundations-group-16 | ||
Author:Majd Abualsoud | ||
Date: January 11, 2025 | ||
Test suite for the area_of_circle function. | ||
""" | ||
|
||
import math | ||
import unittest | ||
|
||
from ..area_circle import area_of_circle | ||
|
||
|
||
class TestAreaOfCircle(unittest.TestCase): | ||
""" | ||
Unit tests for the area_of_circle function. | ||
""" | ||
|
||
def test_positive_radius(self): | ||
"""Test area calculation with a positive radius.""" | ||
self.assertAlmostEqual(area_of_circle(1), 3.141592653589793) | ||
self.assertAlmostEqual(area_of_circle(2.5), 19.634954084936208) | ||
|
||
def test_zero_radius(self): | ||
"""Test area calculation with a radius of zero.""" | ||
self.assertAlmostEqual(area_of_circle(0), 0.0) | ||
|
||
def test_negative_radius(self): | ||
"""Test area calculation with a negative radius.""" | ||
with self.assertRaises(ValueError): | ||
area_of_circle(-1) | ||
|
||
def test_large_radius(self): | ||
"""Test area calculation with a large radius.""" | ||
self.assertAlmostEqual(area_of_circle(1e6), math.pi * 1e6 * 1e6) | ||
|
||
|
||
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,152 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for common_elements function | ||
Test categories: | ||
-Standard cases: typical inputs | ||
-Edge cases: extreme inputs and boundaries | ||
-Defensive tests: wrong input types, assertions | ||
Created on 11/1/2025 | ||
@author: Mohamed Altayeb | ||
Group: ET foundations group 16 (Matrix) | ||
""" | ||
|
||
import unittest | ||
|
||
from ..common_elements import common_elements | ||
|
||
|
||
class TestCommonElements(unittest.TestCase): | ||
"""Test suite for the common_elements function""" | ||
|
||
# Standard cases | ||
def test_lists_with_integers(self): | ||
"""it should return a list with the common integers between the two input lists""" | ||
self.assertEqual(common_elements([1, 2, 3], [2, 3]), [2, 3]) | ||
|
||
def test_lists_with_string_elements(self): | ||
"""it should return a list with the common string elements between the two input lists""" | ||
self.assertEqual(common_elements(["a", "b", "c"], ["b", "c"]), ["b", "c"]) | ||
|
||
def test_lists_with_duplicated_elements(self): | ||
"""it should return a list with the common elements between the two input lists | ||
without duplicating the elements in the output list""" | ||
self.assertEqual(common_elements(["a", "b", "b"], ["b", "b", "c"]), ["b"]) | ||
|
||
def test_lists_with_mixed_string_and_integer_elements(self): | ||
"""it should return a list with the common string elements between the two input lists""" | ||
self.assertEqual( | ||
common_elements(["a", "b", 2, 56, "abc"], ["abc", 56, 999]), [56, "abc"] | ||
) | ||
|
||
def test_lists_within_lists(self): | ||
"""it should return a list with the common elements between the two input lists | ||
even if the elements are lists as well""" | ||
self.assertEqual( | ||
common_elements( | ||
[[1, 2], [2, 3, 4], ["mohamed"]], | ||
[["mohamed"], [1, 2, 3], [2, 3, 4], [4, 5, 6]], | ||
), | ||
[[2, 3, 4], ["mohamed"]], | ||
) | ||
|
||
def test_order_of_elements_in_output_list(self): | ||
"""the function should prioritize the order of the elements in the first input list""" | ||
self.assertEqual(common_elements([1, 2, 3], [3, 2, 1]), [1, 2, 3]) | ||
|
||
# Edge cases | ||
def test_long_lists(self): | ||
"""it should return a list with the common elements between the two input lists | ||
regardless of the length of the input lists""" | ||
self.assertEqual( | ||
common_elements( | ||
[ | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
], | ||
[ | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
9, | ||
], | ||
), | ||
[9], | ||
) | ||
|
||
def test_one_of_the_lists_is_empty(self): | ||
"""it should return an empty list""" | ||
self.assertEqual(common_elements(["Mohamed", "cat", "1,2,3"], []), []) | ||
|
||
def test_both_of_the_lists_are_empty(self): | ||
"""it should return an empty list""" | ||
self.assertEqual(common_elements([], []), []) | ||
|
||
# Defensive tests | ||
def test_input1_is_not_a_list(self): | ||
"""It should raise AssertionError if first input is not a list""" | ||
with self.assertRaises(AssertionError): | ||
common_elements("mohamed", [1, 2, 3, 4, 5, 6]) | ||
|
||
def test_input2_is_not_a_list(self): | ||
"""It should raise AssertionError if second input is not a list""" | ||
with self.assertRaises(AssertionError): | ||
common_elements([1, 2, 3, 4, 5, 6], "numbers") |
Oops, something went wrong.