Hone logo
Hone
Problems

Fraction Arithmetic in Python

This challenge asks you to implement a Fraction class in Python that supports basic arithmetic operations: addition, subtraction, multiplication, and division. Working with fractions directly can be useful in various mathematical and scientific applications, avoiding floating-point inaccuracies that can arise when using decimals.

Problem Description

You are to create a Fraction class that represents a rational number (a fraction) with a numerator and a denominator. The class should:

  1. Initialization: Accept a numerator and denominator as arguments in the constructor. The denominator should not be zero. If the denominator is zero, raise a ValueError. The fraction should be reduced to its simplest form upon initialization (i.e., the greatest common divisor (GCD) of the numerator and denominator should be 1).
  2. __str__ method: Return a string representation of the fraction in the format "numerator/denominator".
  3. __repr__ method: Return a string representation of the fraction in the format "Fraction(numerator, denominator)".
  4. Arithmetic Operations: Implement the following methods:
    • __add__(self, other): Return a new Fraction object representing the sum of the current fraction and other. other can be another Fraction object or an integer. If other is an integer, treat it as a fraction with a denominator of 1.
    • __sub__(self, other): Return a new Fraction object representing the difference of the current fraction and other. other can be another Fraction object or an integer.
    • __mul__(self, other): Return a new Fraction object representing the product of the current fraction and other. other can be another Fraction object or an integer.
    • __truediv__(self, other): Return a new Fraction object representing the quotient of the current fraction and other. other can be another Fraction object or an integer. Raise a ZeroDivisionError if other is zero.
  5. GCD Calculation: Include a helper function gcd(a, b) to calculate the greatest common divisor of two integers. This function should be private (prefixed with an underscore).

Examples

Example 1:

Input:
fraction1 = Fraction(2, 4)
fraction2 = Fraction(1, 2)
result = fraction1 + fraction2
print(result)
Output:
1/2
Explanation:
2/4 is reduced to 1/2. 1/2 + 1/2 = 1. The result is represented as 1/2.

Example 2:

Input:
fraction = Fraction(3, 5)
result = fraction * 2
print(result)
Output:
6/5
Explanation:
3/5 * 2 = 6/5.

Example 3:

Input:
fraction1 = Fraction(1, 2)
fraction2 = Fraction(1, 4)
result = fraction1 / fraction2
print(result)
Output:
2/1
Explanation:
1/2 / 1/4 = (1/2) * (4/1) = 4/2 = 2. The result is represented as 2/1.

Example 4:

Input:
fraction = Fraction(5, 10)
print(fraction)
Output:
1/2
Explanation:
5/10 is reduced to 1/2.

Constraints

  • The numerator and denominator will be integers.
  • The denominator will not be zero during initialization.
  • The GCD function should handle positive integers.
  • All fractions should be reduced to their simplest form.
  • The class should handle integer inputs for arithmetic operations correctly.
  • Division by zero should raise a ZeroDivisionError.

Notes

  • Consider using the math.gcd() function from the math module for calculating the greatest common divisor, or implement your own gcd function.
  • Remember to reduce fractions to their simplest form after each arithmetic operation.
  • Pay close attention to the expected string representations for __str__ and __repr__.
  • Think about how to handle the case where other is an integer in the arithmetic operations. You can treat it as a fraction with a denominator of 1.
Loading editor...
python