-
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 4 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,53 @@ | ||
""" | ||
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. | ||
""" | ||
|
||
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. | ||
""" | ||
|
||
# Defensive assertions | ||
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 = [] | ||
odd_numbers = [] | ||
|
||
for num in range(start, end + 1): | ||
if num % 2 == 0: | ||
even_numbers.append(num) | ||
else: | ||
odd_numbers.append(num) | ||
|
||
return even_numbers, odd_numbers |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import unittest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
def test_even_odd_range_invalid_order(self):
"""
Tests that ValueError is raised if start is greater than end.
"""
with self.assertRaises(ValueError):
even_odd_range(5, 3) def test_even_odd_range_empty(self):
"""
Tests that the function raises an error for an empty range where start > end.
"""
with self.assertRaises(ValueError):
even_odd_range(5, 4) They both check when the start is greater than the end and raise a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you so much, Abdo, for your detailed feedback and constructive suggestions! 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! 😃👌. |
||
|
||
from ..even_odd import even_odd_range | ||
|
||
|
||
class TestEvenOddRange(unittest.TestCase): | ||
""" | ||
Test case for the even_odd_range function. | ||
""" | ||
|
||
def test_even_odd_range_valid(self): | ||
""" | ||
Tests that the function correctly splits a range into even and odd numbers. | ||
""" | ||
result = even_odd_range(1, 5) | ||
self.assertEqual(result, ([2, 4], [1, 3, 5])) | ||
|
||
def test_even_odd_range_empty(self): | ||
""" | ||
Tests that the function raises an error for an empty range where start > end. | ||
""" | ||
with self.assertRaises(ValueError): | ||
even_odd_range(5, 4) | ||
|
||
def test_even_odd_range_single(self): | ||
""" | ||
Tests that a single number in the range is classified correctly. | ||
""" | ||
result = even_odd_range(4, 4) | ||
self.assertEqual(result, ([4], [])) | ||
|
||
def test_even_odd_range_invalid_type(self): | ||
""" | ||
Tests that ValueError is raised if start or end are not integers. | ||
""" | ||
with self.assertRaises(ValueError): | ||
even_odd_range(1, "5") | ||
|
||
def test_even_odd_range_invalid_order(self): | ||
""" | ||
Tests that ValueError is raised if start is greater than end. | ||
""" | ||
with self.assertRaises(ValueError): | ||
even_odd_range(5, 3) |
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.
The function docstring is well written and shows clearly how the function is used but it can be improved even more. Consider adding the strategy to help others better understand how the function works before even going into the details of the code.
Also, make sure to add doctests to the function docstring to provide examples on the output of the function