from bs4 import BeautifulSoup
import requests


def extract_answer_key(url: str) -> dict:
    """
    Scrapes the answer key from the AOPS answer key website.

        Parameters: 
            url (str): URL of the answer key website. Should be of the form 
            https://artofproblemsolving.com/wiki/index.php/{year}_{exam}_Answer_Key
        
        Returns:
            answers (dict): Dictionary of problem number - answer pairs for all 
            problems of the exam
        
    >>> URL = r'https://artofproblemsolving.com/wiki/index.php/2015_AMC_10A_Answer_Key'
    >>> extract_answer_key(URL)
    {1: 'C', 2: 'D', 3: 'D', 4: 'B', 5: 'E', 6: 'B', 7: 'B', 8: 'B', 9: 'D', 10: 'C', 11: 'C',
     12: 'C', 13: 'C', 14: 'C', 15: 'B', 16: 'B', 17: 'D', 18: 'E', 19: 'D', 20: 'B', 21: 'C', 
     22: 'A', 23: 'C', 24: 'B', 25: 'A'}

    """

    answers = []
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')

    for li in soup.find_all('li'):
        for descendant in li:
            answers.append(descendant)

    indices = [i for i in range(1, len(answers) + 1)]

    return dict(zip(indices, answers))