
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the missing value from the given equation a + b = c in Python
Suppose we have one equation in this form: a + b = c, now any one of the terms of a, b or c is missing. We have to find the missing one.
So, if the input is like ? + 4 = 9, then the output will be 5
To solve this, we will follow these steps −
delete all blank spaces from the string and change (+ and = to comma ',')
elements := a list of elements by splitting the string separated by comma
idx := 0
for i in range 0 to size of elements, do
if elements[i] is not numeric, then
idx := i
come out from the loop
if last element is missing, then
return first element + second element
otherwise when second element is missing, then
return last element - first element
otherwise when first element is missing, then
return last element - second element
Example
Let us see the following implementation to get better understanding −
def find_missing(string): string = string.strip().replace(' ', '') string = string.replace('=',',') string = string.replace('+',',') elements = string.split(',') idx = 0 for i in range(len(elements)): if not elements[i].isnumeric(): idx = i break if idx == 2: return int(elements[0]) + int(elements[1]) elif idx == 1: return int(elements[2]) - int(elements[0]) elif idx == 0: return int(elements[2]) - int(elements[1]) print(find_missing('6 + 8 = ?')) print(find_missing('? + 8 = 20')) print(find_missing('5 + ? = 15'))
Input
'6 + 8 = ?' '? + 8 = 20' '5 + ? = 15'
Output
14 12 10
- Related Articles
- Program to find out the value of a given equation in Python
- Find a pair from the given array with maximum nCr value in Python
- Missing even and odd elements from the given arrays in C++
- In each of the following, find the value of $k$ for which the given value is a solution of the given equation: $x^2-x(a+b)+k=0$, $x=a$
- Find a pair from the given array with maximum nCr value in C++
- Program to find expected value of given equation for random numbers in Python
- Find the number of solutions to the given equation in C++
- Program to find the kth missing number from a list of elements in Python
- Find the value of 'a' from the following equation.$-46 + 7a = 87$
- How to find the missing number in a given Array from number 1 to n in Java?
- Write a Python program to find the maximum value from first four rows in a given series
- Find the value of x from the following equation : $5x+7=0$
- Given $4725 = 3^a5^b7^c$, find the value of $2^{-a}3^b 7^c$.
- In each of the following, find the value of $k$ for which the given value is a solution of the given equation: $x^2+3ax+k=0$, $x=-a$
- Find Positive Integer Solution for a Given Equation in C++
