Todos los desafíos
Intermedio 1 resuelto 100.0% de aceptación

Exact Fraction Arithmetic

Perform fraction operations and return a reduced fraction.

Lo que aprenderás

  • Add, subtract, multiply and divide fractions
  • Reduce results with the GCD
  • Normalize signs and zero

Problema

Ver traducción

Represent a fraction as [numerator, denominator]. Apply operation to first and second: add, subtract, multiply, or divide. For absolute, use only first. For power, raise first to the integer exponent and ignore second. Return lowest terms with a positive denominator. Normalize zero to [0, 1]. All divisions and negative powers in valid inputs are defined.

Implement rational_arithmetic(operation, first, second, exponent).

Input

operation: The operation name.

first: Two integers representing the first fraction.

second: Two integers representing the second fraction; use [0, 1] when unused.

exponent: The exponent for power; use 0 for other operations.

Output

Return [numerator, denominator] in reduced form with a positive denominator.

Restricciones

operation is add, subtract, multiply, divide, absolute, or power. Both fraction components have magnitude at most 100; denominators are nonzero. −6 ≤ exponent ≤ 6. For divide, second has nonzero numerator. For a negative power, first has nonzero numerator. The 0th power is [1, 1], including 0⁰ by this task’s convention. Values fit signed 64-bit.

Ejemplos

Ejemplo 1
Entrada:
operation = "add"
first = [1, 6]
second = [1, 4]
exponent = 0

Salida esperada:
expected = [5, 12]
Entrada:
std::string operation = "add";
std::vector<long long> first = {1, 6};
std::vector<long long> second = {1, 4};
long long exponent = 0;

Salida esperada:
std::vector<long long> expected = {5, 12};
Entrada:
string operation = "add";
long[] first = new long[] {1, 6};
long[] second = new long[] {1, 4};
long exponent = 0;

Salida esperada:
long[] expected = new long[] {5, 12};

Explicación

1/6 + 1/4 = 2/12 + 3/12 = 5/12, already reduced.

Ejemplo 2
Entrada:
operation = "power"
first = [2, 3]
second = [0, 1]
exponent = -2

Salida esperada:
expected = [9, 4]
Entrada:
std::string operation = "power";
std::vector<long long> first = {2, 3};
std::vector<long long> second = {0, 1};
long long exponent = -2;

Salida esperada:
std::vector<long long> expected = {9, 4};
Entrada:
string operation = "power";
long[] first = new long[] {2, 3};
long[] second = new long[] {0, 1};
long exponent = -2;

Salida esperada:
long[] expected = new long[] {9, 4};

Explicación

A negative exponent inverts the fraction: (2/3)⁻² = (3/2)² = 9/4.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Convert each fraction to a common representation and combine the numerators according to the chosen operation.

Pista 2

After the operation, reduce the result by dividing by the greatest common divisor and make the denominator positive.

Pista 3

Watch special cases like zero results, absolute value, and the power rule’s zero and negative exponents.

Complejidad esperada

O(log n) time, O(1) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

# Programador Lenguaje Tiempo Memoria Revisión de IA Solución
1 ННикита Python 1,093 ms - 4/5 Ver código

Aprende de otros

Soluciones de la comunidad

Alerta de spoiler: 1 solución completa abajo Intenta resolverlo tú mismo primero. Igual puedes ver el código de otros desarrolladores y las reseñas de IA. Inicia sesión y resuelve el reto para unirte a la discusión.