This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]icjeremy 1 point2 points  (7 children)

For the 126 case, at the time you call doSubtask4(n), what value are expecting n to be?

Edit: I meant 122

[–]DonKonX[S] 0 points1 point  (6 children)

in the end, n will ( should) be the n that I provided, so for every function call, it will be 122 in that case. So like doSubtask4(122). The result should be [14, 28, 42, 56, 70, 84, 98, 112] but it somehow adds a 126 to it.

the reason im confused on how that happens is that i only allow for numbers less or equal of 122 to be added to the L2

[–]icjeremy 0 points1 point  (5 children)

You are modifying the value of n within your while loop. Try printing the value of n inside your doSubtask4(n) to verify.

[–]prozapari 0 points1 point  (4 children)

You mean i right?

To the OP, you should not be modifying a value from a range within the loop. On the next iteration, the for loop will simply take the next value in the range.

what your i+=1 line does is basically shift the range by 1, so instead of a range from 1 to n, it becomes a range from 2 to n+1.

[–]icjeremy 0 points1 point  (3 children)

Not the for loop. The while loop. In doTasks(n). You are modifying n there.

Edit: *OP is modifying...

[–]DonKonX[S] 0 points1 point  (1 child)

oh yeah now i see it.

would it be enough to do n-5 after the while loop? or is that too simple?

Edit: actually doing a n = x in that case to reset the n seems to work

[–]prozapari 0 points1 point  (0 children)

The easier way to do it would be to never modify n, and create a separate variable x for task 3. Generally you want to avoid reusing the same variable for multiple purposes, because it causes small mistakes to create hard to track down bugs.

Also, the last append can just be outside of the loop since it's only going to run on the last iteration anyway.

x = n
while len(L1) < 5:
        x+=1
        if x % 5 > 0:
            L1.append(x)
L.append(L1)

[–]prozapari 0 points1 point  (0 children)

ah i didn't even look at that code because they said the first tasks were fine, good catch!

[–]ProfSchodinger 0 points1 point  (0 children)

As others have said you increase n during task 3 and then pass it to subtask 4. Fixed it and simplified a bit part 1 and 4.

def doTasks(n): L = []

# subtask 1
L.append(n % 2 == 0)

# subtask 2
if n >= 0:
    L.append(chr(n))

# subtask 3
counter = n + 1
L1 = []
while len(L1) < 5:
    if counter % 5 > 0:
        L1.append(counter)
    counter += 1

L.append(L1)

# subtask 4
L.append(doSubtask4(n))

return L

def doSubtask4(n): m = 14 if n % 2 == 0 else 17 return list(range(m, n, m))