This Week's Worksheet ⬇️

mentor07.pdf

mentor07_sol.pdf

Tree Concept Review

Tree Practice Problems

  1. Let t be the tree depicted above. What do the following expressions evaluate to? If the expressions evaluates to a tree, format your answer as tree(... , ...).

Untitled

Untitled


  1. Write the function sum_of_nodes which takes in a tree and outputs the sum of all the elements in the tree.

    def sum_of_nodes(t): 
    	 """
       >>> sum_of_nodes(t) # 4 + 5 + 2 + 1 + 8 + 2 + 1 + 4 = 27
       27
       """
    

    Observations:

    Solution:


    1. Write a function, replace_x that takes in a tree, t, and returns a new tree with all labels x replaced with 0.

      Untitled

      Observations:

      • (Toggle to check)

      Solution:

      • (Toggle to check)

      1. Write a function, all_paths that takes in a tree, t, and returns a list of paths from the root to each leaf. For example, if we called all_paths(t) on the following tree, all_paths(t) would return [[2, 2], [2, 4, 2], [2, 4, 3]].

        Untitled

        def all_paths(t): 
        		paths = []
        		if ________________________________________ 
        				_______________________________________
        		else: 
        				_______________________________________
        		        ___________________________________
        		            _______________________________
        		return paths
        

        Observations:

        • (Toggle to check)

        Solution:

        • (Toggle to check)

    Mutation Concept Review

    Immutability vs Mutability

    List Methods

    Mutative (destructive) Operations

    Non-mutative (non-destructive) Operations

    Untitled

    Mutation Practice Problems

    Untitled

    https://pythontutor.com/visualize.html#mode=edit


    1. Given some list lst, possibly a deep list, mutate lst to have the accumulated sum of all elements so far in the list. If there is a nested list, mutate it to similarly reflect the accumulated sum of all elements so far in the nested list. Return the total sum of the original lst.

      Hint: The isinstance function returns True for isinstance(l, list) if l is a list and False otherwise.

      def accumulate(lst):
      		"""
      		>>> l = [1, 5, 13, 4]
      		>>> accumulate(l)
      		23
      		>>> l
      		[1, 6, 19, 23]
      		>>> deep_l = [3, 7, [2, 5, 6], 9]
      		>>> accumulate(deep_l)
      		32
      		>>> deep_l
      		[3, 10, [2, 7, 13], 32]
      		"""
      		sum_so_far = 0
      		for ________________________________________:
      				________________________________________ 
      				if isinstance(___________________, list):
                  inside = ___________________________
      						____________________________________ 
      				else:
      	          ____________________________________
      						____________________________________ 
      		return ___________________________________
      

      Observations:

      • (Toggle to check)

      Solution:

      • (Toggle to check)