forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from MIT-Emerging-Talent/challenge/hours-to-mi…
…nutes Challenge/hours to minutes
- Loading branch information
Showing
2 changed files
with
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
A module for converting a value in hours to minutes. | ||
Module contents: | ||
> hours_to_minutes: converts a value in hours to minutes. | ||
Created on the 04 Jan 2025 | ||
@author: Tosan Okome | ||
""" | ||
|
||
|
||
def hours_to_minutes(hours: float) -> int: | ||
""" | ||
Converts a given value in hours to its equivalent in minutes. | ||
Parameters: | ||
hours : float | ||
The number of hours to convert to minutes. Must be a numerical value (int or float). | ||
Returns: | ||
int | ||
The equivalent number of minutes. | ||
Raises: | ||
TypeError: | ||
If the input is not a number (int or float). | ||
ValueError: | ||
If the input is negative. | ||
Examples: | ||
>>> hours_to_minutes(4) | ||
240 | ||
>>> hours_to_minutes(0.5) | ||
30 | ||
>>> hours_to_minutes(24) | ||
1440 | ||
>>> hours_to_minutes(12) | ||
720 | ||
""" | ||
if not isinstance(hours, (int, float)): | ||
raise TypeError(f"Invalid input type: {type(hours)}. Must be int or float.") | ||
|
||
if hours < 0: | ||
raise ValueError("Invalid input: hours cannot be negative.") | ||
|
||
return int(hours * 60) |
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,50 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for testing the hours_to_minutes function. | ||
Created on 04 Jan 2025 | ||
@author: Tosan Okome | ||
""" | ||
|
||
import unittest | ||
|
||
from ..hours_to_minutes import hours_to_minutes | ||
|
||
|
||
class TestHoursToMinutes(unittest.TestCase): | ||
"""Test hours_to_minutes function""" | ||
|
||
def test_positive_integer_hours(self): | ||
"""It should return 120""" | ||
actual = hours_to_minutes(2) | ||
expected = 120 | ||
self.assertEqual(actual, expected) | ||
|
||
def test_positive_float_hours(self): | ||
"""It should return 150""" | ||
actual = hours_to_minutes(2.5) | ||
expected = 150 | ||
self.assertEqual(actual, expected) | ||
|
||
def test_zero_hours(self): | ||
"""It should return 0""" | ||
actual = hours_to_minutes(0) | ||
expected = 0 | ||
self.assertEqual(actual, expected) | ||
|
||
def test_negative_hours(self): | ||
"""Negative hours should raise ValueError""" | ||
with self.assertRaises(ValueError): | ||
hours_to_minutes(-1) | ||
|
||
def test_invalid_type_string(self): | ||
"""Strings should raise TypeError""" | ||
with self.assertRaises(TypeError): | ||
hours_to_minutes("two") | ||
|
||
def test_invalid_type_list(self): | ||
"""Lists should raise TypeError""" | ||
with self.assertRaises(TypeError): | ||
hours_to_minutes([1, 2]) |