from typing import OrderedDict


def create_results_dict(results_per_test, linked_problems):
    """
    Generates a dictionary used to create HTML and RST tables.

    :param results_per_test : results nested array of objects
    :type results_per_test : list[list[list]]
    :param linked_problems : rst links for supporting pages
    :type linked_problems : list[str]

    :return : tuple(acc_results, time_results, html_links)
               dictionary of accuracy and timing results and
               html links for rending
    :rtype : tuple(dict, dict, list)
    """

    count = 1
    prev_name = ''
    template = '<a target="_blank" href="{0}">{1}</a>'
    acc_results = OrderedDict()
    time_results = OrderedDict()
    html_links = []

    for prob_results, link in zip(results_per_test, linked_problems):
        name = prob_results[0].problem.name
        if name == prev_name:
            count += 1
        else:
            count = 1
        prev_name = name
        prob_name = name + ' ' + str(count)
        url = link.split('<')[1].split('>')[0]
        html_links.append(template.format(url, prob_name))
        acc_results[prob_name] = [result.chi_sq for result in prob_results]
        time_results[prob_name] = [result.runtime for result in prob_results]
    return acc_results, time_results, html_links