Best Python code snippet using ATX
transformer.py
Source:transformer.py  
...43                break4445    @transform46    def SPECNUM(self, node):47        binnum = find_node(node, self.token.Binnumber)48        if binnum:49            return self.fn.CallFunc("Bin",[self.fn.Number(binnum[1][2:])])50        else:51            hexnum = self.fn.String(' '.join([item[1][2:] for item in find_all(node, self.token.Hexnumber)]))52            return self.fn.CallFunc("Hex", [hexnum])5354    @transform55    def p4d_attr_access(self, node):56        "p4d_attr_access: '@' ( NAME | '*') ->  attribute(NAME) | attributes() "57        name = find_node(node, self.token.NAME)58        if name:59            return self.fn.CallFunc("attribute", [self.fn.String(name[1])])60        else:61            return self.fn.CallFunc("attributes", [])6263    def p4d_string(self, p4dnode):64        return "'''"+str(p4dnode)+"'''"6566    def build_p4d_compound_stmt(self, node, xmlnode = None):67        "p4d_compound_stmt: [NAME '='] p4d_element ':' p4d_suite"68        element = self.build_p4d_element(find_node(node, self.symbol.p4d_element))69        nodes    = self.build_p4d_suite(find_node(node, self.symbol.p4d_suite), element)70        if xmlnode:71            xmlnode.children.extend(nodes)72        else:73            xmlnode = nodes[0]74        return xmlnode7576    def build_p4d_name(self, node):77        "p4d_name: NAME (':' NAME)*"78        return ":".join([name[1] for name in find_all(node, self.token.NAME) ])7980    def build_p4d_element(self, node):81        "p4d_element: p4d_name ['(' [p4d_attribute_list] ')']"82        tag = self.build_p4d_name(find_node(node, self.symbol.p4d_name))83        xmlnode = P4DNode(tag, attrs = {})84        _attrlist = find_node(node, self.symbol.p4d_attribute_list)85        if _attrlist:86            _attributes = find_all(_attrlist, self.symbol.p4d_attribute, depth = 1)87            attrs = {}88            for attr in _attributes:89                attr_name, attr_value = self.build_p4d_attribute(attr)90                attrs[attr_name] = attr_value91            xmlnode.attrs.update(attrs)92        return xmlnode9394    def build_p4d_attribute(self, node):95        "p4d_attribute: p4d_name '=' ['&'] test"96        attr_name  = self.build_p4d_name(find_node(node, self.symbol.p4d_name))97        n_value    = find_node(node, self.symbol.test)98        if find_node(node, self.token.AMPER, depth = 1):99            attr_value = "evaltostr_%s"%self.langlet.unparse(n_value)100        elif is_supersimple(n_value):101            S = find_node(n_value, self.token.STRING)102            if S:103                attr_value = S[1][1:-1]104            else:105                S = find_node(n_value, self.token.NUMBER)106                if S:107                    attr_value = "evaltostr_%s"%self.langlet.unparse(n_value)108                else:109                    _specnum = find_node(n_value, self.symbol.SPECNUM)110                    if _specnum:111                        attr_value = "evaltoobj_%s"%self.langlet.unparse(self.SPECNUM(_specnum))112                    else:113                        raise SyntaxError("P4D attribute value `%s` must be prefixed with &-operator for evaluation."%self.langlet.unparse(n_value))114        else:115            print "N_VALUE", n_value116            raise SyntaxError("P4D attribute value of `%s` must be prefixed with &-operator for evaluation."%self.langlet.unparse(node))117        return attr_name, attr_value118119    def build_p4d_simple_stmt(self, node):120        "p4d_simple_stmt: (p4d_element | p4d_expr) NEWLINE"121        _p4d_element = find_node(node, self.symbol.p4d_element, depth = 1)122        if _p4d_element:123            return self.build_p4d_element(_p4d_element)124        else:125            return self.build_p4d_expr(find_node(node, self.symbol.p4d_expr))126127    def build_p4d_stmt(self, node, xmlnode):128        "p4d_stmt: p4d_simple_stmt | p4d_compound_stmt"129        if is_node(node[1], self.symbol.p4d_simple_stmt):130            res = self.build_p4d_simple_stmt(node[1])131            if isinstance(res, P4DNode):132                xmlnode.children.append(res)133                return [xmlnode]134            elif res.startswith("evaltoobj_"):135                xmlnode.children.append(res)136                return [xmlnode]137            else:138                xmlnode.text = res139                return [xmlnode]140        else:141            xmlnode = self.build_p4d_compound_stmt(node[1], xmlnode)142            return [xmlnode]143144    def build_p4d_suite(self, node, xmlnode):145        "p4d_suite: p4d_expr | NEWLINE INDENT p4d_stmt+ DEDENT"146        if is_node(node[1], self.symbol.p4d_expr):147            return self.build_p4d_stmt([self.symbol.stmt, [self.symbol.p4d_simple_stmt]+node[1:]], xmlnode)148        else:149            nodes = []150            _stmts = find_all(node, self.symbol.p4d_stmt, depth = 1)151            for _stmt in _stmts:152                self.build_p4d_stmt(_stmt, xmlnode)153            return [xmlnode]154155    def build_p4d_expr(self, node):156        "p4d_expr: '&' ['&'] test | '(' [ p4d_expr (',' p4d_expr)] ')' | STRING | NUMBER | SPECNUM | P4D_Comment"157        _test = find_node(node, self.symbol.test, depth = 1)158        if _test:159            if node[2][0] == self.token.AMPER:160                name = find_node(_test, self.token.NAME)[1]161                return "deferredeval_lambda %s: %s"%(name, self.langlet.unparse(_test))162            return "evaltoobj_"+self.langlet.unparse(_test)163        _expr_list = find_all([self.symbol.p4d_simple_stmt]+node[1:], self.symbol.p4d_expr, depth = 1)164        if _expr_list:165            return "evaltoobj_"+str([self.build_p4d_expr(item) for item in _expr_list])166        p4d_comment = find_node(node, self.token.P4D_Comment, depth = 1)167        if p4d_comment:168            comment = self.langlet.unparse(node)[2:-2]169            if comment[0] == '*' and comment[-1] == '*':170                comment  = flatstring(comment[1:-1])171                p4d_node = P4DNode("**")172            else:173                p4d_node = P4DNode("*")174                comment   = flatstring(comment)175            p4d_node.text = comment176            return p4d_node177        _str = find_node(node, self.token.STRING, depth = 1)178        if _str:179            return flatstring(node[1][1])180        _specnum = find_node(node, self.symbol.SPECNUM,depth =1)181        if _specnum:182            return "evaltoobj_%s"%self.langlet.unparse(self.SPECNUM(_specnum))183        _number = find_node(node, self.token.NUMBER, depth =1)184        if _number:185            return "evaltonum_"+node[1][1]186        raise TypeError("no P4D object content or attribute: `%s`."%self.langlet.unparse(node))187188    @transform189    def NAME(self, node):190        name = node[1]191        if '-' in name:192            chain = self.node_stack(node)193            nd, chain = chain.step()194            nid = nd[0]195            if nid in (self.symbol.p4d_attr_access,196                       self.symbol.p4d_name,197                       self.symbol.p4d_accessor):198                return199            elif nid == self.symbol.atom:200                difference = ' - '.join(name.split('-'))201                chain = self.node_stack(node)202                nd, chain = chain.step()  # power203                nd, chain = chain.step()  # factor204                nd, chain = chain.step()  # term205                nd, chain = chain.step()  # arith_expr206                txt = self.langlet.unparse(nd)207                txt = txt.replace(name, difference)208                return find_node(self.langlet.parse(txt), self.symbol.arith_expr)209            else:210                raise SyntaxError("invalid syntax. No hyphenation permitted in Python names.")211        elif '::' in name:212            chain = self.node_stack(node)213            _trailer, chain = chain.step() # trailer214            if _trailer[0]!=self.symbol.trailer:215                raise SyntaxError("invalid use of accessor symbol '::'")216            nd, chain = chain.step() # power217            prefix, local_name = name.split("::")218            accessor = cstnode([self.symbol.p4d_accessor,219                               [self.token.DOUBLECOLON, '::'],220                               [self.token.NAME, local_name]])221            id_trailer = id(_trailer)222            for i, item in enumerate(nd):223                if id(item) == id_trailer:224                    del nd[i]225                    nd.insert(i,[self.symbol.trailer, accessor])226                    nd.insert(i, self.fn.trailer(".", self.fn.Name(prefix)))227                    break228            self.mark_node(nd)229            return nd230231232    @transform233    def p4d_accessor(self, node):234        "p4d_accessor: '.' p4d_attr_access | '::' NAME | '.' '(' ['.'] test ')'"235        _attr = find_node(node, self.symbol.p4d_attr_access, depth = 1)236        if _attr:237            self.unmark_node(_attr)238            _call = self.p4d_attr_access(_attr)239            name  = find_node(_call, self.token.NAME)[1]240            return self.fn.trailer(".", name), find_node(_call, self.symbol.trailer)241        elif find_node(node, self.token.LPAR, depth = 1):242            # .(@x == A)  ==> lambda this: this.attribute("x") == str(A)243            # .(x == A)   ==> lambda this: any(x.text() == A for x in this.x)244            # .(X op Y)   ==> lambda this: T(X) op T(Y)245            # .(x.X == A) ==> lambda this: any(x.X == A for x in this.x)246            # .(x::y == A) ==> lambda this: any(item.text() == A for item in this.children("x:y"))247            # .(@x::y == A) ==> lambda this: this.attribute("x:y") == str(A)248            if is_node(node[3], self.token.DOT):249                _test = find_node(node, self.symbol.test)  # child node250                _test[:] = find_node(self.langlet.parse("_."+self.langlet.unparse(_test)), self.symbol.test)251                filter_type = 3252            else:253                filter_type = 2  # this node254            body = find_node(node, self.symbol.test)255            self.unmark_node(body, self.token.NAME)256            _not_tests = find_all(body, self.symbol.not_test, depth = 3)257            for nt in _not_tests:258                _comparison = find_node(nt, self.symbol.comparison)259                n = len(_comparison)260                if n>4:261                    raise SyntaxError("Invalid filter `.(%s)`"%self.langlet.unparse(_comparison))262                _expr_1 = _comparison[1]263                if find_node(_expr_1, self.token.DOUBLECOLON):264                    _pow = find_node(_expr_1, self.symbol.power)265                    for i,item in enumerate(_pow):266                        if isinstance(item, list):267                            if find_node(item, self.token.DOUBLECOLON):268                                _name = find_node(_pow[i-1], self.token.NAME)269                                _name[1] = _name[1]+':'+find_node(item, self.token.NAME)[1]270                                del _pow[i]271                                break272                self.run(_expr_1)273                name = find_node(_expr_1, self.token.NAME)[1]274                if n>2:275                    s_comp   = self.langlet.unparse(_comparison[2])276                    s_expr_2 = self.langlet.unparse(_comparison[3])277                s_expr_1 = self.langlet.unparse(_expr_1)278                if name == "attribute":279                    filter_type = 1  # attribute280                    if n == 4:281                        _expr = find_node(self.langlet.parse("(this.%s %s str(%s))"%(s_expr_1, s_comp, s_expr_2)), self.symbol.expr)282                    else:283                        _expr = find_node(self.langlet.parse("(this.%s)"%(s_expr_1,)), self.symbol.expr)284                else:285                    if s_expr_1 == name:286                        if n == 4:287                            if name != '_':288                                _expr = find_node(self.langlet.parse("(this.text() %s %s and this.tag == '%s')"%(s_comp, s_expr_2, name)), self.symbol.expr)289                            else:290                                _expr = find_node(self.langlet.parse("(this.text() %s %s)"%(s_comp, s_expr_2)), self.symbol.expr)291                        else:292                            _expr = find_node(self.langlet.parse("(this.tag == '%s')"%(name,)), self.symbol.expr)293                    else:294                        s_expr_1 = s_expr_1.replace(name, "this", 1)295                        if n == 4:296                            if name !='_':297                                _expr = find_node(self.langlet.parse("(%s %s %s and this.tag == '%s')"%(s_expr_1, s_comp, s_expr_2, name)), self.symbol.expr)298                            else:299                                _expr = find_node(self.langlet.parse("(%s %s %s)"%(s_expr_1, s_comp, s_expr_2)), self.symbol.expr)300                        else:301                            if name !='_':302                                _expr = find_node(self.langlet.parse("(%s and this.tag == '%s')"%(s_expr_1, name)), self.symbol.expr)303                            else:304                                _expr = find_node(self.langlet.parse("(%s)"%(s_expr_1,)), self.symbol.expr)305                del _comparison[1:]306                _comparison.append(_expr)307                _inner = self.fn.Lambda(body, ["this"])308            _lambda = self.fn.Lambda( self.fn.Tuple(filter_type, _inner),[])309            return self.fn.trailer(".", "search"), \310                   self.fn.trailer("(", self.fn.arglist(self.fn.argument(self.fn.test(_lambda))),")")311        elif find_node(node, self.fn.token.DOUBLECOLON):312            # .X :: Y313            locname = find_node(node, self.fn.token.NAME)[1]314            chain = self.node_stack(node)315            nd, chain = chain.step() # trailer316            nd, chain = chain.step() # power317            j = 1000318            for i, item in enumerate(nd[1:]):319                if find_node(item, self.fn.token.DOUBLECOLON):320                    break321                if find_node(item, self.fn.symbol.arglist):322                    j = i323            if j == i-1:324                _str = find_node(nd, self.fn.token.STRING)325                _str[1] = "'"+_str[1][1:-1]+":"+locname+"'"326                del nd[i+1]327            else:328                prefix  = find_node(nd[i], self.fn.token.NAME)[1]329                nd[i]   = self.fn.trailer(".", "child")330                nd[i+1] = self.fn.trailer("(",331                            self.fn.arglist(332                                self.fn.argument(333                                    self.fn.test(334                                        self.fn.String(prefix+":"+locname)))),335                                        ")")336            return nd337338339    @transform #_dbg("sn")340    def p4d_compound_stmt(self, node):341        "p4d_compound_stmt: ['elm' | NAME '='] p4d_element ':' p4d_suite"342        self.unmark_node(node)343        # transform this into a comma separated list of items if two or more entries are found344        # otherwise return just this entry345        # chain = self.node_stack(node)346347        p4d_node = self.build_p4d_compound_stmt(node)348        S = self.p4d_string(p4d_node)349350        if p4d_node.tag.startswith("bl:"):351            tagwrapper = self.fn.test(352                                self.fn.CallFunc("Bytelet",353                                    [self.fn.CallFunc("mapeval",[354                                            [self.token.STRING,S],355                                                self.fn.CallFunc("globals",[]),356                                                self.fn.CallFunc("locals",[])])]))357358        elif p4d_node.tag.startswith("bl-schema:"):359            tagwrapper = self.fn.test(360                            self.fn.CallFunc("ByteletSchema",361                                    [self.fn.CallFunc("mapeval",[362                                        [self.token.STRING,S],363                                            self.fn.CallFunc("globals",[]),364                                            self.fn.CallFunc("locals",[])])]))365        else:366            tagwrapper = self.fn.test(367                            self.fn.CallFunc("P4D",368                                [self.fn.CallFunc("mapeval",[369                                    [self.token.STRING,S],370                                    self.fn.CallFunc("globals",[]),371                                    self.fn.CallFunc("locals",[])])]))372373        _name = find_node(node, self.token.NAME, depth = 1)374        if _name:375            return self.fn.stmt(self.fn.Assign(_name, tagwrapper))376        if find_node(node, self.keyword["elm"], depth = 1):377            name_fragments = find_all(find_node(node, self.symbol.p4d_name), self.token.NAME)378            if len(name_fragments) == 1:379                obj_name = name_fragments[0][1]380            else:381                prefix, obj_name = name_fragments[0][1],name_fragments[1][1]382            if obj_name in self.keyword:383                raise SyntaxError("invalid syntax. Keyword cannot be used as name. Use explicit assignment instead.")384            if '-' in obj_name:385                raise SyntaxError("invalid syntax. Hyphenated name cannot be used as Python name. Use explicit assignment instead.")386            assignment = self.fn.Assign(obj_name, tagwrapper)387            return self.fn.stmt(assignment)388        else:389            return self.fn.stmt(tagwrapper)390391
...test_graph.py
Source:test_graph.py  
...33    nid = g.create()34    assert nid > 235def test_graph_add_single_default():36    nid = g.create()37    node = g.find_node(nid)38    assert(type(node) is Node)39def test_graph_add_multiple_default():40    assert len(g.get_nodes()) == 241    nid1 = g.create()42    nid2 = g.create()43    assert len(g.get_nodes()) == 444    assert (g.find_node(nid1) is not None)45    assert (g.find_node(nid2) is not None)46def test_graph_add_subtype_BMU():47    nid = g.create(node_type=BMU)48    assert (type(g.find_node(nid)) is BMU)49def test_graph_add_subtype_calibrate():50    nid = g.create(node_type=Calibrate)51    assert (type(g.find_node(nid)) is Calibrate)52def test_graph_add_subtype_concatenate():53    props = dict()54    props["axis"] = 055    nid = g.create(node_type=Concat, props=props)56    assert (type(g.find_node(nid)) is Concat)57def test_graph_add_subtype_distance():58    nid = g.create(node_type=Dist)59    assert (type(g.find_node(nid)) is Dist)60def test_graph_add_subtype_input_container():61    nid = g.create(node_type=InputContainer)62    assert (type(g.find_node(nid)) is InputContainer)63def test_graph_add_subtype_som():64    props = dict()65    props["size"] = 166    props["dim"] = 167    nid = g.create(node_type=SOM, props=props)68    assert (type(g.find_node(nid)) is SOM)69def test_graph_custom_id_basic():70    nid = g.create_with_id(10)71    assert (nid == 10)72    assert (g.find_node(10) is not None)73def test_graph_custom_id_override_inbuilts():74    nid1 = g.create_with_id(1)  # Start75    nid2 = g.create_with_id(2)  # End76    assert (len(g.get_nodes()) == 2)77    assert (nid1 == -1)78    assert (nid2 == -1)79    assert (g.find_node(nid1) is None)80    assert (g.find_node(nid2) is None)81def test_graph_custom_id_duplicate():82    nid1 = g.create_with_id(3)  # Start83    nid2 = g.create_with_id(3)  # End84    assert (nid1 != -1)85    assert (nid2 == -1)86    assert (g.find_node(nid1) is not None)87    assert (g.find_node(nid2) is None)88    assert (len(g.get_nodes()) == 3)89"""90Getter and Setter Tests91"""92def test_find_node_good():93    nid = g.create(node_type=Node)94    node = g.find_node(nid)95    assert (node is not None)96def test_find_node_bad():97    node = g.find_node(-1)98    assert (node is None)99def test_get_nodes_basic():100    nid = g.create(node_type=Node)101    nodes = g.get_nodes()102    assert (len(nodes) == 3)103    node = g.find_node(nid)104    assert (nid in nodes.keys())105    assert (nodes[nid] is node)106def test_get_nodes_multiple():107    nid1 = g.create()108    nid2 = g.create()109    nodes = g.get_nodes()110    assert (len(nodes) == 4)111    node1 = g.find_node(nid1)112    node2 = g.find_node(nid2)113    assert (nid1 in nodes.keys())114    assert (nid2 in nodes.keys())115    assert (nodes[nid1] is node1)116    assert (nodes[nid2] is node2)117def test_extract_output_untrained():118    g.create_with_id(100)119    g.set_input(g.find_node(100))120    g.connect(g.start, g.end, 1)121    end_out = g.get_output(1)122    assert (end_out is not None)  # return type is not strict123def test_extract_output_bad_slot():124    end_out = g.get_output(1)125    assert (end_out is None)126def test_extract_output_identity():127    end_out = g.get_output(0)128    assert (end_out is g.find_node(g.end))129def test_set_param():130    try:131        g.set_param("the truth", True)132    except Exception:133        pytest.fail("Should have passed")134def test_set_param_empty():135    try:136        g.set_param("", None)137    except Exception:138        pytest.fail("Should have passed")139def test_set_param_nonstring():140    with pytest.raises(Exception):141        g.set_param(1, "one")142"""143Connection Tests144"""145def test_graph_connect_default_good():146    assert (g.connect(g.start, g.end, 1) is True)147def test_graph_connect_default_self():148    assert (g.connect(g.start, g.start, 1) is False)149    assert (g.connect(g.end, g.end, 1) is False)150def test_graph_connect_new_self():151    nid = g.create()152    assert (g.connect(nid, nid, 1) is False)153def test_graph_connect_invalid():154    assert (g.connect(g.start, -1, 1) is False)155    assert (g.connect(-1, g.end, 1) is False)156    assert (g.connect(-1, -1, 1) is False)157def test_graph_backwards():158    assert (g.connect(g.end, g.start, 1) is True)159# This should be mocked honestly...160def test_bad_connection_default_node():161    nid = g.create()162    assert (g.connect(nid, g.end, 2) is False)  # Node requires 0 or 1163def test_graph_add_connect_single_path():164    nid = g.create()165    start = g.find_node(g.start)166    mid = g.find_node(nid)167    end = g.find_node(g.end)168    assert (len(start.get_incoming()) == 0)169    assert (len(mid.get_incoming()) == 0)170    assert (len(end.get_incoming()) == 0)171    assert (g.connect(g.start, nid, 1) is True)172    assert (g.connect(nid, g.end, 1) is True)173    assert (len(start.get_incoming()) == 0)174    assert (len(mid.get_incoming()) == 1)175    assert (len(end.get_incoming()) == 1)176    assert (len(g.get_nodes()) == 3)177def test_graph_add_connect_skip_new():178    nid = g.create()179    start = g.find_node(g.start)180    mid = g.find_node(nid)181    end = g.find_node(g.end)182    assert (mid is not None)183    assert (len(start.get_incoming()) == 0)184    assert (len(mid.get_incoming()) == 0)185    assert (len(end.get_incoming()) == 0)186    assert (g.connect(g.start, g.end, 1) is True)187    assert (len(start.get_incoming()) == 0)188    assert (len(mid.get_incoming()) == 0)189    assert (len(end.get_incoming()) == 1)190    assert (len(g.get_nodes()) == 3)191def test_graph_add_multiple_long_path():192    nid1 = g.create()193    nid2 = g.create()194    nid3 = g.create()195    nid4 = g.create()196    node1 = g.find_node(nid1)197    node2 = g.find_node(nid2)198    node3 = g.find_node(nid3)199    node4 = g.find_node(nid4)200    assert (node1 is not None)201    assert (node2 is not None)202    assert (node3 is not None)203    assert (node4 is not None)204    assert (len(g.get_nodes()) == 6)205    assert (g.connect(g.start, nid1, 1) is True)206    assert (g.connect(nid1, nid2, 1) is True)207    assert (g.connect(nid2, nid3, 1) is True)208    assert (g.connect(nid3, nid4, 1) is True)209    assert (g.connect(nid4, g.end, 1) is True)210    start = g.find_node(g.start)211    end = g.find_node(g.end)212    assert (len(start.get_incoming()) == 0)213    assert (len(node1.get_incoming()) == 1)214    assert (len(node2.get_incoming()) == 1)215    assert (len(node3.get_incoming()) == 1)216    assert (len(node4.get_incoming()) == 1)217    assert (len(end.get_incoming()) == 1)218def test_graph_branched_paths():219    nid1 = g.create()220    nid2 = g.create()221    nid3 = g.create()222    nid4 = g.create()223    node1 = g.find_node(nid1)224    node2 = g.find_node(nid2)225    node3 = g.find_node(nid3)226    node4 = g.find_node(nid4)227    assert (node1 is not None)228    assert (node2 is not None)229    assert (node3 is not None)230    assert (node4 is not None)231    assert (len(g.get_nodes()) == 6)232    assert (g.connect(g.start, nid1, 1) is True)233    assert (g.connect(g.start, nid2, 1) is True)234    assert (g.connect(nid1, nid3, 1) is True)235    assert (g.connect(nid2, nid4, 1) is True)236    assert (g.connect(nid3, g.end, 1) is True)237    assert (g.connect(nid4, g.end, 1) is True)238    start = g.find_node(g.start)239    end = g.find_node(g.end)240    assert (len(start.get_incoming()) == 0)241    assert (len(node1.get_incoming()) == 1)242    assert (len(node2.get_incoming()) == 1)243    assert (len(node3.get_incoming()) == 1)244    assert (len(node4.get_incoming()) == 1)...problem3.py
Source:problem3.py  
1#This algorithm is designed to find the median of two sorted AVL trees containing only positive integers.2#The function find_node has a worst case time of O(log(n)) and the function median has a worst case time complexity of O(log(n)^2)3#Both algorithms assume that both AVL trees maintain AVL form via self-balancing techniques.4def find_node(k):5    count = 06    if root == None:7        return "Empty tree"8    if root.left != None:9        return root.left10    count += 111    if count == k:12        return root13    return find_node(root.right, k)14def median(A, B):15    median_point = (A.root.subtree_value + B.root.subtree_value + 1) / 216    start = 017    if A.root.subtree_value < B.root.subtree_value:18        while True:19            end = A.root.subtree_value20            partitionA = (end + start) / 221            partitionB = median_point - partitionA22            if (A.find_node(partitionA - 1) <= B.find_node(partitionB)) and (B.find_node(partitionB - 1) <= A.find_node(partitionA)):23                return max(A.find_node(partitionA - 1), B.find_node(partitionB - 1))24            elif A.find_node(partitionA - 1) > B.find_node(partitionB):25                start = partitionA - 126            else:27                start = partitionA + 128    else:29        while True:30            end = B.root.subtree_value31            partitionB = (end + start) / 232            partitionA = median_point - partitionB33            if (B.find_node(partitionB - 1) <= A.find_node(partitionA)) and (A.find_node(partitionA - 1) <= B.find_node(partitionB)):34                return max(A.find_node(partitionA - 1), B.find_node(partitionB - 1))35            elif B.find_node(partitionB - 1) > A.find_node(partitionA):36                start = partitionB - 137            else:...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
