How to use descendants method in hypothesis

Best Python code snippet using hypothesis

youngest_common_ancestor.py

Source:youngest_common_ancestor.py Github

copy

Full Screen

1import unittest2"""3Given three inputs, all of which are instances of an AncestralTree class that have an4ancestor property pointing to their youngest ancestor. The first input is the top ancestor5in an ancestral tree (i.e., the only instance that has no ancestor -- its ancestor property6points to None / Null), and the other two inputs are descendants in the ancestral tree.7The function returns the youngest common ancestor to the two descendants.8Note that a descendant is considered its own ancestor. So in the simple ancestral tree below,9the youngest common ancestor to nodes A and B is node A.10 A11 /12 B13"""14class AncestralTree:15 def __init__(self, name):16 self.name = name17 self.ancestor = None18 def addDescendants(self, *descendants):19 for descendant in descendants:20 descendant.ancestor = self21# Helper function22def new_trees():23 ancestralTrees = {}24 for letter in list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):25 ancestralTrees[letter] = AncestralTree(letter)26 return ancestralTrees27# O(d) time | O(1) space - where d is depth (height) of the ancestral tree28def getYoungestCommonAncestor(topAncestor, descendantOne, descendantTwo):29 depthOne = getDescendantDepth(descendantOne, topAncestor)30 depthTwo = getDescendantDepth(descendantTwo, topAncestor)31 if depthOne > depthTwo:32 return backtrackAncestralTree(descendantOne, descendantTwo, depthOne - depthTwo)33 else:34 return backtrackAncestralTree(descendantTwo, descendantOne, depthTwo - depthOne)35def getDescendantDepth(descendant, topAncestor):36 depth = 037 while descendant != topAncestor:38 descendant = descendant.ancestor39 depth += 140 return depth41def backtrackAncestralTree(lowerDescendant, higherDescendant, diff):42 while diff > 0:43 lowerDescendant = lowerDescendant.ancestor44 diff -= 145 while lowerDescendant != higherDescendant:46 lowerDescendant = lowerDescendant.ancestor47 higherDescendant = higherDescendant.ancestor48 49 return lowerDescendant50class TestProgram(unittest.TestCase):51 def test_case_1(self):52 trees = new_trees()53 trees["A"].addDescendants(trees["B"], trees["C"])54 trees["B"].addDescendants(trees["D"], trees["E"])55 trees["D"].addDescendants(trees["H"], trees["I"])56 trees["C"].addDescendants(trees["F"], trees["G"])57 yca = getYoungestCommonAncestor(trees["A"], trees["E"], trees["I"])58 self.assertTrue(yca == trees["B"])59 def test_case_2(self):60 trees = new_trees()61 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])62 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])63 trees["C"].addDescendants(trees["J"])64 trees["D"].addDescendants(trees["K"], trees["L"])65 trees["F"].addDescendants(trees["M"], trees["N"])66 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])67 trees["K"].addDescendants(trees["S"])68 trees["P"].addDescendants(trees["T"], trees["U"])69 trees["R"].addDescendants(trees["V"])70 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])71 trees["X"].addDescendants(trees["Z"])72 yca = getYoungestCommonAncestor(trees["A"], trees["A"], trees["B"])73 self.assertTrue(yca == trees["A"])74 def test_case_3(self):75 trees = new_trees()76 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])77 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])78 trees["C"].addDescendants(trees["J"])79 trees["D"].addDescendants(trees["K"], trees["L"])80 trees["F"].addDescendants(trees["M"], trees["N"])81 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])82 trees["K"].addDescendants(trees["S"])83 trees["P"].addDescendants(trees["T"], trees["U"])84 trees["R"].addDescendants(trees["V"])85 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])86 trees["X"].addDescendants(trees["Z"])87 yca = getYoungestCommonAncestor(trees["A"], trees["B"], trees["F"])88 self.assertTrue(yca == trees["A"])89 def test_case_4(self):90 trees = new_trees()91 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])92 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])93 trees["C"].addDescendants(trees["J"])94 trees["D"].addDescendants(trees["K"], trees["L"])95 trees["F"].addDescendants(trees["M"], trees["N"])96 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])97 trees["K"].addDescendants(trees["S"])98 trees["P"].addDescendants(trees["T"], trees["U"])99 trees["R"].addDescendants(trees["V"])100 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])101 trees["X"].addDescendants(trees["Z"])102 yca = getYoungestCommonAncestor(trees["A"], trees["G"], trees["M"])103 self.assertTrue(yca == trees["A"])104 def test_case_5(self):105 trees = new_trees()106 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])107 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])108 trees["C"].addDescendants(trees["J"])109 trees["D"].addDescendants(trees["K"], trees["L"])110 trees["F"].addDescendants(trees["M"], trees["N"])111 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])112 trees["K"].addDescendants(trees["S"])113 trees["P"].addDescendants(trees["T"], trees["U"])114 trees["R"].addDescendants(trees["V"])115 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])116 trees["X"].addDescendants(trees["Z"])117 yca = getYoungestCommonAncestor(trees["A"], trees["U"], trees["S"])118 self.assertTrue(yca == trees["A"])119 def test_case_6(self):120 trees = new_trees()121 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])122 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])123 trees["C"].addDescendants(trees["J"])124 trees["D"].addDescendants(trees["K"], trees["L"])125 trees["F"].addDescendants(trees["M"], trees["N"])126 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])127 trees["K"].addDescendants(trees["S"])128 trees["P"].addDescendants(trees["T"], trees["U"])129 trees["R"].addDescendants(trees["V"])130 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])131 trees["X"].addDescendants(trees["Z"])132 yca = getYoungestCommonAncestor(trees["A"], trees["Z"], trees["M"])133 self.assertTrue(yca == trees["A"])134 def test_case_7(self):135 trees = new_trees()136 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])137 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])138 trees["C"].addDescendants(trees["J"])139 trees["D"].addDescendants(trees["K"], trees["L"])140 trees["F"].addDescendants(trees["M"], trees["N"])141 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])142 trees["K"].addDescendants(trees["S"])143 trees["P"].addDescendants(trees["T"], trees["U"])144 trees["R"].addDescendants(trees["V"])145 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])146 trees["X"].addDescendants(trees["Z"])147 yca = getYoungestCommonAncestor(trees["A"], trees["O"], trees["I"])148 self.assertTrue(yca == trees["B"])149 def test_case_8(self):150 trees = new_trees()151 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])152 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])153 trees["C"].addDescendants(trees["J"])154 trees["D"].addDescendants(trees["K"], trees["L"])155 trees["F"].addDescendants(trees["M"], trees["N"])156 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])157 trees["K"].addDescendants(trees["S"])158 trees["P"].addDescendants(trees["T"], trees["U"])159 trees["R"].addDescendants(trees["V"])160 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])161 trees["X"].addDescendants(trees["Z"])162 yca = getYoungestCommonAncestor(trees["A"], trees["T"], trees["Z"])163 self.assertTrue(yca == trees["H"])164 def test_case_9(self):165 trees = new_trees()166 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])167 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])168 trees["C"].addDescendants(trees["J"])169 trees["D"].addDescendants(trees["K"], trees["L"])170 trees["F"].addDescendants(trees["M"], trees["N"])171 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])172 trees["K"].addDescendants(trees["S"])173 trees["P"].addDescendants(trees["T"], trees["U"])174 trees["R"].addDescendants(trees["V"])175 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])176 trees["X"].addDescendants(trees["Z"])177 yca = getYoungestCommonAncestor(trees["A"], trees["T"], trees["V"])178 self.assertTrue(yca == trees["H"])179 def test_case_10(self):180 trees = new_trees()181 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])182 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])183 trees["C"].addDescendants(trees["J"])184 trees["D"].addDescendants(trees["K"], trees["L"])185 trees["F"].addDescendants(trees["M"], trees["N"])186 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])187 trees["K"].addDescendants(trees["S"])188 trees["P"].addDescendants(trees["T"], trees["U"])189 trees["R"].addDescendants(trees["V"])190 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])191 trees["X"].addDescendants(trees["Z"])192 yca = getYoungestCommonAncestor(trees["A"], trees["T"], trees["H"])193 self.assertTrue(yca == trees["H"])194 def test_case_11(self):195 trees = new_trees()196 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])197 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])198 trees["C"].addDescendants(trees["J"])199 trees["D"].addDescendants(trees["K"], trees["L"])200 trees["F"].addDescendants(trees["M"], trees["N"])201 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])202 trees["K"].addDescendants(trees["S"])203 trees["P"].addDescendants(trees["T"], trees["U"])204 trees["R"].addDescendants(trees["V"])205 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])206 trees["X"].addDescendants(trees["Z"])207 yca = getYoungestCommonAncestor(trees["A"], trees["W"], trees["V"])208 self.assertTrue(yca == trees["V"])209 def test_case_12(self):210 trees = new_trees()211 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])212 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])213 trees["C"].addDescendants(trees["J"])214 trees["D"].addDescendants(trees["K"], trees["L"])215 trees["F"].addDescendants(trees["M"], trees["N"])216 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])217 trees["K"].addDescendants(trees["S"])218 trees["P"].addDescendants(trees["T"], trees["U"])219 trees["R"].addDescendants(trees["V"])220 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])221 trees["X"].addDescendants(trees["Z"])222 yca = getYoungestCommonAncestor(trees["A"], trees["Z"], trees["B"])223 self.assertTrue(yca == trees["B"])224 def test_case_13(self):225 trees = new_trees()226 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])227 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])228 trees["C"].addDescendants(trees["J"])229 trees["D"].addDescendants(trees["K"], trees["L"])230 trees["F"].addDescendants(trees["M"], trees["N"])231 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])232 trees["K"].addDescendants(trees["S"])233 trees["P"].addDescendants(trees["T"], trees["U"])234 trees["R"].addDescendants(trees["V"])235 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])236 trees["X"].addDescendants(trees["Z"])237 yca = getYoungestCommonAncestor(trees["A"], trees["Q"], trees["W"])238 self.assertTrue(yca == trees["H"])239 def test_case_14(self):240 trees = new_trees()241 trees["A"].addDescendants(trees["B"], trees["C"], trees["D"], trees["E"], trees["F"])242 trees["B"].addDescendants(trees["G"], trees["H"], trees["I"])243 trees["C"].addDescendants(trees["J"])244 trees["D"].addDescendants(trees["K"], trees["L"])245 trees["F"].addDescendants(trees["M"], trees["N"])246 trees["H"].addDescendants(trees["O"], trees["P"], trees["Q"], trees["R"])247 trees["K"].addDescendants(trees["S"])248 trees["P"].addDescendants(trees["T"], trees["U"])249 trees["R"].addDescendants(trees["V"])250 trees["V"].addDescendants(trees["W"], trees["X"], trees["Y"])251 trees["X"].addDescendants(trees["Z"])252 yca = getYoungestCommonAncestor(trees["A"], trees["A"], trees["Z"])253 self.assertTrue(yca == trees["A"])254if __name__ == '__main__':...

Full Screen

Full Screen

test_get_descendants.py

Source:test_get_descendants.py Github

copy

Full Screen

...11 b = Normal(mu=a, sigma=1.0)12 c = Normal(mu=0.0, sigma=1.0)13 d = Normal(mu=c, sigma=1.0)14 e = Normal(mu=tf.multiply(b, d), sigma=1.0)15 self.assertEqual(set(get_descendants(a)), set([b, e]))16 self.assertEqual(get_descendants(b), [e])17 self.assertEqual(set(get_descendants(c)), set([d, e]))18 self.assertEqual(get_descendants(d), [e])19 self.assertEqual(get_descendants(e), [])20 def test_a_structure(self):21 with self.test_session():22 a = Normal(mu=0.0, sigma=1.0)23 b = Normal(mu=a, sigma=1.0)24 c = Normal(mu=b, sigma=1.0)25 d = Normal(mu=a, sigma=1.0)26 e = Normal(mu=d, sigma=1.0)27 self.assertEqual(set(get_descendants(a)), set([b, c, d, e]))28 self.assertEqual(get_descendants(b), [c])29 self.assertEqual(get_descendants(c), [])30 self.assertEqual(get_descendants(d), [e])31 self.assertEqual(get_descendants(e), [])32 def test_chain_structure(self):33 with self.test_session():34 a = Normal(mu=0.0, sigma=1.0)35 b = Normal(mu=a, sigma=1.0)36 c = Normal(mu=b, sigma=1.0)37 d = Normal(mu=c, sigma=1.0)38 e = Normal(mu=d, sigma=1.0)39 self.assertEqual(set(get_descendants(a)), set([b, c, d, e]))40 self.assertEqual(set(get_descendants(b)), set([c, d, e]))41 self.assertEqual(set(get_descendants(c)), set([d, e]))42 self.assertEqual(get_descendants(d), [e])43 self.assertEqual(get_descendants(e), [])44 def test_tensor(self):45 with self.test_session():46 a = Normal(mu=0.0, sigma=1.0)47 b = tf.constant(2.0)48 c = a + b49 d = Normal(mu=c, sigma=1.0)50 self.assertEqual(get_descendants(a), [d])51 self.assertEqual(get_descendants(b), [d])52 self.assertEqual(get_descendants(c), [d])53 self.assertEqual(get_descendants(d), [])54 def test_control_flow(self):55 with self.test_session():56 a = Bernoulli(p=0.5)57 b = Normal(mu=0.0, sigma=1.0)58 c = tf.constant(0.0)59 d = tf.cond(tf.cast(a, tf.bool), lambda: b, lambda: c)60 e = Normal(mu=d, sigma=1.0)61 self.assertEqual(get_descendants(a), [e])62 self.assertEqual(get_descendants(b), [e])63 self.assertEqual(get_descendants(c), [e])64 self.assertEqual(get_descendants(d), [e])65 self.assertEqual(get_descendants(e), [])66if __name__ == '__main__':...

Full Screen

Full Screen

generate_descendants.py

Source:generate_descendants.py Github

copy

Full Screen

1import json2import sys3from sets import Set4data = json.load(open('courses.json'))5def get_descendants(course_of_interest, checked_descendants):6 descendants = Set([])7 for course in data.keys():8 if "prerequisites" in data[course]:9 for prereq in data[course]["prerequisites"]:10 if isinstance(prereq, list):11 for subreq in prereq:12 if subreq == course_of_interest :13 descendants.add(course)14 elif prereq == course_of_interest :15 descendants.add(course)16 new_descendants = Set([])17 num_old_descendants = -118 while not (num_old_descendants == len(new_descendants)) :19 num_old_descendants = len(new_descendants)20 new_descendants = Set([])21 for x in descendants:22 if not x in checked_descendants:23 checked_descendants.add(x)24 new_descendants = new_descendants | get_descendants(x, checked_descendants)25 descendants = descendants | new_descendants26 return descendants27test = "false"28for course_of_interest in data.keys():29 descendants = get_descendants(course_of_interest, Set([]))30 if not len(descendants) == 0:31 sys.stdout.write(course_of_interest + ": ")32 for descendant in descendants :33 sys.stdout.write(descendant + " ")...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful