Skip to main content
Fixed typos.
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

As suggested in your commentcomments, the task can be done directly without having to modify values to ints or change the structure of the input.

Input: A tuple A of n rows and m columns.
Output: Whether there is matching $4$ "O"'s or 4 "X"'s on a diagonal, horizontal, or vertical line.
Procedure:

# horizontal lines
For i in range(n):
    For j in range(m-3):
        Find if A[i][j], A[i][j+1], A[i][j+2], A[i][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

# vertical lines
For j in range(m):
    For i in range(n-3):
        Find if A[i][j], A[i+1][j], A[i+2][j], A[i+3][j] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

# diagonal lines
For i in range(n-3):
    For j in range(m-3):
        Find if A[i][j], A[i+1][j+1], A[i+2][j+2], A[i+3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

# other diagonal lines
For i in range(3, n):
    For j in range(m-3):
        Find if A[i][j], A[i-1][j+1], A[i-2][j+2], A[i-3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

Return False, since $4$no $4$ "O"'s ornor 4 "X"'s has been found.

ThereOne common difficulty is not a lot of wisdom here. Just make sure ofhow we can figure out the ranges of the possible indices of the staringstarting "O" or "X".

The basic technique is, for example, if A[i-3][j+3] will be used, then it means "0 <= i-3 < n" and "0 <= j+3 < m", which means "3 <= i < n+3" and "-3 <= j < m -3". Together with "0 <= i < n" and "0 <= j < m", we have "3 <= i < n" and "0 <= j < m-3". That is why you seehow we can write "i in range(3, n)" and "j in range(m-3)" in the case of "other diagonal lines".

  

As suggested in your comment, the task can be done directly without having to modify values to ints.

Input: A tuple A of n rows and m columns.
Output: Whether there is matching $4$ "O"'s or 4 "X"'s on a diagonal, horizontal, or vertical line.
Procedure:

# horizontal lines
For i in range(n):
    For j in range(m-3):
        Find if A[i][j], A[i][j+1], A[i][j+2], A[i][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes.
# vertical lines
For j in range(m):
    For i in range(n-3):
        Find if A[i][j], A[i+1][j], A[i+2][j], A[i+3][j] are 4 "0"s or 4 "X"s. Return accordingly if yes.
# diagonal lines
For i in range(n-3):
    For j in range(m-3):
        Find if A[i][j], A[i+1][j+1], A[i+2][j+2], A[i+3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes.
# other diagonal lines
For i in range(3, n):
    For j in range(m-3):
        Find if A[i][j], A[i-1][j+1], A[i-2][j+2], A[i-3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes.
Return False, since $4$ "O"'s or 4 "X"'s has been found.

There is not a lot of wisdom here. Just make sure of the ranges of the indices of the staring "O" or "X".

The basic technique is, for example, if A[i-3][j+3] will be used, then it means "0 <= i-3 < n" and "0 <= j+3 < m", which means "3 <= i < n+3" and "-3 <= j < m -3". Together with "0 <= i < n" and "0 <= j < m", we have "3 <= i < n" and "0 <= j < m-3". That is why you see "i in range(3, n)" and "j in range(m-3)" in the case of "other diagonal lines".

 

As suggested in comments, the task can be done directly without having to modify values to ints or change the structure of the input.

Input: A tuple A of n rows and m columns.
Output: Whether there is matching $4$ "O"'s or 4 "X"'s on a diagonal, horizontal, or vertical line.
Procedure:

# horizontal lines
For i in range(n):
    For j in range(m-3):
        Find if A[i][j], A[i][j+1], A[i][j+2], A[i][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

# vertical lines
For j in range(m):
    For i in range(n-3):
        Find if A[i][j], A[i+1][j], A[i+2][j], A[i+3][j] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

# diagonal lines
For i in range(n-3):
    For j in range(m-3):
        Find if A[i][j], A[i+1][j+1], A[i+2][j+2], A[i+3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

# other diagonal lines
For i in range(3, n):
    For j in range(m-3):
        Find if A[i][j], A[i-1][j+1], A[i-2][j+2], A[i-3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes. 

Return False, since no $4$ "O"'s nor 4 "X"'s has been found.

One common difficulty is how we can figure out the ranges of the possible indices of the starting "O" or "X".

The basic technique is, for example, if A[i-3][j+3] will be used, then it means "0 <= i-3 < n" and "0 <= j+3 < m", which means "3 <= i < n+3" and "-3 <= j < m -3". Together with "0 <= i < n" and "0 <= j < m", we have "3 <= i < n" and "0 <= j < m-3". That is how we can write "i in range(3, n)" and "j in range(m-3)" in the case of "other diagonal lines".

 
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

As suggested in your comment, the task can be done directly without having to modify values to ints.

Input: A tuple A of n rows and m columns.
Output: Whether there is matching $4$ "O"'s or 4 "X"'s on a diagonal, horizontal, or vertical line.
Procedure:

# horizontal lines
For i in range(n):
    For j in range(m-3):
        Find if A[i][j], A[i][j+1], A[i][j+2], A[i][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes.
# vertical lines
For j in range(m):
    For i in range(n-3):
        Find if A[i][j], A[i+1][j], A[i+2][j], A[i+3][j] are 4 "0"s or 4 "X"s. Return accordingly if yes.
# diagonal lines
For i in range(n-3):
    For j in range(m-3):
        Find if A[i][j], A[i+1][j+1], A[i+2][j+2], A[i+3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes.
# other diagonal lines
For i in range(3, n):
    For j in range(m-3):
        Find if A[i][j], A[i-1][j+1], A[i-2][j+2], A[i-3][j+3] are 4 "0"s or 4 "X"s. Return accordingly if yes.
Return False, since $4$ "O"'s or 4 "X"'s has been found.

There is not a lot of wisdom here. Just make sure of the ranges of the indices of the staring "O" or "X".

The basic technique is, for example, if A[i-3][j+3] will be used, then it means "0 <= i-3 < n" and "0 <= j+3 < m", which means "3 <= i < n+3" and "-3 <= j < m -3". Together with "0 <= i < n" and "0 <= j < m", we have "3 <= i < n" and "0 <= j < m-3". That is why you see "i in range(3, n)" and "j in range(m-3)" in the case of "other diagonal lines".