Skip to main content
Code Review

Return to Answer

added 75 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element. If there is any then it must be False.
  • Then find the next non-False element. There should not be any.

If that succeeds then the list is not of the required form, so the function can return False.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 return (next(elem for elem in list_iter if elem is not True) is False
 and next(elem for elem in list_iter if elem is not False) returnis False)
 except StopIteration:
 return True

Peilonrayz explained how to document the function using docstrings. In addition, the test cases can also be embedded into the docstrings, with doctest:

def check_true_then_false(x):
 """Check first n values are True and the rest are False.
 >>> check_true_then_false([True])
 True
 >>> check_true_then_false([False])
 True
 >>> check_true_then_false([False, True])
 False
 >>> check_true_then_false([True, False, True])
 False
 >>> check_true_then_false([1, 1, 0])
 False
 >>> check_true_then_false(["a", "b"])
 False
 """
 # ... Your code ...
if __name__ == "__main__":
 import doctest
 doctest.testmod()

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element.
  • Then find the next non-False element.

If that succeeds then the list is not of the required form, so the function can return False.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 next(elem for elem in list_iter if elem is not True)
 next(elem for elem in list_iter if elem is not False) return False
 except StopIteration:
 return True

Peilonrayz explained how to document the function using docstrings. In addition, the test cases can also be embedded into the docstrings, with doctest:

def check_true_then_false(x):
 """Check first n values are True and the rest are False.
 >>> check_true_then_false([True])
 True
 >>> check_true_then_false([False])
 True
 >>> check_true_then_false([False, True])
 False
 >>> check_true_then_false([True, False, True])
 False
 >>> check_true_then_false([1, 1, 0])
 False
 >>> check_true_then_false(["a", "b"])
 False
 """
 # ... Your code ...
if __name__ == "__main__":
 import doctest
 doctest.testmod()

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element. If there is any then it must be False.
  • Then find the next non-False element. There should not be any.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 return (next(elem for elem in list_iter if elem is not True) is False
 and next(elem for elem in list_iter if elem is not False) is False)
 except StopIteration:
 return True

Peilonrayz explained how to document the function using docstrings. In addition, the test cases can also be embedded into the docstrings, with doctest:

def check_true_then_false(x):
 """Check first n values are True and the rest are False.
 >>> check_true_then_false([True])
 True
 >>> check_true_then_false([False])
 True
 >>> check_true_then_false([False, True])
 False
 >>> check_true_then_false([True, False, True])
 False
 >>> check_true_then_false([1, 1, 0])
 False
 >>> check_true_then_false(["a", "b"])
 False
 """
 # ... Your code ...
if __name__ == "__main__":
 import doctest
 doctest.testmod()
added 874 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element.
  • Then find the next non-False element.

If that succeeds then the list is not of the required form, so the function can return False.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 next(elem for elem in list_iter if elem is not True)
 next(elem for elem in list_iter if elem is not False)
 return False
 except StopIteration:
 return True

Peilonrayz explained how to document the function using docstrings. In addition, the test cases can also be embedded into the docstrings, with doctest :

def check_true_then_false(x):
 """Check first n values are True and the rest are False.
 >>> check_true_then_false([True])
 True
 >>> check_true_then_false([False])
 True
 >>> check_true_then_false([False, True])
 False
 >>> check_true_then_false([True, False, True])
 False
 >>> check_true_then_false([1, 1, 0])
 False
 >>> check_true_then_false(["a", "b"])
 False
 """
 # ... Your code ...
if __name__ == "__main__":
 import doctest
 doctest.testmod()

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element.
  • Then find the next non-False element.

If that succeeds then the list is not of the required form, so the function can return False.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 next(elem for elem in list_iter if elem is not True)
 next(elem for elem in list_iter if elem is not False)
 return False
 except StopIteration:
 return True

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element.
  • Then find the next non-False element.

If that succeeds then the list is not of the required form, so the function can return False.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 next(elem for elem in list_iter if elem is not True)
 next(elem for elem in list_iter if elem is not False)
 return False
 except StopIteration:
 return True

Peilonrayz explained how to document the function using docstrings. In addition, the test cases can also be embedded into the docstrings, with doctest :

def check_true_then_false(x):
 """Check first n values are True and the rest are False.
 >>> check_true_then_false([True])
 True
 >>> check_true_then_false([False])
 True
 >>> check_true_then_false([False, True])
 False
 >>> check_true_then_false([True, False, True])
 False
 >>> check_true_then_false([1, 1, 0])
 False
 >>> check_true_then_false(["a", "b"])
 False
 """
 # ... Your code ...
if __name__ == "__main__":
 import doctest
 doctest.testmod()
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95

Your code works correctly under the assumption that the given list contains only True or False elements. For other lists it can return "false positives"

>>> check_true_then_false([1, 1, 0])
True

or abort with a runtime error:

>>> check_true_then_false(["a", "b"])
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The function traverses the given list in order to find the number of True elements. It then creates two additional lists, which are also traversed to check if all elements are True resp. False.

A more efficient way would be to iterate the given list only once:

  • Find the first non-True element.
  • Then find the next non-False element.

If that succeeds then the list is not of the required form, so the function can return False.

If either of the above iterations fails (and next() raises a StopIteration exception) then the list is of the required form, and the function returns True:

def check_true_then_false(x):
 list_iter = iter(x)
 try:
 next(elem for elem in list_iter if elem is not True)
 next(elem for elem in list_iter if elem is not False)
 return False
 except StopIteration:
 return True
lang-py

AltStyle によって変換されたページ (->オリジナル) /