[Tutor] function return values

Phil phillor9 at gmail.com
Thu Dec 2 23:11:37 EST 2021


On 3/12/21 05:37, Alan Gauld via Tutor wrote:
> On 02/12/2021 00:24, Phil wrote:
>
>>       def boxPairsInSameRow(self, pairs, x, y):
>>           col_list = []
> You don't need this since you set it below
>
>>           for item, count in pairs.items():
>>               if count == 2:  # then item is a pair candidate
>>                   for row in range(x, x + 3):
>>                       col_list = []
>>                       for col in range(y, y + 3):
>>                           if item in self.solution[row][col]:
>>                               col_list.append(col)
>>                       return item, row, col_list
> This return will happen after the first row is processed,
> you never process the other rows. I suspect that's not
> what you intended?

You are correct Alan. The pair exists on row zero in my test case.

This function should return a valid result if two numbers are in the 
same row. So, if a number passes the "if count == 2" test then that 
number is a pair and if the pair is found in row zero then it cannot 
exist anywhere else so there is no need to keep searching. But, what if 
the pair is in row 1 or 2 and not in the first row? I think a "found" 
test is needed before the return statement and the first thing that 
comes to mind is:

if len(col_list) == 2:

     return item, row, col_list

else:

     continue

And, if the pair is not in the same row then return None, None, None.

I'll give this a test tomorrow.

-- 

Regards,
Phil



More information about the Tutor mailing list