# UNQ_C4 is incorrect

Hi everyone. I do Exercise 4: Get Span From Scores. When I check my result is same as expect value. But when I submit, I have message Code Cell UNQ_C4: Function 'get_span_from_scores is incorrect. Check implementation. I don’t know why i wrong, can anyone explain it ti me . Thanks. This is my code:

``````def get_span_from_scores(start_scores, end_scores, input_mask, verbose=False):
"""
Find start and end indices that maximize sum of start score
and end score, subject to the constraint that start is before end
and both are valid according to input_mask.

Args:
start_scores (list): contains scores for start positions, shape (1, n)
end_scores (list): constains scores for end positions, shape (1, n)
input_mask (list): 1 for valid positions and 0 otherwise
"""
n = len(start_scores)
max_start_i = -1
max_end_j = -1
max_start_score = -np.inf
max_end_score = -np.inf
max_sum = -np.inf

# Find i and j that maximizes start_scores[i] + end_scores[j]
# so that i <= j and input_mask[i] == input_mask[j] == 1

### START CODE HERE (REPLACE INSTANCES OF 'None' with your code) ###
# set the range for i
for i in range(n): # complete this line

# set the range for j
for j in range(1,n): #complete this line

# both input masks should be 1
# check if the sum of the start and end scores is greater than the previous max sum
if start_scores[i]+end_scores[j]>max_sum: # complete this line

# calculate the new max sum
max_sum = start_scores[i]+end_scores[j]

# save the index of the max start score
max_start_i = i

# save the index for the max end score
max_end_j = j

# save the value of the max start score
max_start_val = start_scores[i]

# save the value of the max end score
max_end_val = end_scores[j]

### END CODE HERE ###
if verbose:
print(f"max start is at index i={max_start_i} and score {max_start_val}")
print(f"max end is at index i={max_end_j} and score {max_end_val}")
print(f"max start + max end sum of scores is {max_sum}")
return max_start_i, max_end_j
``````

In the first if loop you should add i<=j in the condition. Check the comment before the ### START CODE HERE line.

Also start the range of j from 0,not from 1.

2 Likes

Hi,
In the loop to iterate over scores to get span, the inner loop should start from i instead of 1. For instance
` for j in range (i,n) `
I’m quoting text from the lab assignment,

In words, this formulas is saying, calculate the sum and start scores of start position ‘i’ and end position ‘j’, given the constraint that the start ‘i’ is either before or at the end position ‘j’; then find the positions ‘i’ and ‘j’ where this sum is the highest.

If we loop from (1,n) then there will be some values of i for which it’ll be before the end of j. So, the j loop must start from the current i th iteration.

Also, make sure that ` # UNQ_C4 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)` is there in the first line of the cell.

2 Likes

Thank you so much for helping me solve this problem.

2 Likes

Thank you so much for helping me solve this problem.

2 Likes