import ast


def print_price_table(soup):
    """
    Print the price table for "ANA株主優待券（新券）"

    Note that original_price_table[0] is ['週', '価格', '初日の平均価格', '最終日の平均価格', '価格']
    original_price_table[0][0]: 週の始まりの日（日曜日）
    original_price_table[0][1]: 週全体の安値／高値
    original_price_table[0][2]: 初日の平均価格
    original_price_table[0][3]: 最終日の平均価格
    original_price_table[0][4]: 週全体の安値／高値

    See this page for details.
    https://developers.google.com/chart/interactive/docs/gallery/candlestickchart
    """

    chart_all0_script_element = soup.select(r'#pageSection > script:nth-of-type(2)')
    chart_all0_script_text = chart_all0_script_element[0].getText()
    price_table_text = chart_all0_script_text               \
        .split('google.visualization.arrayToDataTable(')[1] \
        .split(')')[0]
    original_price_table = ast.literal_eval(price_table_text)

    def extract_value(item):
        """Extract values from original item in original_price_table"""
        average_price = (item[1] + item[4]) / 2.0     # Take average of the row price and the high price
        return [item[0], average_price]

    price_table = list(map(extract_value, original_price_table[1:]))
    print(price_table)