H.1.1: The secret rule isn't giving the answer of the right length

The secret rule is nothing more than an array rotation. However, I used the append and slice method, rather than the concatenate method. Why does the append and slice method not work?

input = [1, 1, 0] # MODIFY EXAMPLE
output = secret_box(input)
print("The result of applying the secret box to ", input, "is ", output)
# We will secretly apply the function and return the result!

def deterministic_box(bits):
    """Guess the secret deterministic rule.
    
    Args:
        bits (list[int]): A list of bits representing an initial condition.
         
    Returns: 
        list[int]: The output bits measured after deterministic evolution.
    """
    ##################
    # YOUR CODE HERE #
    ##################
    bits.append(bits[0])
    return bits[1:] # MODIFY THIS

print(deterministic_box([1,0,0,1,0,0,0,1]))

Hi, @mack.attack !
Thanks for this question, it really made me think! :thinking:
You spotted an interesting mistake.
I see that your code outputs the right answer and our evaluation shouldn’t be giving an error. However, it is doing it and the reason is that when you use append, the input array is modified and our evaluation code does a check comparing the input and output lengths returning a False value, hence the error.
Changing our evaluation code is a bit tricky and for that reason, I will add sentence in the exercise statement so it is clear that the function must preserve the input length.

Thanks. I definitely used an in-place method, even though the method is expecting an out-of-place method. What is the benefits of having to code this out-of-place?

I believe that when we wrote this codercise and the evaluation code, we weren’t considering these two methods and only stick with one. However, now that you ask, I can come up with a reason for using a method that doesn’t modify the input: in a more general circuit, you may have subsequent computations to perform on that same input and you would like to preserve it as is.
Thanks for your questions! I hope the Codebook experience keeps being fun and stimulating.

1 Like