Skip to content
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

Add even number function and its corresponding unit to main #25

Merged
merged 14 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,8 @@
},

// Enable/disable update table of contents on save
"markdown.extension.toc.updateOnSave": false
"markdown.extension.toc.updateOnSave": false,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
40 changes: 40 additions & 0 deletions solutions/is_even.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on XX XX XX
A module for checking to see if an integer is even.
@author: Luyando .E. Chitindi
"""


def is_even(number: int) -> bool:
"""
This checks if an integer is even.
Parameters:
number: int, the number to check
Returns -> bool:
True if the number is even, false otherwise.
Raises:
AssertionError: if the input is not an integer
Example:
>>> is_even(4)
True
>>> is_even(3)
False
>>> is_even(0)
True
>>> is_even("hello")
Traceback (most recent call last):
...
AssertionError: Input must be an integer.
"""
assert isinstance(number, int), "Input must be an integer"
if number % 2 == 0:
return True
return False
55 changes: 55 additions & 0 deletions solutions/tests/test_is_even.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
from solutions.is_even import is_even


class TestIsEven(unittest.TestCase):
"""Test the is_even function"""

def test_even_number(self):
"""It should evaluate 4 to true"""
actual = is_even(4)
expected = True
self.assertEqual(actual, expected)

def test_odd_number(self):
"""It should evaluate 3 to False"""
actual = is_even(3)
expected = False
self.assertEqual(actual, expected)

def test_zero(self):
"""It should evaluate 0 to True"""
actual = is_even(0)
expected = True
self.assertEqual(actual, expected)

def test_negative_even_number(self):
"""It should evaluate -2 to True"""
actual = is_even(-2)
expected = True
self.assertEqual(actual, expected)

def test_negative_odd_number(self):
"""It should evaluate -3 to False"""
actual = is_even(-3)
expected = False
self.assertEqual(actual, expected)

def test_non_integer_input(self):
"""It should raise an assertion error if the argument is not an integer"""
with self.assertRaises(AssertionError):
is_even(3.5)

with self.assertRaises(AssertionError):
is_even("string")

def test_less_than_0(self):
"""It should raise an assertion error if the argument is less than 0"""
with self.assertRaises(AssertionError):
is_even(-2.5)


if __name__ == "__main__":
unittest.main()
Loading