Skip to content

Interleaving

Learning Objectives

At the end of this sub-unit, students should

  • 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.

  1. Compute the right operand and store into a tuple \(T_1\).
  2. 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.
  3. Given \(T_1\) and \(T_2\) such that the elements on the same index match, compute the sum of the multiplication.
def isbn10(n):
  # Compute Right Operand to t1




  # Compute Left Operand to t2




  # Remaining Sum of Multiplication
  
  
  
  
def isbn10(n):
  # Compute Right Operand to t1
  t1, i, m = (), 1, n
  while m > 0:
    t1 = t1 + (i,)
    i, m = i + 1, m // 10
  # Compute Left Operand to t2




  # Remaining Sum of Multiplication
  
  
  
  
def isbn10(n):
  # Compute Right Operand to t1




  # Compute Left Operand to t2
  t2, m = (), n
  while m > 0:
    t2 = t2 + (m % 10,)
    m = m // 10
  # Remaining Sum of Multiplication
  
  
  
  
def isbn10(n):
  # Compute Right Operand to t1




  # Compute Left Operand to t2




  # Remaining Sum of Multiplication
  mul = 0
  for i in range(len(t1)): # or len(t2)
    mul = mul + (t1[i] * t2[i])
  return mul % 11 == 0
def isbn10(n):
  # Compute Right Operand to t1
  t1, i, m = (), 1, n
  while m > 0:
    t1 = t1 + (i,)
    i, m = i + 1, m // 10
  # Compute Left Operand to t2
  t2, m = (), n
  while m > 0:
    t2 = t2 + (m % 10,)
    m = m // 10
  # Remaining Sum of Multiplication
  mul = 0
  for i in range(len(t1)): # or len(t2)
    mul = mul + (t1[i] * t2[i])
  return mul % 11 == 0