C5W3 Insert_Ones

Maybe I’m misunderstanding the requirements because I think I’m doing what’s asked but not passing the unit tests.

We’re taking 2 inputs:

y = a np.array of zeroes with a shape of (1,1375) representing the audio steps
segment_end_ms = which is the end of the positive segment in ms

The given code converts segment_end_ms to segment_end_y which is the step in y which the segment ends.

We’re asked to start at the step = segment_end_y+1 and set all the successive values of y to 1. So if segment_end_y = 30 the values of y[0,31:1374] should be set to 1. Is that correct?

I got it to pass by limiting my for loop to segment_end_y+51 but that feels “hacky”

I got the tests to pass by implementing the segment_end_y+51 you mentioned but in the sanity check cell 48 I got this error:

<ipython-input-48-21e1908c08b9> in <module>
----> 1 arr1 = insert_ones(np.zeros((1, Ty)), 9700)

<ipython-input-46-7eb81572289d> in insert_ones(y, segment_end_ms)
     26         for i in range(segment_end_y, segment_end_y+51):
     27             if segment_end_y < i:
---> 28                 y[0, i] = 1
     29         ### END CODE HERE ###
     30 

IndexError: index 1375 is out of bounds for axis 1 with size 1375

Is there something wrong with my if statement? Did you figure out a better way?

The issue had to do with the if statement.
I figured it out with this tip but it feels a bit hacky indeed.

As Given in The Instructions The Loop Starts after then end of segment So,segment_end_y+1 should be the start of the Range Function

Another addition to the above “hacky” solution to the problem , i added another condition in the if statement to pass the “sanity check” ! if i < 1375 and segment_end_y < i:)

Keep in mind Ty = 1375 if you’re looking for a less hacky way to implement the same idea :wink:

No, that’s not what the instruction say. They say to set the next 50 values to 1 starting with one after the end of the segment.

You don’t need a for loop to do that: python/numpy indexing supports ranges.

Why is that “hacky” if the instructions are to set 50 values to 1? Well, it might be a little more elegant if they had made the number of 1 values to set be a parameter. But the instructions are pretty clear as written.