# ------------------------------------------------------------
# CLI runner for year/question exercises with dynamic dispatch.
# Run like:  python <script_name>.py 2023 4
# This will call a function named q4_2023() if it exists.
# ------------------------------------------------------------

import sys
import math

# Map of (year, question) -> custom title line.
# If a pair is present here, its custom text is printed instead of the default prompt.
CUSTOM_TITLES = {
}

def print_question(question, year):
    """
    Print a standardized header for a given year/question.
    Note: This function intentionally checks CUSTOM_TITLES to allow per-question overrides.
    """
    print()
    print(f"--------{year}--Q{question}--------")
    if (year, question) in CUSTOM_TITLES:
        print(CUSTOM_TITLES[(year, question)])
    else:
        print("Qu'est-ce que ce programme affiche ?")
    print()
    print("Réponse attendue:")

# -------------------------------------------------------------------
# BEGIN QUESTION IMPLEMENTATIONS (do not edit anything in this region)
# -------------------------------------------------------------------

def q1_2023():

    x = 2
    y = -3

    if y or x:
        y = y - 1
        x = x + 1

    if y < 0 or x < 0:
        y = y + 2
        x = x + 2

    z = x and y

    if z:
        print(x * z)
    else:
        print(-x * y)

    return None

''' Answer:
    -10
'''

def q2_2023():

    my_string = "Avatar-The-way-of-water"
    s = list(my_string) # converts string to a list of characters
    t = s[-12::-3] if len(s) % 2 else s[-12::3]
    print(t)

    return None
''' Answer:
    ['w', 'h', 'r', 'a']
'''

def q3_2023():

    t = [0, False, True, "empty"]
    x = True == '1'
    y = t[1] or t[-1]
    z  = len(t[3]) > False < True
    print(x or y or z)

    return None

    # print(x, y, z)
''' Answer:
    empty
'''

def q4_2023():

    s1 = [x for x in range(1,10)]
    s2 = [x for x in range(5,15)]
    s3 = [s1.index(x) if x in s1 else 0 for x in s2]
    print(s3[3])

    return None
''' Answer:
    7
'''

def q5_2023():
    i = 37
    while i > 1:
        if i % 2:
            i  = i // 2
        else:
            print("*")
            i = i + 1
    return None
''' Answer:
    3 (three stars are printed)
'''

def q6_2023():

    s = list("ABcDefg HiJk") # converts string to a list of characters
    t = s[-1::-1]
    for c in t:
        if c == 'c':
            break
        elif c == c.lower():
            continue
        print(c, end = ',')

    print()
    return None
''' Answer:
    J,H,D,
'''

def q7_2023():

    # Function max(my_list) returns the maximum element in a list my_list

    t = []
    for i in range(10, 20):
        for j in range(2, i):
            if i % j == 0:
                t.append(j)
                break
    print(len(t), end = ",")
    print(max(t))

    return None
''' Answer:
   6,3
'''

def q8_2023():

    s = [2, -10, 6, 2, 9, 9, 5, 18, 10, -16]
    b = 0
    for v in s:
        if (v if v > 0 else -v) > (b if b > 0 else -b):
            b = v
    print(b)

    return None
''' Answer:
    18 (max absolute value)
'''

def q9_2023():
    s = [x for x in range(1,6)]
    s = s[-3:] + s[:-3]
    s.reverse()
    print(s)

    return None
''' Answer:
    [2, 1, 5, 4, 3]
'''

def q1_2022():
    s = 'La vie est belle'
    t = s[::4] # slicing over strings is identical to slicing over lists
    t = t + s[-4::] + s[1]
    print(t[2::2])
    return None
''' Answer:
    sela
'''

def q2_2022():
    n = 2000
    cnt = 0

    while n > 1:
        if n % 25:
            n = n // 2 if n % 4 else n // 4
        else:
            n = n // 25
            continue
        cnt = cnt + 1

    print(cnt)
    return None
''' Answer:
    4
'''

def q3_2022():
    s = list('1011010101')
    key = list('010')
    v = []
    for i in range(len(s)):
        if i > (len(s) - len(key)):
            break
        if s[i:i+len(key)] == key:
            v = s[i:]
            break
    print(v)
    return None
''' Answer:
    ['0', '1', '0', '1', '0', '1']
'''

def q4_2022():
    n = 10
    a = []

    for i in range(n):
        a.append([])

    cnt = 0
    for i in range(n-1, -1, -1):
        for j in range(n):
            a[i].append(i + j)
        if a[i][j] > cnt:
            cnt = a[i][j]
    print(cnt)

    return None
''' Answer:
    18
'''

def q5_2022():
    seq = [9, 38, 7, 50, 22, 2]
    seq = [seq[(i + 3) % len(seq)] for i, x in enumerate(seq)]
    print(seq)
    return None
''' Answer:
    [50, 22, 2, 9, 38, 7]
'''

def q1_2021():
    x = [True, False]

    for i in range(1,5):
        if i % 2:
            x.append(True)
        else:
            x.append(False)
    print(x[3])
    print(x[1] or x[4])
    print(x[0] and not x[5])
    return None
''' Answer:
    False
    True
    True
'''

def q2_2021():
    s = list('Abracadabra')
    if (s[0] == s[3]):
        print(s[0:3])
    elif (s[1]==s[-2]):
        print(s[:])
    elif (s[-1] == s[-6]):
        print(s[-1::-4])
    else:
        print(s[::-1])
    return None
''' Answer:
    ['a', 'd', 'r']
'''

def q3_2021():
    x = 2
    y = 5
    z = x - y//x
    w = y + x/y
    r = z or w
    print(r)
    return None
''' Answer:
    5.4
'''

def q4_2021():
    x = 8
    y = 2**2

    z = math.log2(x)**math.log2(y)
    z = -z
    z = z % y
    print(z)
    return None
''' Answer:
    3.0
'''

def q5_2021():
    N = 15
    cnt = 0
    for i in range (1, int(N/3) + 1, 2):
        for j in range (int(N/3), int(2*N/3) + 1, 2):
            for k in range(int(2*N/3), N + 1, 2):
                cnt = cnt + 1
    print(cnt)
    return None
''' Answer:
    27
'''

def q6_2021():
    s = list('1001')
    l = len(s)
    r = sum([int(s[l - i - 1]) * 2**i for i in range(l)])
    print(r)
''' Answer:
    9
'''

def q7_2021():
    s = list('0001011110')
    n = len(s)
    i = -1
    while n:
        i = i + 1
        if s[i] == '1':
            n = n - 1
            continue
        elif s[i] == s[i+1]:
            n = n - 2
        else:
            n = n - 1
        print(s[i], end = ':')
    print()
    return
''' Answer:
    0:0:0:0:
'''

def q8_2021():
    '''
    La methode index() renvoie l'index d'un element dans une liste.
    La methode count() renvoie le nombre d'occurrences d'un element dans une liste.
    '''

    l = [[3], 3, [3, 3, 3]]
    print(l.index(3))
    print(l.count(3))
    return None
''' Answer:
    1
    1
'''

def q9_2021():
    a = -2000
    b = 4001
    c = 6002

    c, b, a = c // a, b or a, c % a

    print(a, b, c)
    return None
''' Answer:
    -1998 4001 -4
'''

# -------------------------------------------------------------------
# END QUESTION IMPLEMENTATIONS
# -------------------------------------------------------------------

def main():
    """
    Parse CLI arguments (year, question), look up a function named q<question>_<year>,
    print a standardized header, and execute the function if present.
    """
    # Expect exactly two positional args: <year> and <question_number>
    if len(sys.argv) != 3:
        # Keep usage minimal and explicit for quick reference.
        print("Usage: python <script_name>.py <year> <question_number>")
        sys.exit(1)

    # Convert inputs to integers and fail fast on invalid input.
    try:
        year = int(sys.argv[1])
        question = int(sys.argv[2])
    except ValueError:
        print("Both year and question number must be integers.")
        sys.exit(1)

    # Dynamic dispatch target: e.g., q5_2023 for year=2023, question=5
    func_name = f"q{question}_{year}"

    # Look up the function by name in the global namespace and call it if found.
    if func_name in globals():
        print_question(question, year)
        globals()[func_name]()
        print()  # trailing newline for neat separation in terminal output
    else:
        # Helpful message when a function isn't implemented yet.
        print(f"No code found for year {year}, question {question}.")

# Standard Python main
if __name__ == '__main__':
    main()