Skip to content

Commit 5596465

Browse files
committed
added detect html tags
1 parent ba50811 commit 5596465

File tree

3 files changed

+59
-83
lines changed

3 files changed

+59
-83
lines changed

data_structures/partition_min.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Partition:
2+
class Position:
3+
__slots__ = '_container', '_element', '_size', '_parent'
4+
5+
def __init__(self, container, e):
6+
self._container = container # reference to Partition instance
7+
self._element = e
8+
self._size = 1
9+
self._parent = self # convention for a group leader
10+
11+
def element(self):
12+
return self._element
13+
14+
def _validate(self, p):
15+
if not isinstance(p, self.Position):
16+
raise TypeError('p must be proper Position type')
17+
if p._container is not self:
18+
raise ValueError('p does not belong to this container')
19+
20+
def make_group(self, e):
21+
return self.Position(self, e)
22+
23+
def find(self, p):
24+
self._validate(p)
25+
if p._parent != p:
26+
p._parent = self.find(p._parent) # overwrite p._parent after recursion
27+
return p._parent
28+
29+
def union(self, p, q):
30+
a = self.find(p)
31+
b = self.find(q)
32+
if a is not b: # only merge if different groups
33+
if a._size > b._size:
34+
b._parent = a
35+
a._size += b._size
36+
else:
37+
a._parent = b
38+
b._size += a._size

hackerrank/graphs/components-in-graph.py

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,9 @@
22

33
import os
44
import sys
5-
from collections import defaultdict
5+
from data_structures.graph_no_vertice_object import Graph
6+
from data_structures.partition import Partition
67

7-
class Partition:
8-
class Position:
9-
__slots__ = '_container', '_element', '_size', '_parent'
10-
11-
def __init__(self, container, e):
12-
self._container = container # reference to Partition instance
13-
self._element = e
14-
self._size = 1
15-
self._parent = self # convention for a group leader
16-
17-
def element(self):
18-
return self._element
19-
20-
def _validate(self, p):
21-
if not isinstance(p, self.Position):
22-
raise TypeError('p must be proper Position type')
23-
if p._container is not self:
24-
raise ValueError('p does not belong to this container')
25-
26-
def make_group(self, e):
27-
return self.Position(self, e)
28-
29-
def find(self, p):
30-
self._validate(p)
31-
if p._parent != p:
32-
p._parent = self.find(p._parent) # overwrite p._parent after recursion
33-
return p._parent
34-
35-
def union(self, p, q):
36-
a = self.find(p)
37-
b = self.find(q)
38-
if a is not b: # only merge if different groups
39-
if a._size > b._size:
40-
b._parent = a
41-
a._size += b._size
42-
else:
43-
a._parent = b
44-
b._size += a._size
45-
46-
class Graph:
47-
class Edge:
48-
__slots__ = '_origin', '_destination'
49-
50-
def __init__(self, u, v):
51-
self._origin = u
52-
self._destination = v
53-
54-
def endpoints(self):
55-
return self._origin, self._destination
56-
57-
def opposite(self, v):
58-
return self._destination if v == self._origin else self._origin
59-
60-
def __hash__(self): # will allow edge to be a map/set key
61-
return hash((self._origin, self._destination))
62-
63-
def __str__(self):
64-
return '({0},{1})'.format(self._origin, self._destination)
65-
66-
def __init__(self):
67-
self._outgoing = defaultdict(dict)
68-
69-
def vertices(self):
70-
return self._outgoing.keys()
71-
72-
def get_edge(self, u, v):
73-
return self._outgoing[u].get(v) # returns None if v not adjacent
74-
75-
def incident_edges(self, v):
76-
adj = self._outgoing
77-
for edge in adj[v].values():
78-
yield edge
79-
80-
def insert_edge(self, u, v):
81-
if self.get_edge(u, v) is not None: # includes error checking
82-
raise ValueError('u and v are already adjacent')
83-
e = self.Edge(u, v)
84-
self._outgoing[u][v] = e
85-
self._outgoing[v][u] = e
86-
#
87-
# Complete the componentsInGraph function below.
88-
#
898
def componentsInGraph(gb):
909
forest = Partition()
9110
p = {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import re
2+
3+
matches = set()
4+
5+
p = re.compile('<\s*(\w+)')
6+
7+
# p = re.compile('<\\b(\w+)')
8+
9+
# p = re.compile('<\s*([a-zA-Z0-9]+)')
10+
11+
# p = re.compile('<\s*([a-zA-Z0-9]+)\s?(?=[>\w(/>)])')
12+
13+
# p = re.compile('(?<=<)\s?([a-zA-Z]+)\s?(?=[>\w])')
14+
15+
for _ in range(int(input())):
16+
m = p.findall(input())
17+
matches.update(m)
18+
19+
print(';'.join(sorted(matches)))

0 commit comments

Comments
 (0)