Hone logo
Hone
Problems

Python Fraction Operations

This challenge focuses on building a robust Fraction class in Python that can handle fundamental arithmetic operations. Implementing fraction operations is a common exercise that deepens understanding of object-oriented programming, operator overloading, and mathematical concepts like the greatest common divisor (GCD).

Problem Description

Your task is to create a Python class named Fraction that represents a rational number. This class should allow users to perform basic arithmetic operations: addition, subtraction, multiplication, and division. The class should also handle simplification of fractions and ensure that the denominator is always positive.

Key requirements:

  1. Initialization: The Fraction class should be initialized with a numerator and a denominator. Both should be integers.
  2. Representation: Implement __str__ and __repr__ for a user-friendly string representation (e.g., "3/4").
  3. Simplification: All fractions should be automatically simplified to their lowest terms using the greatest common divisor (GCD). The denominator should always be positive. If the numerator is 0, the fraction should be represented as "0/1".
  4. Operator Overloading: Implement the following arithmetic operators:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
  5. Equality: Implement __eq__ to compare two Fraction objects for equality.

Expected behavior:

  • When a Fraction is created, it should be stored in its simplest form.
  • Operations between Fraction objects should result in a new Fraction object, also in its simplest form.
  • Division by zero should raise an appropriate error (e.g., ZeroDivisionError).

Edge cases to consider:

  • Fractions with zero numerator.
  • Fractions with negative numerators or denominators.
  • Operations involving zero.
  • Division by zero.

Examples

Example 1:

f1 = Fraction(1, 2)
f2 = Fraction(1, 3)
result_add = f1 + f2
print(result_add)

Output:

5/6

Explanation: 1/2 + 1/3 = 3/6 + 2/6 = 5/6. The fraction 5/6 is already in its simplest form.

Example 2:

f3 = Fraction(3, 4)
f4 = Fraction(2, 3)
result_mul = f3 * f4
print(result_mul)

Output:

1/2

Explanation: 3/4 * 2/3 = 6/12. The GCD of 6 and 12 is 6. Simplifying 6/12 gives 1/2.

Example 3:

f5 = Fraction(4, -6)
print(f5)

Output:

-2/3

Explanation: The fraction 4/-6 is simplified and the denominator is made positive, resulting in -2/3.

Example 4:

f6 = Fraction(0, 5)
f7 = Fraction(1, 2)
result_div = f6 / f7
print(result_div)

Output:

0/1

Explanation: 0/5 is simplified to 0/1. 0/1 divided by 1/2 is 0. The simplest form of 0 is 0/1.

Example 5:

f8 = Fraction(1, 2)
f9 = Fraction(2, 4)
print(f8 == f9)

Output:

True

Explanation: Although represented differently initially, 1/2 and 2/4 are mathematically equivalent. Both simplify to 1/2.

Constraints

  • Numerators and denominators will be integers.
  • The initial denominator will not be zero.
  • Your solution should handle standard integer ranges in Python.
  • The GCD function used for simplification should be efficient.

Notes

  • You will likely need a helper function to compute the Greatest Common Divisor (GCD). Python's math module has a gcd function you can use, or you can implement your own (e.g., using the Euclidean algorithm).
  • Consider how to handle the sign of the fraction. A common convention is to keep the numerator potentially negative and the denominator always positive.
  • When implementing subtraction, you can leverage the addition operator by negating the second operand.
  • Think about how to represent the fraction 0/x after simplification.
Loading editor...
python