Skip to main content
Code Review

Return to Answer

added 1 character in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

It is better to test for x is None rather than x == None.


Avoid using single-letter variable names — they may make sense to you, but not to anyone else.

I don't see any reason why a node should be automatically not counted if its value is None. Shouldn't it be up to the predicate to decide whether nodes with None as a value isare counted or not?

You can eliminate a case by taking advantage of the fact that int(False) is 0 and int(True) is 1.

def count(tree_node, predicate):
 """Counts the tree_node and its descendants whose value satisfies the predicate."""
 if tree_node is None:
 return 0
 else:
 return int(predicate(tree_node.value)) + \
 count(tree_node.left, predicate) + \
 count(tree_node.right, predicate)

Stylistically, it would be better to consistently use either one long if... elif chain or just ifs with early returns. I also suggest putting the recursive case at the end of the function.

(ll1 != None and ll2 == None) or (ll2 != None and ll1 == None) can be simplified.

def equal(ll1, ll2):
 """Recursively checks whether two linked lists contain the same values
 in the same order."""
 if ll1 is None and ll2 is None:
 return True
 if ll1 is None or ll2 is None:
 return False
 if ll1.value != ll2.value:
 return False
 return equal(ll1.next, ll2.next)

Assuming that the linked list contains no None data values, the logic can be simplified.

def min_max(ll):
 """Returns a 2-tuple of the minimum and maximum values.
 If ll is empty, returns (None, None)."""
 if ll is None:
 return None, None
 if ll.next is None:
 return ll.value, ll.value
 least, greatest = min_max(ll.next)
 return min(ll.value, least), max(ll.value, greatest)

It is better to test for x is None rather than x == None.


Avoid using single-letter variable names — they may make sense to you, but not to anyone else.

I don't see any reason why a node should be automatically not counted if its value is None. Shouldn't it be up to the predicate to decide whether nodes with None as a value is counted or not?

You can eliminate a case by taking advantage of the fact that int(False) is 0 and int(True) is 1.

def count(tree_node, predicate):
 """Counts the tree_node and its descendants whose value satisfies the predicate."""
 if tree_node is None:
 return 0
 else:
 return int(predicate(tree_node.value)) + \
 count(tree_node.left, predicate) + \
 count(tree_node.right, predicate)

Stylistically, it would be better to consistently use either one long if... elif chain or just ifs with early returns. I also suggest putting the recursive case at the end of the function.

(ll1 != None and ll2 == None) or (ll2 != None and ll1 == None) can be simplified.

def equal(ll1, ll2):
 """Recursively checks whether two linked lists contain the same values
 in the same order."""
 if ll1 is None and ll2 is None:
 return True
 if ll1 is None or ll2 is None:
 return False
 if ll1.value != ll2.value:
 return False
 return equal(ll1.next, ll2.next)

Assuming that the linked list contains no None data values, the logic can be simplified.

def min_max(ll):
 """Returns a 2-tuple of the minimum and maximum values.
 If ll is empty, returns (None, None)."""
 if ll is None:
 return None, None
 if ll.next is None:
 return ll.value, ll.value
 least, greatest = min_max(ll.next)
 return min(ll.value, least), max(ll.value, greatest)

It is better to test for x is None rather than x == None.


Avoid using single-letter variable names — they may make sense to you, but not to anyone else.

I don't see any reason why a node should be automatically not counted if its value is None. Shouldn't it be up to the predicate to decide whether nodes with None as a value are counted or not?

You can eliminate a case by taking advantage of the fact that int(False) is 0 and int(True) is 1.

def count(tree_node, predicate):
 """Counts the tree_node and its descendants whose value satisfies the predicate."""
 if tree_node is None:
 return 0
 else:
 return int(predicate(tree_node.value)) + \
 count(tree_node.left, predicate) + \
 count(tree_node.right, predicate)

Stylistically, it would be better to consistently use either one long if... elif chain or just ifs with early returns. I also suggest putting the recursive case at the end of the function.

(ll1 != None and ll2 == None) or (ll2 != None and ll1 == None) can be simplified.

def equal(ll1, ll2):
 """Recursively checks whether two linked lists contain the same values
 in the same order."""
 if ll1 is None and ll2 is None:
 return True
 if ll1 is None or ll2 is None:
 return False
 if ll1.value != ll2.value:
 return False
 return equal(ll1.next, ll2.next)

Assuming that the linked list contains no None data values, the logic can be simplified.

def min_max(ll):
 """Returns a 2-tuple of the minimum and maximum values.
 If ll is empty, returns (None, None)."""
 if ll is None:
 return None, None
 if ll.next is None:
 return ll.value, ll.value
 least, greatest = min_max(ll.next)
 return min(ll.value, least), max(ll.value, greatest)
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

It is better to test for x is None rather than x == None.


Avoid using single-letter variable names — they may make sense to you, but not to anyone else.

I don't see any reason why a node should be automatically not counted if its value is None. Shouldn't it be up to the predicate to decide whether nodes with None as a value is counted or not?

You can eliminate a case by taking advantage of the fact that int(False) is 0 and int(True) is 1.

def count(tree_node, predicate):
 """Counts the tree_node and its descendants whose value satisfies the predicate."""
 if tree_node is None:
 return 0
 else:
 return int(predicate(tree_node.value)) + \
 count(tree_node.left, predicate) + \
 count(tree_node.right, predicate)

Stylistically, it would be better to consistently use either one long if... elif chain or just ifs with early returns. I also suggest putting the recursive case at the end of the function.

(ll1 != None and ll2 == None) or (ll2 != None and ll1 == None) can be simplified.

def equal(ll1, ll2):
 """Recursively checks whether two linked lists contain the same values
 in the same order."""
 if ll1 is None and ll2 is None:
 return True
 if ll1 is None or ll2 is None:
 return False
 if ll1.value != ll2.value:
 return False
 return equal(ll1.next, ll2.next)

Assuming that the linked list contains no None data values, the logic can be simplified.

def min_max(ll):
 """Returns a 2-tuple of the minimum and maximum values.
 If ll is empty, returns (None, None)."""
 if ll is None:
 return None, None
 if ll.next is None:
 return ll.value, ll.value
 least, greatest = min_max(ll.next)
 return min(ll.value, least), max(ll.value, greatest)
lang-py

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