Skip to content

Commit

Permalink
Merge pull request #33 from MIT-Emerging-Talent/challenge/hours-to-mi…
Browse files Browse the repository at this point in the history
…nutes

Challenge/hours to minutes
  • Loading branch information
Safiya-hash authored Jan 8, 2025
2 parents c3970e1 + c397aa5 commit ae835ba
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
50 changes: 50 additions & 0 deletions solutions/hours_to_minutes.py
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)
50 changes: 50 additions & 0 deletions solutions/tests/test_hours_to_minutes.py
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])

0 comments on commit ae835ba

Please sign in to comment.