[Python] Difficulty Interpreting This Exam Question

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    enter image description here

    Here's the original Problem, it's a linked list environment diagram of sorts.

    def slice_up(s):
    copy = s
    t = [copy]
    while len(s) > 1:
    t.append(s[:len(s)-1])
    s.pop()
    s[0] = len(s)
    return t

    t = [3, 4, 5]
    print(slice_up(t))
    print(t)


    So here's how I tried to map it out:

    slice_up([3, 4, 5])
    copy = [3, 4, 5]
    t = [[3,4,5]] ([3,4,5] = [[3, 4, 5]])
    while len([3,4,5]) > 1 (yes, 3 > 1)
    t.append(s[ :len(s)-1]) len of s is 3 so s[:2]?
    s.pop() pops the last digit of s so [3,4]
    s[0] = len(s) first index of s = 2 so [2,4]?
    return t


    So I ended up getting [[3, 4, 5], 5] as the return statement for line 11 and [2, 4] for print(t) which ended up being very wrong.

    Continue reading...

Compartilhe esta Página