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

import sys
import json
import operator
import math

from itertools import izip


true_path = 'true_hh_salaries.txt'

team_path = 'hh_salaries.txt'

result = { 'score': 1, 'details': 'failed' }

to_error = 0
from_error = 0

to_lines_number = 0
to_total = 0
from_lines_number = 0
from_total = 0

for true_line, team_line in izip(open(true_path), open(team_path)):
    trdata = json.loads(true_line)
    tmdata = json.loads(team_line)

    if trdata['id'] != tmdata['id']:
        raise('Ids mismatch error!')
        break

    if trdata['salary']['to']:
        to_total += 1
        if tmdata['salary']['to']:
            to_lines_number += 1
            trto = float(trdata['salary']['to'])
            tmto = float(tmdata['salary']['to'])
            to_error += abs(min(trto, tmto) / max(trto, tmto) - 1)**2

    if trdata['salary']['from']:
        from_total += 1
        if tmdata['salary']['from']:
            from_lines_number += 1
            trfrom = float(trdata['salary']['from'])
            tmfrom = float(tmdata['salary']['from'])
            from_error += abs(min(trfrom, tmfrom) / max(trfrom, tmfrom) - 1)**2

predicted_to = to_lines_number / float(to_total)
predicted_from = from_lines_number / float(from_total)

if predicted_to >= 0.5 or predicted_from >= 0.5:
    to_error = math.sqrt(to_error / to_lines_number)
    from_error = math.sqrt(from_error / from_lines_number)

    result['score'] = (to_error + from_error) / 2
    result['details'] = 'Average relative error for "to" – %s, "from" – %s. Predicted "to" lines – %s, "from" – %s' % (to_error, from_error, predicted_to, predicted_from)
else:
    result['details'] = 'Not enough to/from lines predicted'

print repr(result)