[Python] Is this DP function correct for the given prompt? [closed]

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    S(n) = 1 if n = 1
    2 if n = 2
    S(n - 1) + S(n - 2) if n is even
    2S(n - 1) - S(n - 2) if n is odd and n > 2


    SOLUTION:

    def layup_s(n):
    # given base cases
    if n == 1:
    return 1
    elif n == 2:
    return 2

    s1, s2 = 1, 2 #first two sequence values

    for i in range(3, n + 1):
    if i % 2 == 0: #even case
    sn = s2 + s1
    else:
    sn = 2 * s2 - s1 #odd case
    s1, s2 = s2, sn
    return s2


    Can someone explain what is wrong with this solution? For smaller values it functions correctly:

    S(1) = 1, S(2) = 2, S(3) = 3, S(4) = 5, S(5) = 7


    A recruiter is hell bent on telling me it is incorrect for S(10000) even though I had two LLMs generate the exact same answer as mine and I just do not understand the issue!

    EDIT : The task was to find S(10000) and am being told the value is incorrect....

    Continue reading...

Compartilhe esta Página