title: Python - highlight pattern inside a matrix
body: <p>I have a simple exam to pass where I have to write a program where I create a binary matrix and a pattern and I have to find the pattern inside the matrix and to highlight it if found. I can't use any external library - specially numpy.</p>
  <p>What I have atm are just some highlighted numbers that don't match my pattern. I don't know how to continue. Can someone help me please?</p>
  <p>My code is this:</p>
  <p>pattern.py:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code># to create a matrix from an array of arrays
  def create(n, m, data):
      matrix = []
      for i in range(n): #n=rows number
          row = []
          for j in range(m): #m=columns number
              if data[j] not in matrix:
                  row.append(data[m * i + j])
          matrix.append(row)
      return matrix
  
  
  # to colour the elements in 1 inside the pattern
  def colora(element):
      if element == 1:
          element = '\u001b[36m1'
      elif element == 0:
          element = '\u001b[00m0'
      return element
  
  # my pattern forms an I vertically - I should also find it if horizontal
  def patternI():
      array = [1, 0, 0, 1, 0, 0, 1, 0, 0]
      pattern = create(3, 3, array)
      for i in range(3):
          for j in range(3):
              col_element = colora(pattern[i][j])
              print(col_element, end=" ")
              # print(pattern[i][j], end=" ")
          print()
      return pattern</code></pre>
  </div>
  </div>
  </p>
  <p>main.py:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import patterns
  import ricerca
  
  # to create and print the matrix
  def matrix():
      m = 5
      n = 5
      array = [0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0]
      matrix = patterns.create(n, m, array)
      for i in range(n):
          for j in range(m):
              print(matrix[i][j], end=" ")
          print()
      return matrix
  
  
  
  def main():
      print("pattern:")
      pattern = patterns.patternI()
      print("\r")
  
      print("matrix:")
      matrice = matrix()
      print("\r")
  
      print("risult:")
      newArray=ricerca.ricerca(pattern, matrice)
  
  
  
  
  main()</code></pre>
  </div>
  </div>
  </p>
  <p>ricerca.py:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import patterns
  
  # to search the pattern (m_piccola) inside the (m_grande)
  def ricerca(m_piccola, m_grande) :
      daricordare = [] #an array to fill with the position of the matrix which combines a little with the pattern
      for jR in range(len(m_piccola) - 1) :
          for iR in range(len(m_grande)) :
              for iC in range(len(m_grande[0])) :
                  for jC in range(len(m_piccola[jR])) :
                      if m_piccola[jR][jC] == 0 :
                          jC += 1
                          break
                      if m_piccola[jR][jC] == 1 :
                          if m_grande[iR][iC] == m_piccola[jR][jC] :
                              daricordare.append((iR, iC))
                          else :
                              iC += 1
      data = coloraQuelliDelPattern(daricordare, m_grande)
      for i in range(5):
          for j in range(5):
              print(data[i][j], end = " ")
          print()
      return data
  
  
  # to highlight the numbers in the array which are inside "daricordare"
  def coloraQuelliDelPattern(daricordare, array) :
  
      for i in range(len(daricordare) - 2) :
          a = daricordare[i]
          b = daricordare[i + 1]
          c = daricordare[i + 2]
          if b[1] == a[1] + 1  and c[1]==a[1] + 2:
              array[a[0]][a[1]] = "\u001b[36m1"
              array[b[0]][b[1]] = "\u001b[36m1"
              array[c[0]][c[1]] = "\u001b[36m1"
          if b[1] != a[1] + 1 or c[1] != a[1] + 2 :
              array[a[0]][a[1]] = "\u001b[00m1"
              array[b[0]][b[1]] = "\u001b[00m1"
              array[c[0]][c[1]] = "\u001b[00m1"
  
      return array</code></pre>
  </div>
  </div>
  </p>