appreciate the use of tuple to avoid interleaving.
ISBN-10
Recap the ISBN-10 problem we did before.
We used the idea of interleaving to combine the solution from the two patterns.
In this short sub-unit, we will use tuple to connect two components instead of performing an interleaving.
Hopefully, using this idea, our pattern bank will be even simpler.
The rest of the problem solving becomes more of an art of problem solving.
The idea is to decompose the problem into the following.
Compute the right operand and store into a tuple \(T_1\).
Compute the left operand and store into a tuple \(T_2\).
We need to ensure that \(T_1\) and \(T_2\) has the same number of elements.
We need to ensure elements in \(T_1\) and \(T_2\) match for the given index.
Given \(T_1\) and \(T_2\) such that the elements on the same index match, compute the sum of the multiplication.
defisbn10(n):# Compute Right Operand to t1t1,i,m=(),1,nwhilem>0:t1=t1+(i,)i,m=i+1,m//10# Compute Left Operand to t2# Remaining Sum of Multiplication
defisbn10(n):# Compute Right Operand to t1# Compute Left Operand to t2# Remaining Sum of Multiplicationmul=0foriinrange(len(t1)):# or len(t2)mul=mul+(t1[i]*t2[i])returnmul%11==0
defisbn10(n):# Compute Right Operand to t1t1,i,m=(),1,nwhilem>0:t1=t1+(i,)i,m=i+1,m//10# Compute Left Operand to t2t2,m=(),nwhilem>0:t2=t2+(m%10,)m=m//10# Remaining Sum of Multiplicationmul=0foriinrange(len(t1)):# or len(t2)mul=mul+(t1[i]*t2[i])returnmul%11==0