#!/usr/bin/env python
# coding: utf-8

import numpy as np

DATA = np.genfromtxt("data.txt", dtype=int)
SIZE = 4


def count_occurrences (data, pattern):
    '''Find all occurrences of pattern and count them'''
    pattern = np.fromstring (pattern, dtype=int, sep=' ')
    pattern_len = np.shape(pattern)[0]
    data_len = np.shape(data)[0] # data[data_len] is not already allowed to call
    occurrences_amount = 0
    index = 0
    for value in data:
        if value == pattern[0]: # possible start of pattern
            start = index
            if start + pattern_len >= data_len: # if pattern doesn't fit into sequence
                break
            else: # check is it pattern
                is_match = True
                for p in xrange(pattern_len):
                    if data[start + p] != pattern[p]: # it is not
                        is_match = False
                        break
                if is_match:
                    occurrences_amount += 1
        index += 1
    return occurrences_amount


def gen_2d_frequency_matrix(data, size):
    '''Generates 2d frequency matrix'''    
    frequency_matrix = np.empty((size, size))
    for i in xrange(size):
        for j in xrange(size):
            pattern = ' '.join(str(x) for x in [i + 1, j + 1])
            frequency_matrix[i, j] = count_occurrences(data, pattern)
    return frequency_matrix


def gen_3d_frequency_matrix(data, size):
    '''Generates 3d frequency matrix'''    
    frequency_matrix = np.zeros((size ** 2, size))
    import itertools
    patterns = []
    for i, j, k in itertools.product(xrange(size), xrange(size), xrange(size)):
        patterns.append(' '.join(str(i) for i in [i + 1, j + 1, k + 1]))
    s = 0
    for i in xrange(size ** 2):
        for j in xrange(size):
            frequency_matrix[i, j] = count_occurrences(data, patterns[s])
            s += 1
    return frequency_matrix


if __name__ == '__main__':
#    array_string = ' '.join(str(x) for x in DATA)
#    print array_string
    matrix = gen_2d_frequency_matrix(DATA, SIZE)
    print matrix, '\n'
    matrix = gen_3d_frequency_matrix(DATA, SIZE)
    print matrix

@@ -128,17 +128,16 @@
 urrences
- 
 (data, p
@@ -149,19 +149,19 @@
 n):%0A    
-'''
+%22%22%22
 Find all
@@ -198,19 +198,19 @@
 unt them
-'''
+%22%22%22
 %0A    pat
@@ -229,17 +229,16 @@
 omstring
- 
 (pattern
@@ -330,16 +330,17 @@
 data)%5B0%5D
+ 
  # data%5B
@@ -475,16 +475,17 @@
 tern%5B0%5D:
+ 
  # possi
@@ -578,16 +578,17 @@
 ata_len:
+ 
  # if pa
@@ -658,16 +658,17 @@
    else:
+ 
  # check
@@ -813,16 +813,17 @@
 tern%5Bp%5D:
+ 
  # it is
@@ -1094,36 +1094,32 @@
 quency matrix'''
-    
 %0A    frequency_m
@@ -1407,35 +1407,35 @@
 ata, size):%0A    
-'''
+%22%22%22
 Generates 3d fre
@@ -1451,15 +1451,11 @@
 trix
-'''    
+%22%22%22
 %0A   
@@ -1917,16 +1917,20 @@
 ain__':%0A
+    
 #    arr
@@ -1972,16 +1972,20 @@
 n DATA)%0A
+    
 #    pri

Format code