Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- # linked list class representing hiearchy and selectors
- debug = True
- def print_debug(input):
- if debug:
- print(input)
- class Selector:
- def __init__(self, tag, id, classes):
- # we do empty string checks in constructor for convenience of constructing selectors
- self.classes = None
- self.tag = None
- self.id = None
- if not tag and not id and not classes:
- print("nothing to select")
- if tag:
- self.tag = tag
- if id:
- self.id = id
- if classes:
- self.classes = classes
- def match(self, other):
- if not other:
- return false
- match = True
- if self.tag:
- match &= self.tag == other.tag
- if self.id:
- match &= self.id == other.id
- if self.classes:
- if not other.classes:
- return False
- match &= set(self.classes) <= set(other.classes) # python is literally pseudocode. check subset or equality
- return match
- def __repr__(self):
- return "Selector:[tag: {}, id: {}, classes {}]".format(self.tag, self.id, self.classes)
- class DOM(Selector):
- def __init__(self, tag):
- if not tag:
- print("Tag can't be null for DOM object")
- self.tag = tag
- self.children = None
- def parse_selector(string_rep):
- selector_hierarchy = string_rep.split(" ") # hierarchy is based on split lines
- selector_chain = []
- for elem in selector_hierarchy:
- tag, hashbrown, after_brown = elem.partition("#")
- id, dot, all_the_classes = after_brown.partition(".")
- classes = []
- while all_the_classes:
- class_to_add, dotty, rest = all_the_classes.partition(".")
- if class_to_add:
- classes.append(class_to_add)
- all_the_classes = rest
- selector_chain.append(Selector(tag, id, classes))
- return selector_chain
- def parse_dom(input_node):
- tag = input_node["tag"]
- classes = input_node["classes"] if "classes" in input_node else None
- id = input_node["id"] if "id" in input_node else None
- children = None
- if "children" in input_node:
- children = [parse_dom(elem) for elem in input_node["children"]]
- dom_to_return = DOM(tag)
- dom_to_return.classes = classes
- dom_to_return.id = id
- dom_to_return.children = children
- return dom_to_return
- def iterative_DFS_matcher(dom_tree, selector):
- counter = 0
- stack = [dom_tree]
- curr_selector_depth = 0
- tombstone = "tombstone" # when we pop this, it means that we gotta go up the selector hierarchy by 1"
- while stack:
- elem_to_visit = stack.pop()
- print_debug("visiting: {}".format(elem_to_visit))
- if elem_to_visit == tombstone:
- curr_selector_depth -= 1
- print_debug("got tombstone, decrementing current selector_depth to : {}".format(curr_selector_depth))
- else:
- if selector[curr_selector_depth].match(elem_to_visit):
- if curr_selector_depth == len(selector) - 1:
- print_debug("got full hit on selector: {} full selector: {}".format(selector[curr_selector_depth], selector))
- counter += 1
- else:
- print_debug("got partial hit on selector: {} full selector: {}".format(selector[curr_selector_depth], selector))
- curr_selector_depth +=1
- # add tombstone indicating that if you pop this you gotta go back up the selector chain
- stack.append(tombstone)
- stack = stack + elem_to_visit.children if elem_to_visit.children else stack
- return counter
- # get the input
- read_input = ""
- # while(True):
- # try:
- # new_line = input()
- # read_input = read_input + new_line + "\n"
- # except EOFError:
- # break
- with open('input004.txt', 'r') as myfile:
- read_input=myfile.read().replace('\n', '')
- json_map = json.loads(read_input)
- # build the selectors list
- selectors_list = [parse_selector(element) for element in json_map["tests"]]
- dom_tree = parse_dom(json_map["hierarchy"])
- print_debug("selectors list: {}".format(selectors_list))
- print_debug("dom_tree: {}".format(dom_tree))
- # for some fucky reason this test case is not formatting the strings correctly sigh
- output = [iterative_DFS_matcher(dom_tree, elem) for elem in selectors_list]
- print("[" + ",".join(str(elem) for elem in output) + "]")
- # print_debug("let's go select some stuff: {}".format(iterative_DFS_matcher(dom_tree, selectors_list[0])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement