In Artificial Intelligence you will encounter this puzzle. Given below is a crypt-arithmetic problem solved.
Example :
SEND
+ MORE
-------
= MONEY
-------
CP
+ IS
+ FUN
--------
= TRUE
-------
All the different alphabets range from 0-9 and each and every alphabet should have a unique value. All the same letters have to be the same number. For deeper analysis, every step is broken down below. b3, b2, b1 are the respective borrowers ( b1,b2,b3=10/0 ) on their corresponding digits.
b3 b2 b1 C O U N T - C O I N --------------- S N U B ---------------
Solution : This can be solved by understanding the following:
Equations: b3=10 -- [0] b2 =10 -- [1] 10-b1/10+N - I = U -- [2] b1+ T - N = B -- [3] N =U-1 -- [4]
We can consider two cases, b1 = 10 and b1 = 0.
Case-1:
b1 =10 10 +T -N =B which means T is less than N. Since 10 -1 =9, S =9 From equation [2] and[4] we see that 10-10/10 +(U-1) -I =U this implies I =8
Now we check by using various combinations of U ,N –>{(7,6)…..(3,2) } and check other values using hit and trial from remaining elements from 0-9.
We check for U =7,N =6 which means 10 -1 + 6 -8 must be 7, which satisfies and we proceed further 1076T – 1086 ——- 967B 10 +T-6=B T+4=B T,B has no value for remaining elements.
Solution 10652 - 1085 -------- 9567 --------
Solution : Since b1 =10 , this means we subtract 1 from U 15 -1 -I =6 I =8 10 +T -5 =B T+5 =B, and T =2, B= 7.
All the values are unique, so this is correct solution
Case-2:
b1=0 T -N=B b2 =10 10+N - I = U N =U-1 --[4] which implies I =9, which is invalid as S =9
Hence, b1=0 is invalid and there exists no solution for this case. Given below is the python code for same
import re
def solve(q):
try:
n = next(i for i in q if i.isalpha()) # Check if q has alphabetic characters
except StopIteration:
# Use eval to evaluate the parsed string
# Use sub to replace strings with regular expressions
# In the regular expression, ab+ will match 'a' followed by any non-zero number of 'b's
# [abv] is a set of characters, which will match 'a', 'b', or 'v'
# ^[0-9] means "any digit, at the start of the string"; [^0-9] means "anything except a digit"
return q if eval(re.sub(r'(^|[^0-9])0+([1-9]+)', r'\1\2', q)) else False
else:
for i in (str(i) for i in range(10) if str(i) not in q):
r = solve(q.replace(n, str(i))) # Replace character with number
if r:
return r
return False
# Driver code
if __name__ == "__main__":
query = "COUNT - COIN == SNUB"
r = solve(query)
print(r) if r else print("No solution found.")
Output:
03185 - 0368 == 2817
Note:
1. We cannot solve this by
C O I N
+ S N U B
---------------
C O U N T
---------------
2. All distinct alphabets should have unique value (0-9).
