Coder Social home page Coder Social logo

19ai405expno8's Introduction

ExpNo 8 : Solve Cryptarithmetic Problem,a CSP (Constraint Satisfaction Problem) using Python

Name: LINGESWARAN K

Register Number: 212222110022

๐“ ๐… ๐“ข

Aim:

To solve Cryptarithmetic Problem,a CSP (Constraint Satisfaction Problem) using Python

Procedure:

Input and Output
Input: This algorithm will take three words.
B A S E
B A L L
----------
G A M E S

Output: It will show which letter holds which number from 0 โ€“ 9. For this case it is like this.

          B A S E                         2 4 6 1
          B A L L                         2 4 5 5
         ---------                       ---------
        G A M E S                       0 4 9 1 6

Algorithm For this problem, we will define a node, which contains a letter and its corresponding values.

isValid(nodeList, count, word1, word2, word3)

Input โˆ’ A list of nodes, the number of elements in the node list and three words.

Output โˆ’ True if the sum of the value for word1 and word2 is same as word3 value.

Begin
m := 1
for each letter i from right to left of word1, do
ch := word1[i]
for all elements j in the nodeList, do
if nodeList[j].letter = ch, then
break
done
val1 := val1 + (m * nodeList[j].value)
m := m * 10
done

m := 1
for each letter i from right to left of word2, do
ch := word2[i]
for all elements j in the nodeList, do
if nodeList[j].letter = ch, then
break
done

  val2 := val2 + (m * nodeList[j].value)
  m := m * 10

done

m := 1
for each letter i from right to left of word3, do
ch := word3[i]
for all elements j in the nodeList, do
if nodeList[j].letter = ch, then
break
done

  val3 := val3 + (m * nodeList[j].value)
  m := m * 10

done

if val3 = (val1 + val2), then
return true
return false
End

Program

from itertools import permutations

def solve_cryptarithmetic():
    for perm in permutations(range(10), 8):
        S, E, N, D, M, O, R, Y = perm

        # Check for leading zeros
        if S == 0 or M == 0:
            continue

        # Check the equation constraints
        SEND = 1000 * S + 100 * E + 10 * N + D
        MORE = 1000 * M + 100 * O + 10 * R + E
        MONEY = 10000 * M + 1000 * O + 100 * N + 10 * E + Y

        if SEND + MORE == MONEY:
            return SEND, MORE, MONEY

    return None

solution = solve_cryptarithmetic()

if solution:
    SEND, MORE, MONEY = solution
    print(f'SEND = {SEND}')
    print(f'MORE = {MORE}')
    print(f'MONEY = {MONEY}')
else:
    print("No solution found.")

Sample Input and Output:

SEND = 9567
MORE = 1085

MONEY = 10652

Result:

Thus the Cryptarithmetic Problem was solved using Python successfully

19ai405expno8's People

Contributors

natsaravanan avatar lingeswaran04 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.