Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.
Example 1:
Input: expression = "-1/2+1/2" Output: "0/1"
Example 2:
Input: expression = "-1/2+1/2+1/3" Output: "1/3"
Example 3:
Input: expression = "1/3-1/2" Output: "-1/6"
Constraints:
'0' to '9', '/', '+' and '-'. So does the output.±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.[1, 10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.[1, 10].When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves directly performing the fraction operations as they appear in the input string. We process the fractions one by one, combining each new fraction with the accumulated result from the previous calculations. At the end, we simplify the final fraction to its lowest terms.
Here's how the algorithm would work step-by-step:
def fraction_addition_subtraction(expression):
current_numerator = 0
current_denominator = 1
index = 0
while index < len(expression):
sign = 1
if index > 0:
if expression[index] == '+':
index += 1
elif expression[index] == '-':
sign = -1
index += 1
numerator_start = index
while index < len(expression) and expression[index] != '/':
index += 1
new_numerator = int(expression[numerator_start:index])
index += 1
denominator_start = index
while index < len(expression) and expression[index] not in ['+', '-']:
index += 1
if index == len(expression):
break
new_denominator = int(expression[denominator_start:index])
# Find common denominator and update numerators
common_denominator = current_denominator * new_denominator
current_numerator = current_numerator * new_denominator + sign * new_numerator * current_denominator
current_denominator = common_denominator
# Simplify the final fraction
greatest_common_divisor = gcd(abs(current_numerator), current_denominator)
current_numerator //= greatest_common_divisor
current_denominator //= greatest_common_divisor
return str(current_numerator) + '/' + str(current_denominator)
def gcd(number_a, number_b):
#Euclidean algorithm for greatest common divisor
while(number_b):
number_a, number_b = number_b, number_a % number_b
return number_aThe key is to process the fractions one by one, keeping track of the current result. We simplify the problem by focusing on adding or subtracting a single fraction at each step and then reducing the final result to its simplest form.
Here's how the algorithm would work step-by-step:
def fraction_addition_and_subtraction(expression):
current_numerator = 0
current_denominator = 1
index = 0
while index < len(expression):
sign = 1
if index > 0:
if expression[index] == '+':
index += 1
elif expression[index] == '-':
sign = -1
index += 1
numerator = 0
while index < len(expression) and expression[index].isdigit():
numerator = numerator * 10 + int(expression[index])
index += 1
index += 1
denominator = 0
while index < len(expression) and expression[index].isdigit():
denominator = denominator * 10 + int(expression[index])
index += 1
# Find the common denominator for addition/subtraction.
new_numerator = current_numerator * denominator + sign * numerator * current_denominator
new_denominator = current_denominator * denominator
# Simplify the resulting fraction.
greatest_common_divisor = gcd(abs(new_numerator), new_denominator)
current_numerator = new_numerator // greatest_common_divisor
current_denominator = new_denominator // greatest_common_divisor
return str(current_numerator) + '/' + str(current_denominator)
def gcd(numerator_value, denominator_value):
# Euclidean algorithm to find the greatest common divisor.
while denominator_value:
numerator_value, denominator_value = denominator_value, numerator_value % denominator_value
return numerator_value| Case | How to Handle |
|---|---|
| Empty input string | Return "0/1" if the input string is empty as there is nothing to add or subtract. |
| Input string contains only whitespace | Return "0/1" if the input string contains only whitespace after trimming. |
| Input string starts with '/' | The fraction string should be well-formed, so return an error or "0/1" depending on requirements if it starts with a division. |
| Integer overflow in numerator or denominator | Use long type for numerator and denominator calculations to prevent overflow, and check after calculations if overflow occured. |
| Zero denominator in an intermediate fraction | Handle the case of a zero denominator appropriately, either by throwing an error or returning a special value, based on problem definition. |
| Consecutive operators (e.g., '1/2++1/3') | Consider such inputs as invalid, and return an error or "0/1". |
| Missing numerator or denominator | Consider this malformed input, and return an error or "0/1" |
| Input string contains non-numeric characters in numerator/denominator | Consider the input as invalid, and return an error or "0/1". |