Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Cryptarithmetic Puzzle

Challenging Task
• Weight in Assessment – 20%

• Understanding and Planning is more


important before starting to code
Points to Remember while Attempting
Challenging Task
• Read the problem carefully
• Note down the key points
• Plan the solution for the problem with
modules
• Refine the solution for the problem
• Write a detail algorithm for each module in
the perspective of Python
• Implement module by module and check
Sample Problem
• Given a crypt equation of factorization, design
an algorithm and write the Python program to
find the values to be assigned for each letter
in the equation. The factors in the given
equation are prime numbers. The values to be
assigned for the letters are unique for each
letter and from the set {0,1, 2,...9} but 0
cannot be first digit of any number.
Sample Problem Contd...
For example, consider the equation ABCBA =
D*BE*BFFA the task is to determine where A,
B, C, D, E and F which are distinct digits. The
equation with numerical values is 91819 = 7 *
13 * 1009, hence the program should print
{'A':9, 'B':1, 'C':8 , 'D':7,'E':3, 'F':0}. [Hint: use
pprint function for printing dictionary in
sorted order.
• Key points in the problem

• ABCBA = D*BE*BFFA

• A five (ABCBA) digit number is represented as


product of three prime numbers (D, BE, BFFA)

• One of the prime number is one digit (D),


other one is two digit (BE) and the other one
four digit (BFFA)
Solution

• There are two approaches for the problem


– Start from prime numbers and match the pattern
given in the problem

– Work in the perspective of string and check


whether product of the numbers match the
pattern of the equation
• Key points in the problem

• ABCBA = D*BE*BFFA

• In the four digit prime number (BFFA), second


and the third digits are same

• In the resultant number (ABCBA), second and


the fourth digits are same
Level 1 design of algorithm
• Read the arithmetic expression as a string
• Split the expression into sub components
• Identify the letters in each component and their
position
• Generate all one digit, two digit and four digit prime
numbers
• Remove the one digit/two digit/ four digit prime
numbers which do not satisfy the constraints of
equality
Modules required to solve the problem
• Generate prime numbers in a given range of numbers
• Check if a number matches the pattern in the component
of the given equation (In the given example, a four digit
prime number should have second and third digits as
same).
• Check if the numbers in the crypt arithmetic expression
satisfies the constraints as a whole (across the
components (i.e) one digit, two digit, four digit and
resultant value)
• Module to read the expression and divide them
into modules

• Read the expression as a string and use split


function to split it
• Identify the unique letters in the expression and
the position of it in the component in the
expression

• Set may be used to identify unique letters in the


expression
• Form a dictionary for each component with
letters in it as key and list of positions as values
– From the list of unique letters, take one letter at a
time and check the position of it in the component
and add it
• Prepare a list of one digit, two digits and four
digits prime number
• Discard prime numbers that do not have pattern
in the given equation and which do not satisfy
the constraint of uniqueness (i.e.) each letter in
the crypt equation should be different
Approach 2
• In this approach, each unique letter in the
crypt equation and their positions in the
equation are identified
• Then values are assigned one by one for each
letter
• During assignment of value for a letter, values
assigned for other letters are not given to
satisfy the constraint of uniqueness
• Above steps are repeated till values that
satisfy the equation are identified

You might also like