Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Cryptcipher

program code

Uploaded by

athirayanpericse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Cryptcipher

program code

Uploaded by

athirayanpericse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Python program to implement

# Cryptarithmetic Puzzle Solver

# Function to remove preceding zeroes


def removeZeroes(a):
return a.lstrip('0') or '0'

# function change character string


# to digit string
def charToDigit(a, digits):
return ''.join(digits[ord(c) - ord('a')] for c in a)

# function to find sum of numbers


# in the form of strings
def findSum(a, b):
if len(a) > len(b):
a, b = b, a

str_res = []
carry = 0

for i in range(len(a)):
sum_digit = int(a[-1 - i]) + int(b[-1 - i]) + carry
str_res.append(str(sum_digit % 10))
carry = sum_digit // 10

for i in range(len(a), len(b)):


sum_digit = int(b[-1 - i]) + carry
str_res.append(str(sum_digit % 10))
carry = sum_digit // 10

if carry:
str_res.append(str(carry))

return removeZeroes(''.join(reversed(str_res)))

# function to check if puzzle is solved


def isSolved(a, b, sum, digits):
x = charToDigit(a, digits)
y = charToDigit(b, digits)
z = charToDigit(sum, digits)

res = findSum(x, y)
z = removeZeroes(z)

return z == res

# Function to solve Cryptarithmetic


# Puzzle using backtracking
def cryptarithmeticSolver(ind, digits, characters, a, b, sum):
if ind == 26:
return isSolved(a, b, sum, digits)

if digits[ind] != '+':
return cryptarithmeticSolver(ind + 1, digits, characters, a, b, sum)

# Try assigning digits only for characters that haven't been assigned yet
for i in range(10):
if characters[i] == 0:
characters[i] = 1
digits[ind] = str(i)
if cryptarithmeticSolver(ind + 1, digits, characters, a, b, sum):
return True
digits[ind] = '+'
characters[i] = 0

return False

# Function to solve Cryptarithmetic Puzzle


def solvePuzzle(a, b, sum):
digits = ['-' for _ in range(26)]
characters = [0] * 10

for c in a + b + sum:
digits[ord(c) - ord('a')] = '+'

if cryptarithmeticSolver(0, digits, characters, a, b, sum):


x = charToDigit(a, digits)
y = charToDigit(b, digits)
res = charToDigit(sum, digits)
return [x, y, res]
else:
return ["-1"]

# Driver Code
a = "s"
b = "p"
sum = "f"
ans = solvePuzzle(a, b, sum)
print(" ".join(ans))

You might also like