Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python : You should not be using for or while loops on any of the problems and i

ID: 3684398 • Letter: P

Question

Python: You should not be using for or while loops on any of the problems and it must be a pure function with no side effect.

#A Binary BST Tree (BST) is one of:
# - String
# - a two-item tuple of binary string trees (BST, BST)

1. Define a recursive function called flatten() that takes a BST and returns a single long tuple whose items are the leaves of the tree. So flatten( ( ("one", ("two","three")) , ("four","five") ) ) should return ("one", "two", "three", "four", "five"),flatten( ("left","right") ) should return ("left", "right"), and flatten("One-leaf") should return ("One-leaf",)

What I did-- which result in returning per character in a tuple instead of word:

def flatten(BST):
"""returns a single long tuple whose items are the leaves of the tree

BST -> tuple-of-str"""
long_tuple = ()
if isinstance(BST, tuple):
long_tuple += flatten(BST[0]) + flatten(BST[1])
return long_tuple
else:
return tuple(BST)

2. Define a recursive function called has_o() that takes a BST and returns True if at least one leaf in the tree contains the letter o (upper or lowercase). So has_o( ( ("one", ("two","three")) , ("four","five") ) ) should return True, has_o( ("left","right") ) should return False, and has_o("One-leaf") should return True.

What I did -- which doesn't work at all:

def has_o(BST):
"""returns True if at least one leaf contains letter o (upper or lowercase)

BST -> boolean"""
if isinstance(BST, tuple):
return has_o(BST[0]) or has_o(BST[1]) in "oO"
else:
return BST == 0

Could you please help me fixing my function. Thank you!

Explanation / Answer

----Program run as following take as input and produce output