Skip to content

Commit

Permalink
Merge pull request #24 from MIT-Emerging-Talent/reverse-32bit-integer
Browse files Browse the repository at this point in the history
Reverse 32bit integer
  • Loading branch information
SiSaR-Pal authored Dec 31, 2024
2 parents 579f75e + 66f2541 commit 0e7795a
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
65 changes: 65 additions & 0 deletions solutions/reverse_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class ReverseIntegerError(Exception):
"""Custom exception for reverse integer errors."""

pass


def reverse(input_number: int) -> int:
"""
Reverses the digits of a signed 32-bit integer.
Parameters:
input_number (int): The integer to reverse.
Returns:
int: The reversed integer if it falls within the signed 32-bit range [-2^31, 2^31 - 1].
Returns 0 if the reversed integer overflows.
Raises:
AssertionError: If the argument is not an integer.
MemoryError: If the operation exceeds memory limits for large inputs.
ReverseIntegerError: If the input is outside a specified range.
Examples:
>>> reverse(123)
321
>>> reverse(-123)
-321
>>> reverse(120)
21
>>> reverse(0)
0
"""
# Assert that the input is an integer
assert isinstance(input_number, int), "Input must be an integer"

# Define the range for signed 32-bit integers
INT_MIN, INT_MAX = -(2**31), 2**31 - 1

# Raise custom exception if input is out of bounds
if input_number < INT_MIN or input_number > INT_MAX:
raise ReverseIntegerError("Input is outside the 32-bit signed integer range")

# Simulate potential memory errors for extremely large inputs (unlikely in practical scenarios)
try:
# Determine if the number is negative
negative = input_number < 0

# Work with the absolute value of input_number
input_number = abs(input_number)

# Reverse the digits using string manipulation
reversed_x = int(str(input_number)[::-1])

# Restore the sign if the number was negative
if negative:
reversed_x = -reversed_x

# Check for overflow
if reversed_x < INT_MIN or reversed_x > INT_MAX:
return 0

return reversed_x

except MemoryError:
raise MemoryError("Operation exceeded memory limits for large inputs")
75 changes: 75 additions & 0 deletions solutions/tests/test_reverse_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Unit tests for the reverse_integer module.
This module contains a suite of tests for the reverse function, which reverses the digits
of a signed 32-bit integer, ensuring correctness and handling edge cases like overflow.
"""

import unittest
from ..reverse_integer import reverse


class TestReverseInteger(unittest.TestCase):
"""
Test suite for the reverse function in the reverse_integer module.
This class includes tests for typical cases, edge cases, and boundary conditions
to ensure the function behaves as expected.
"""

def test_positive_number(self):
"""Test reversing a positive number."""
self.assertEqual(reverse(123), 321)

def test_negative_number(self):
"""Test reversing a negative number."""
self.assertEqual(reverse(-123), -321)

def test_number_with_trailing_zero(self):
"""Test reversing a number with trailing zeros."""
self.assertEqual(reverse(120), 21)

def test_zero(self):
"""Test reversing zero."""
self.assertEqual(reverse(0), 0)

def test_positive_overflow(self):
"""Test reversing a large positive number that exceeds the 32-bit signed integer range."""
self.assertEqual(reverse(1534236469), 0)

def test_negative_overflow(self):
"""Test reversing a large negative number that exceeds the 32-bit signed integer range."""
self.assertEqual(reverse(-1534236469), 0)

def test_single_digit_positive(self):
"""Test reversing a single positive digit."""
self.assertEqual(reverse(7), 7)

def test_single_digit_negative(self):
"""Test reversing a single negative digit."""
self.assertEqual(reverse(-7), -7)

def test_large_positive_number(self):
"""Test reversing a large positive number that exceeds the 32-bit range."""
self.assertEqual(reverse(1000000003), 0)

def test_large_negative_number(self):
"""Test reversing a large negative number that exceeds the 32-bit range."""
self.assertEqual(reverse(-1000000003), 0)

def test_palindrome_number(self):
"""Test reversing a palindrome number to ensure it remains the same."""
self.assertEqual(reverse(1221), 1221)

def test_invalid_input(self):
"""Test that invalid input raises AssertionError."""
with self.assertRaises(AssertionError):
reverse(None)
with self.assertRaises(AssertionError):
reverse("string")
with self.assertRaises(AssertionError):
reverse(3.14)


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

0 comments on commit 0e7795a

Please sign in to comment.