Hone logo
Hone
Problems

Implementing a Simplified Operator Module in Python

The Python operator module provides a set of efficient functions corresponding to the intrinsic operators in Python. This challenge asks you to implement a simplified version of this module, focusing on a subset of operators to understand the underlying principles. This exercise will enhance your understanding of functional programming concepts and how operators can be abstracted into functions.

Problem Description

You are tasked with creating a module that mimics a subset of the Python operator module. Specifically, you need to implement functions for the following operators: add, sub, mul, truediv, floordiv, mod, pow. These functions should accept two arguments and return the result of applying the corresponding operation.

What needs to be achieved:

  • Create a Python module (a single .py file) containing functions that replicate the behavior of add, sub, mul, truediv, floordiv, mod, and pow operators.
  • Each function should take two numerical arguments (integers or floats) and return the result of the operation.
  • Handle potential TypeError if the inputs are not numbers.
  • Handle ZeroDivisionError for truediv and floordiv operations.

Key Requirements:

  • The functions must be named exactly as specified: add, sub, mul, truediv, floordiv, mod, pow.
  • The functions should perform the correct mathematical operations.
  • Error handling is crucial for robustness.

Expected Behavior:

The functions should behave identically to their corresponding operators when applied to numerical inputs. For example, add(2, 3) should return 5, and mul(4, 5) should return 20. When division by zero occurs, a ZeroDivisionError should be raised. If non-numerical inputs are provided, a TypeError should be raised.

Edge Cases to Consider:

  • Division by zero (for truediv and floordiv).
  • Non-numerical inputs (e.g., strings, lists) for any of the functions.
  • Negative numbers as inputs.
  • Floating-point numbers as inputs.
  • Large numbers that might cause overflow (though this is less critical for this exercise).

Examples

Example 1:

Input: add(5, 2)
Output: 7
Explanation: The add function performs addition of 5 and 2.

Example 2:

Input: mul(3.5, 2)
Output: 7.0
Explanation: The mul function performs multiplication of 3.5 and 2.

Example 3:

Input: truediv(10, 0)
Output: ZeroDivisionError
Explanation: Division by zero raises a ZeroDivisionError.

Example 4:

Input: mod("hello", 5)
Output: TypeError
Explanation:  String input to mod raises a TypeError.

Constraints

  • The module should be implemented in Python 3.
  • All functions must accept numerical inputs (integers or floats).
  • The functions should raise TypeError if the inputs are not numbers.
  • The functions should raise ZeroDivisionError if division by zero is attempted.
  • The module should be a single .py file.

Notes

  • Consider using the isinstance() function to check the type of the inputs.
  • Think about how to handle the different types of division (true division vs. floor division).
  • This is a good opportunity to practice exception handling.
  • Focus on clarity and readability in your code. Good variable names and comments are encouraged.
  • You don't need to implement all the features of the full operator module; just the specified subset.
Loading editor...
python