Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python


Suppose we have a string s which represents an equation of the form x+y=z. We have to find the minimum number of digits that we need to add into s so the equation becomes true.

So, if the input is like s = '2+6=7', then the output will be 2.

We can turn the equation into "21+6=27" by inserting "1" and "2". So the total number of corrections required is 2.

To solve this, we will follow these steps −

  • split s into parts based on "+" character, put left part into A and right part into rest

  • split rest into parts based on "=" character, put left part into B and right part into C

  • return dp(size of A - 1, size of B - 1, size of C - 1, 0)

  • Define a function dp() . This will take i, j, k, carry

  • if i <= -1 and j <= -1 and k <= -1, then

    • return 0 if carry is same as 0 otherwise 1

  • last1 := (A[i]) if i >= 0 otherwise 0

  • last2 := (B[j]) if j >= 0 otherwise 0

  • last3 := (C[k]) if k >= 0 otherwise 0

  • prefix1 := (A[from index 0 to i + 1]) if i >= 0 otherwise 0

  • prefix2 := (B[from index 0 to j + 1]) if j >= 0 otherwise 0

  • prefix3 := (C[from index 0 to k + 1]) if k >= 0 otherwise 0

  • if i <= -1 and j <= -1, then

    • rhs := prefix3 - carry

    • if rhs <= 0, then

      • return |rhs|

    • if i is same as -1 or j is same as -1, then

      • return size of string rhs

    • otherwise,

      • return False

    • if k <= -1, then

      • return size of str(prefix1 + prefix2 + carry)

    • ans := infinity

    • carry2, lhs := return quotient and remainder dividing(carry + last1 + last2) by 10

    • if lhs is same as last3, then

      • ans := dp(i - 1, j - 1, k - 1, carry2)

    • req := last3 - carry - last2

    • extra_zeros := maximum of 0, -1 - i

    • carry2 := 1 if req < 0 otherwise 0

    • ans := minimum of ans, 1 + extra_zeros + dp(maximum of -1, i, j - 1, k - 1, carry2)

    • req := last3 - carry - last1

    • extra_zeros := maximum of 0, -1 - j

    • carry2 := 1 if req < 0 otherwise 0

    • ans = minimum of (ans, 1 + extra_zeros + dp(i - 1, max(-1, j), k - 1, carry2))

    • carry2, lhs := return quotient and remainder dividing(last1 + last2 + carry) by 10

    • ans := minimum of ans, 1 + dp(i - 1, j - 1, k, carry2)

    • return ans

  • from the main method return dp(size of A – 1, size of B – 1, size of C – 1, 0)

Example 

Let us see the following implementation to get better understanding −

 Live Demo

class Solution:
   def solve(self, s):
      A, rest = s.split("+")
      B, C = rest.split("=")
      def dp(i, j, k, carry):
         if i <= -1 and j <= -1 and k <= -1:
            return 0 if carry == 0 else 1
         last1 = int(A[i]) if i >= 0 else 0
         last2 = int(B[j]) if j >= 0 else 0
         last3 = int(C[k]) if k >= 0 else 0
         prefix1 = int(A[: i + 1]) if i >= 0 else 0
         prefix2 = int(B[: j + 1]) if j >= 0 else 0
         prefix3 = int(C[: k + 1]) if k >= 0 else 0
         if i <= -1 and j <= -1:
            rhs = prefix3 - carry
            if rhs <= 0:
               return abs(rhs)
            if i == -1 or j == -1:
               return len(str(rhs))
            else:
               assert False
         if k <= -1:
            return len(str(prefix1 + prefix2 + carry))
         ans = float("inf")
         carry2, lhs = divmod(carry + last1 + last2, 10)
         if lhs == last3:
            ans = dp(i - 1, j - 1, k - 1, carry2)
         req = last3 - carry - last2
         extra_zeros = max(0, -1 - i)
         carry2 = 1 if req < 0 else 0
         ans = min(ans, 1 + extra_zeros + dp(max(-1, i), j - 1, k - 1, carry2))
         req = last3 - carry - last1
         extra_zeros = max(0, -1 - j)
         carry2 = 1 if req < 0 else 0
         ans = min(ans, 1 + extra_zeros + dp(i - 1, max(-1, j), k - 1, carry2))
         carry2, lhs = divmod(last1 + last2 + carry, 10)
         ans = min(ans, 1 + dp(i - 1, j - 1, k, carry2))
         return ans
      return dp(len(A) - 1, len(B) - 1, len(C) - 1, 0)

ob = Solution()
print (ob.solve('2+6=7'))

Input

'2+6=7'

Output

2

Updated on: 23-Dec-2020

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements