text_plain_output_1.png
import json

import os # To walk through the data files provided

import re # Regular expressions

testDirectory = '/kaggle/input/abstraction-and-reasoning-challenge/test/'
trainingDirectory = '/kaggle/input/abstraction-and-reasoning-challenge/training/'
evaluationDirectory = '/kaggle/input/abstraction-and-reasoning-challenge/training/'

def readTaskFile(filename):
    f = open(filename, 'r')
    data = json.loads(f.read())
    data['id'] = re.sub('(.*/)|(\\.json)', '', filename)
    f.close()
    return data

filename = testDirectory + '19bb5feb.json'
readTaskFile(filename)

def getGridSizeComparison(filename):
    data = readTaskFile(filename)
    trainSection = data['train']
    ident = data['id']
    numTrain = len(trainSection)
    result = {}
    for i in range(numTrain):
        trainCase = trainSection[i]
        trainCaseInput = trainCase['input']
        trainCaseOutput = trainCase['output']
        sameY = len(trainCaseInput) == len(trainCaseOutput)
        sameX = len(trainCaseInput[0]) == len(trainCaseOutput[0])
        result[ident + '_train_' + str(i)] = sameX and sameY
    return result

filename = testDirectory + '19bb5feb.json'
getGridSizeComparison(filename)

def getResults(directory, f):
    results = {}
    for _, _, filenames in os.walk(directory):
        for filename in filenames:
            results.update(f(directory + filename))
    return results

results = getResults(trainingDirectory, getGridSizeComparison)

count = 0
for key, value in results.items():
    if value:
        count += 1
print('Proportion of training examples with the same grid size: ' + str(round(count / len(results), 2)))