Quek98
Quek98

Reputation: 21

Program Reasoning, python function

Program reasoning requires us to find the input for the function so that it outputs a certain result. The function is:

def func1(stuff, more_stuff=None):
    data = []
    for i, thing in enumerate(stuff):
        if thing >= 0 or i < 2:
            data.append(thing + i)
    print(len(stuff))
    if more_stuff is not None:
        data += more_stuff
    print(data)

And the expected output is:

4
[5, 3, 1, 5, -3]

The closest I can get it is:

4
[5, 3, 2, 5, -3]

with func1([5,2,0,2], [-3]) as input

I'm having trouble trying to get the the 1 and I'm just wanting to know how/why you can get a 1 as if it's anything less than 0 ie -1 as value for that index then 'thing' is < 0 and i = 2 so it skips that value/index and the output will be:

4
[5, 3, 5, -3]

Upvotes: 1

Views: 81

Answers (1)

Loocid
Loocid

Reputation: 6441

The key here is the length of stuff must be 4, NOT the length of data after the first for loop. You're absolutely right that you run into the problem where data[2] can never be 1 if you are trying to use stuff to fill data.

To fix this issue, chop stuff off before that and use more_stuff to append the values you need.

Ie the input you're looking for is:

>>>func1([5, 2, -1, -1], [1, 5, -3])
4
[5, 3, 1, 5, -3] 

Upvotes: 2

Related Questions