-1

This is my solution:

#across
for i in range(len(myList)):
     for j in range(len(myList[0])):
         if (myList[i][j]=='X' OR 'O'):
             print("player myList[i][j] is the winner" %s)
#down
for i in range(len(myList)):
    for j in range(len(myList[0])):
        if (myList[j][i]=='X' OR 'O'): #mylist[0][0] [1][0]
            print("player myList[i][j] is the winner" %s)

However what is confusing me is that it's unclear if the values GOING DOWN A COLUMN are being compared between i loops?

cullzie
  • 2,705
  • 2
  • 16
  • 21
  • `myList[i][j]=='X' OR 'O'` is not the correct way to test if a variable contains either value. It should be `myList[i][j]=='X' OR myList[i][j]== 'O'` or `myList[i][j] in ['X', 'O']` – Barmar Apr 16 '19 at 01:06

1 Answers1

0

Neither of your loops is testing whether all the values are the same.

To test whether all the values are the same, put the value in the first entry of the row or column in a variable, then use the all() function to test the rest of the row or column.

# across
for row in myList:
    first = row[0]
    if first in ['X', 'O'] and all(col = first for col in row[1:]):
        print("Player %s is the winner" % first)
        break
else: # This will only be executed if we didn't break out of the loop
    # down
    for colnum in len(myList[0]):
        first = myList[0][colnum]
        if first in ['X', 'O'] and all(row[colnum] = first for row in myList[1:])
            print("Player %s is the winner" % first)
            break

Another way to tell if a list is all equal is with len(set(l)) == 1 (converting a list to a set removes all the duplicates, there will be one element if they're all duplicates), so you can write:

# across
for row in myList:
    first = row[0]
    if first in ['X', 'O'] and len(set(row)) == 1:
        print("Player %s is the winner" % row[0])
        break
else: # This will only be executed if we didn't break out of the loop
    # down
    for colnum in len(myList[0]):
        if first in ['X', 'O'] and len(set(row[colnum] for row in myList))) == 1:
            print("Player %s is the winner" % myList[0][colnum])
            break
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • i finally understood the syntax here, but but check for first ? Is there a simpler syntax to just check all three values I basically wouldn't be able to come up with this myself, so why did we initialize the first element of row 0 and then iterate to see if it matches that value? – Mukhter Ali Said Apr 16 '19 at 02:59
  • See https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical for other ways to test if all elements in an array are equal. But to do this for columns, you have to convert a column to a list. You can do this by transposing the array and then processing the rows of this. – Barmar Apr 16 '19 at 03:02
  • `len(set(row)) == 1` is a very simple syntax to tell if all the elements are the same. – Barmar Apr 16 '19 at 03:04
  • `len(set(row[colnum] for row in myList))) == 1` will tell if a column is all the same. – Barmar Apr 16 '19 at 03:05