-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Challenge/1 even odd range rouaa #52
Changes from all commits
c80c985
187b635
9f85f1a
17ab504
a103249
e3458c6
3663ff5
d7924fb
3cfec3a
63eebd3
0a2a47c
b555315
2870933
c787167
861d1ea
c1be394
43b797a
1dd14d1
b523768
2953217
9ba9be7
8fa4c1e
a13cab3
6e1833e
3e326fd
1f90043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
This module contains a function that takes a range of numbers and returns | ||
two lists: one for even numbers and one for odd numbers within the specified range. | ||
|
||
Function: | ||
even_odd_range(start: int, end: int) -> tuple[list[int], list[int]] | ||
|
||
Arguments: | ||
start (int): The starting number of the range (inclusive). | ||
end (int): The ending number of the range (inclusive). | ||
|
||
Returns: | ||
tuple[list[int], list[int]]: A tuple containing two lists: | ||
- A list of even numbers in the range. | ||
- A list of odd numbers in the range. | ||
|
||
Raises: | ||
ValueError: If start or end are not integers. | ||
ValueError: If start is greater than end. | ||
|
||
Examples: | ||
>>> even_odd_range(1, 5) | ||
([2, 4], [1, 3, 5]) | ||
>>> even_odd_range(0, 0) | ||
([0], []) | ||
>>> even_odd_range(-3, 3) | ||
([0, -2, 2], [-3, -1, 1, 3]) | ||
>>> even_odd_range(5, 3) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The start value must not be greater than the end value. | ||
""" | ||
|
||
from typing import List, Tuple | ||
|
||
|
||
def even_odd_range(start: int, end: int) -> Tuple[List[int], List[int]]: | ||
""" | ||
Returns two lists: one with the even numbers and one with the odd numbers | ||
within the specified range. | ||
|
||
Arguments: | ||
start -- The starting number (inclusive) | ||
end -- The ending number (inclusive) | ||
|
||
Returns: | ||
A tuple of two lists: the first list contains even numbers, the second contains odd numbers. | ||
|
||
Examples: | ||
>>> even_odd_range(-2, 2) | ||
([0, -2, 2], [-1, 1]) | ||
""" | ||
from typing import List, Tuple | ||
Check failure on line 53 in solutions/even_odd_range.py GitHub Actions / py_lintingRuff (E402)
Check failure on line 53 in solutions/even_odd_range.py GitHub Actions / py_lintingRuff (E402)
|
||
|
||
def even_odd_range(start: int, end: int) -> Tuple[List[int], List[int]]: | ||
Check failure on line 55 in solutions/even_odd_range.py GitHub Actions / py_lintingRuff (F811)
Check failure on line 55 in solutions/even_odd_range.py GitHub Actions / py_lintingRuff (F811)
|
||
""" | ||
Returns two lists: one with the even numbers and one with the odd numbers | ||
within the specified range. | ||
|
||
Arguments: | ||
start -- The starting number (inclusive) | ||
end -- The ending number (inclusive) | ||
|
||
Returns: | ||
A tuple of two lists: the first list contains even numbers, the second contains odd numbers. | ||
|
||
Examples: | ||
>>> even_odd_range(-2, 2) | ||
([0, -2, 2], [-1, 1]) | ||
""" | ||
if not isinstance(start, int) or not isinstance(end, int): | ||
raise ValueError("Both start and end must be integers.") | ||
if start > end: | ||
raise ValueError("The start value must not be greater than the end value.") | ||
|
||
even_numbers = [num for num in range(start, end + 1) if num % 2 == 0] | ||
odd_numbers = [num for num in range(start, end + 1) if num % 2 != 0] | ||
|
||
return even_numbers, odd_numbers |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import unittest | ||
import sys | ||
from pathlib import Path | ||
sys.path.append(str(Path(file).parent.parent)) | ||
Check failure on line 4 in solutions/tests/test_even_odd_range.py GitHub Actions / py_lintingRuff (F821)
Check failure on line 4 in solutions/tests/test_even_odd_range.py GitHub Actions / py_lintingRuff (F821)
|
||
from even_odd_range import even_odd_range | ||
|
||
|
||
class TestEvenOddRange(unittest.TestCase): | ||
""" | ||
Test case for the even_odd_range function. | ||
""" | ||
|
||
def test_even_odd_range_valid(self): | ||
"""Tests valid ranges with both even and odd numbers.""" | ||
result = even_odd_range(1, 5) | ||
self.assertEqual(result, ([2, 4], [1, 3, 5])) | ||
|
||
def test_even_odd_range_single(self): | ||
"""Tests single number in the range (even).""" | ||
result = even_odd_range(4, 4) | ||
self.assertEqual(result, ([4], [])) | ||
|
||
def test_even_odd_range_negative_numbers(self): | ||
"""Tests ranges that include negative numbers.""" | ||
result = even_odd_range(-5, 0) | ||
self.assertEqual(result, ([-4, -2, 0], [-5, -3, -1])) | ||
|
||
def test_even_odd_range_large_range_even(self): | ||
"""Tests large range for even numbers.""" | ||
result = even_odd_range(1, 100) | ||
self.assertEqual(result[0], list(range(2, 101, 2))) | ||
|
||
def test_even_odd_range_large_range_odd(self): | ||
"""Tests large range for odd numbers.""" | ||
result = even_odd_range(1, 100) | ||
self.assertEqual(result[1], list(range(1, 101, 2))) | ||
|
||
def test_even_odd_range_invalid_type(self): | ||
"""Tests ValueError for non-integer inputs.""" | ||
with self.assertRaises(ValueError) as context: | ||
even_odd_range(1, "5") | ||
self.assertEqual(str(context.exception), "Both start and end must be integers.") | ||
|
||
def test_even_odd_range_invalid_order(self): | ||
"""Tests ValueError when start > end.""" | ||
with self.assertRaises(ValueError) as context: | ||
even_odd_range(5, 3) | ||
self.assertEqual(str(context.exception), "The start value must not be greater than the end value.") | ||
|
||
def test_even_odd_range_empty_range(self): | ||
"""Tests empty range with the same start and end.""" | ||
result = even_odd_range(5, 5) | ||
self.assertEqual(result, ([], [5])) | ||
|
||
def test_even_odd_range_all_negative(self): | ||
"""Tests range with only negative numbers.""" | ||
result = even_odd_range(-10, -1) | ||
self.assertEqual(result, ([-10, -8, -6, -4, -2], [-9, -7, -5, -3, -1])) | ||
|
||
if _name_ == "_main_": | ||
Check failure on line 60 in solutions/tests/test_even_odd_range.py GitHub Actions / py_lintingRuff (F821)
Check failure on line 60 in solutions/tests/test_even_odd_range.py GitHub Actions / py_lintingRuff (F821)
|
||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your test file is also well written, and I liked how each function has a descriptive docstring and that me it easier for my to understand the purpose of each test. Overall, the quality is high and it can be even better with some changes.
They both check when the start is greater than the end and raise a
ValueError
and this seems redundant, and you can consider dropping one of the tests. I suggest keeping the first function since it describes the scenario better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abdoalsir
Thank you so much, Abdo, for your detailed feedback and constructive suggestions!
I really appreciate you pointing out the strengths in the code and offering ways to improve it further. Here’s how I plan to address your comments:
1. Function Docstring and Doctests: I’ll update the function docstring to include more details about the strategy, making it easier for others to understand how it works at a glance. I’ll also add doctests to show examples of the function’s behavior.
2. Test File Improvements:
• I’ll add a module docstring to explain the purpose of the test file and summarize the types of cases it covers.
• I’ll include tests for larger ranges and negative numbers to make the test cases more comprehensive.
• Regarding the redundant tests, I’ll remove the test_even_odd_range_empty test as you suggested and keep the test_even_odd_range_invalid_order since it’s clearer.
Once I make these changes, I hope the code will be more polished and easier to understand. Thanks again for taking the time to review and for your helpful insights—it really means a lot! 😃👌.