The dtree_subst function produces the same tree as tree_subst, but docs so destructively. Only the blossom...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
The dtree_subst function produces the same tree as tree_subst, but docs so destructively. Only the blossom function may create new trees. def dtree subst (pattern, blossom, tree): *"Return a tree that results from replacing each subtree of TREE that matches PATTERN (as defined by tree_match) with BLOSSOM (d), where d is the dictionary mapping pattern variable labels to trees that is returned by tree_match. Matching proceeds from the leaves up, with the pattern always being matched to the subtree resulting from performing substitution on its children. The function is destructive. >> T = Tree("*", [Tree("+", [Tree (10), Tree("+", [Tree (0), Tree (".", [Tree(2), Tree(3)])])]), Tree("+", [Tree (0), Tree (0)])1) >>> P1 = Tree ("+", [ Tree (0), Tree ("SX") 1) >>> dtree_subst (P1, lambda d: d['$X'], T) Tree ('', [Tree ('+', [Tree(10), Tree('*, [Tree (2), Tree (3)])]), Tree(0)]) >>> T Tree('*', [Tree('+', [Tree(10), Tree ('*', [Tree(2), Tree(3)1)]), Tree (0)]) >> dtree_subst (P1, lambda d: d['$X'), T) Tree(, (Tree('+, (Tree(10), Tree(, (Tree (2), Tree (3)])]), Tree(0)]) >>> dtree_subst (P1, lambda d: d['$X'], Tree('+', [Tree ('+', [Tree(0), Tree (0)]), Tree (5)])) ... Tree (5) >> T = Tree('*, (Tree(5), Tree(10)]) >>> TO = T.branches [0] >» dtree subst (P1, lambda d: Tree (0), T) # Should not change tree. Tree('', (Tree (5), Tree(10)]) >>> TO is T.branches [0] # Check that new node not created. True for k in #3 (a) (ь) %23 d = tree_match(pattern, tree) if <same as blank (c) in the tree subst problem>: return <same as blank (d) in tree subst problem> else: return (c) i. Fill in blank (a). Fill in blank (b). iii. Fill in blank (c). I'd like to use dtree_subst to replace all subtrees of T of the form Tree('+', [Tree (A), Tree (B) ]), where A and B are integers, with Tree (C), where C is the sum of A and B. We'll assume that Trees with integer labels are leaves. I use the call iv. dtree subst (Tree('+', [Tree('$A'), Tree('$B')]), compute_subst, T) and define compute subst like this: def compute_subst (d) : A = d['$A'].label B = d['$B'].label if type (A) is int and type(B) is int return A + B else return What should replace the blank? O dl' $B'] Tree ('+', [A, B]) T Tree('+', [d['$A'], d['$B']]) [. V$.]p O Tree('+', [Tree(d['$A']), Tree (d['$B'1)]) The dtree_subst function produces the same tree as tree_subst, but docs so destructively. Only the blossom function may create new trees. def dtree subst (pattern, blossom, tree): *"Return a tree that results from replacing each subtree of TREE that matches PATTERN (as defined by tree_match) with BLOSSOM (d), where d is the dictionary mapping pattern variable labels to trees that is returned by tree_match. Matching proceeds from the leaves up, with the pattern always being matched to the subtree resulting from performing substitution on its children. The function is destructive. >> T = Tree("*", [Tree("+", [Tree (10), Tree("+", [Tree (0), Tree (".", [Tree(2), Tree(3)])])]), Tree("+", [Tree (0), Tree (0)])1) >>> P1 = Tree ("+", [ Tree (0), Tree ("SX") 1) >>> dtree_subst (P1, lambda d: d['$X'], T) Tree ('', [Tree ('+', [Tree(10), Tree('*, [Tree (2), Tree (3)])]), Tree(0)]) >>> T Tree('*', [Tree('+', [Tree(10), Tree ('*', [Tree(2), Tree(3)1)]), Tree (0)]) >> dtree_subst (P1, lambda d: d['$X'), T) Tree(, (Tree('+, (Tree(10), Tree(, (Tree (2), Tree (3)])]), Tree(0)]) >>> dtree_subst (P1, lambda d: d['$X'], Tree('+', [Tree ('+', [Tree(0), Tree (0)]), Tree (5)])) ... Tree (5) >> T = Tree('*, (Tree(5), Tree(10)]) >>> TO = T.branches [0] >» dtree subst (P1, lambda d: Tree (0), T) # Should not change tree. Tree('', (Tree (5), Tree(10)]) >>> TO is T.branches [0] # Check that new node not created. True for k in #3 (a) (ь) %23 d = tree_match(pattern, tree) if <same as blank (c) in the tree subst problem>: return <same as blank (d) in tree subst problem> else: return (c) i. Fill in blank (a). Fill in blank (b). iii. Fill in blank (c). I'd like to use dtree_subst to replace all subtrees of T of the form Tree('+', [Tree (A), Tree (B) ]), where A and B are integers, with Tree (C), where C is the sum of A and B. We'll assume that Trees with integer labels are leaves. I use the call iv. dtree subst (Tree('+', [Tree('$A'), Tree('$B')]), compute_subst, T) and define compute subst like this: def compute_subst (d) : A = d['$A'].label B = d['$B'].label if type (A) is int and type(B) is int return A + B else return What should replace the blank? O dl' $B'] Tree ('+', [A, B]) T Tree('+', [d['$A'], d['$B']]) [. V$.]p O Tree('+', [Tree(d['$A']), Tree (d['$B'1)])
Expert Answer:
Related Book For
Posted Date:
Students also viewed these computer engineering questions
-
In the following code, state and nextstate are integers with a range of 0 to 2. always @(state,X) begin case(state) 0:begin if(X == 1'b1) nextstate = 2'b01; end 1:begin if(X == 1'b0) nextstate =...
-
Show that if n and k are integers with 1 ¤ k ¤ n, then
-
Prove that if n and k are integers with 1 ¤ k ¤ n, then a) Using a combinatorial proof. b) Using an algebraic proof based on the formula for (nr) given in Theorem 2 in Section 6.3. k...
-
Which of the units listed in Multiple-Choice Question 9 are appropriate for the biologically equivalent dose that results when a person is exposed to radiation? Data From Question 9 Which of these...
-
Explain how the specialization of labor can lead to increased productivity.
-
Suppose a realtor is interested in comparing the asking prices of midrange homes in Peoria, Illinois, and Evansville, Indiana. The realtor conducts a small telephone survey in the two cities, asking...
-
Its not like what youve done has really hurt anyone. At most, the missing money might cost earnings-pershare, maybe a penny. Is that what caused you to write those checks in the name of your company?
-
Eisler Corporation issued 2,000 $1,000 bonds at 101. Each bond was issued with one detachable stock warrant. After issuance, the bonds were selling in the market at 98, and the warrants had a market...
-
Write a program the reads from the user a real number. Then your program displays 2 choices to the user. 1- get the square of input 2- get square root of input Based on the user's choice, the program...
-
Brian and Corrine Lee are married taxpayers filing jointly. They live in the home they own, located at 3301 Pacific Coast Hwy., Laguna Beach, CA 92651. Brian is an optometrist who owns his business;...
-
JK Limited has prepared a budget for the next 12 months when it intends to make and sell four products, details of which are shown below: Variable cost Selling price per unit () Sales in units...
-
Melinda purchased Verizon Stock at $51 per share on January 1, 2019 and sold it at $51 per share on January 1, 2021. Verizon stock paid a 6% dividend yield. What is Melinda's total return?
-
The cost of ARV drugs and medical supplies per patient per year is $150. Staff salaries to operate the clinic are $127,305 per year and are fixed as long as the clinic serves 1,125 patients or less...
-
The marketing research department for a company that manufactures and sells computers established the following price- demand and cost functions. p(x)=2379-66x where p(x) is the wholesale price per...
-
Judy and Lucy are planning to have a bakery store. They calculated and found that the initial investment is $55,000, property lease is $5,500 every month, baking materials are $3,000 every month, and...
-
Show that f(x) = x + 2x + 5x + 100 is O(x3). b) If f(n) = O(g(n)) then g(n) counter example. = O(f(n)). Is this true? If not, prove it with a c) What is the time-complexity of the following procedure...
-
Good business analysts can differentiate between poor and great business investments and IT improvements. They can also help to resolve issues without jumping to premature conclusions. Competence has...
-
Use of the contraceptive Depo Provera appears to triple women's risk of infection with chlamydia and gonorrhea , a study reports today. An estimated 20 million to 30 million women worldwide use Depo...
-
Let y = y(x) = x - x3 / 3! + x5 / 5! - x7 / 7! + .... Show that y satisfies that differential equation y" + y = 0 with the conditions y(0) = 0 and y'(0) = 1. From this guess a simple formula for y?
-
If = 46 and = 108 are direction angles for a vector u, find two possible values for the third angle.
-
A hot-air balloon left the ground rising at 4 feet per second. Sixteen seconds later, Victoria threw a ball straight up to her friend Colleen in the balloon. At what speed did she throw the ball if...
-
A gene is a segment of DNA that contains the information to produce a functional product. The functional product of most genes is a. DNA. b. mRNA. c. a polypeptide. d. none of the above.
-
Which of the following is not a constituent of a cells proteome? a. An enzyme b. A cytoskeletal protein c. A transport protein in the plasma membrane d. An mRNA
-
The function of the genetic code is to a. promote transcription. b. specify the amino acids within a polypeptide. c. alter the sequence of DNA. d. do none of the above.
Study smarter with the SolutionInn App