Feb-02-2019, 03:02 AM
Feb-02-2019, 03:06 AM
The all function is your friend. You could do all on each row as a list comprehension, and then all on the result. Depending on the size of the matrix, doing it as a loop so you can short circuit in may be good. Then you would loop through the rows, any on each row, break if it's false, and else on the loop for if all of them are true.
Feb-02-2019, 03:34 AM
Thank you for your prompt reply. But unfortunately I am such a total beginnier that your advice it too complicated for me. I know about "loop" and "all" but I am unable to get it working. I have the following:
I would be very grateful if you could advise me on this.
Thank you!
def result(matrix): for row in matrix: for element in row: if all(matrix) == "X": return True else: return FalseBut it returns a wrong result.
I would be very grateful if you could advise me on this.
Thank you!
Feb-02-2019, 04:30 AM
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Let's forget about all. You have the right idea, but you don't want to return True until the end of the loop. You are returning True when you get one correct result, you only want to do that when all the results are True. However, as soon as one of the checks fails (
So switch the conditional in the loop to return False if the element isn't correct. Then after both loops are done, return True.
Note that your code checks that all the items are 'X'. To check that they are all equal (no matter what they are), check against matrix[0][0] instead (that's the first item in the first row).
Let's forget about all. You have the right idea, but you don't want to return True until the end of the loop. You are returning True when you get one correct result, you only want to do that when all the results are True. However, as soon as one of the checks fails (
if element != 'X':
) you can return False. So switch the conditional in the loop to return False if the element isn't correct. Then after both loops are done, return True.
Note that your code checks that all the items are 'X'. To check that they are all equal (no matter what they are), check against matrix[0][0] instead (that's the first item in the first row).
Feb-02-2019, 04:58 AM
(Feb-02-2019, 04:30 AM)ichabod801 Wrote: [ -> ]Note that your code checks that all the items are 'X'. To check that they are all equal (no matter what they are), check against matrix[0][0] instead (that's the first item in the first row).
I don't know is it important or not but just observing that in case of empty matrix (list of empty lists) checking against matrix[0][0] will give IndexError.
Feb-02-2019, 08:37 AM
There are also some other approaches to explore:
- there are sets in Python (from documentation: "Basic uses include membership testing and eliminating duplicate entries."). Problem with solution below is that it will not short circuit (following code assumes that in case of empty matrix elements are considered equal as well, if its not the case comparison should be == 1):
- there are sets in Python (from documentation: "Basic uses include membership testing and eliminating duplicate entries."). Problem with solution below is that it will not short circuit (following code assumes that in case of empty matrix elements are considered equal as well, if its not the case comparison should be == 1):
In [1]: m = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] In [2]: len({el for row in m for el in row}) <= 1 # 0 when empty matrix, 1 when all equal Out[2]: True In [3]: n = [[1, 1, 1], [1, 1, 1], [1, 2, 1]] In [4]: len({el for row in n for el in row}) <= 1 Out[4]: False- Python has built-in itertools module and it's documentation contains recipies where one of them is:
def all_equal(iterable): "Returns True if all the elements are equal to each other" g = groupby(iterable) return next(g, True) and not next(g, False)
Feb-02-2019, 01:02 PM
Thank you very much for your help with this! :)
Feb-02-2019, 04:17 PM
(Feb-02-2019, 04:58 AM)perfringo Wrote: [ -> ]I don't know is it important or not but just observing that in case of empty matrix (list of empty lists) checking against matrix[0][0] will give IndexError.
I think it should. Checking that all the items in an empty matrix are equal doesn't make sense, so I think returning an IndexError is a good idea. Sort of like popping from an empty list.