- read file into list
with open('data/my_input/1.in') as f:
lines = [ line.strip() for line in f]
- read multi-line with emptyline-separator into list
with open('data/my_input/6.in') as f:
lines = f.read().split('\n\n')
- usual regex functions
num=lambda s : re.findall('(\d+)-(\d+) (.): (.*)',s)
l=num(line)
a= re.compile(r'.*ecl:([a-zA-Z0-9]+)', re.IGNORECASE)
b=a.match(s)
return b and b.groups()[0] in fs
- Counter from collections
from collections import Counter
l=Counter(s)
[ (k,v) for k,v in l.items()]
[ v for v in l.values()]
[ k for k in l.keys()]
- "advanced" search
lis=sorted(lis,key=lambda p:p[1],reverse=True)
- Networkx
import networkx as nx
d=nx.DiGraph()
for li in l:
x,y,z=re.findall('[A-Z]',li)
d.add_edge(y,z)
print(''.join(nx.lexicographical_topological_sort(d)))
another example :
G = nx.DiGraph()
for n in open("data/my_input/day6-input.file").readlines():
G.add_edge(*[x.strip() for x in n.split(")")])
print(nx.transitive_closure(G).size())
print(nx.shortest_path_length(G.to_undirected(), "YOU", "SAN") - 2)
- geographic move
dx=dict(zip('LRUD',[-1,1,0,0]))
dy=dict(zip('LRUD',[0,0,1,-1]))
e.g.:
direction='L'
x+=dx[direction]
y+=dy[direction]
or
LRUD = [(0, 1), (0, -1), (1, 0), (-1, 0)]
LRUD = {(0, 1), (0, -1), (1, 0), (-1, 0)}
for lrud in LRUD:
dx, dy = lrud
or (less efficient)
for dj in [-1, 0, 1]:
for di in [-1, 0, 1]:
if (dj,di) == (0,0):
continue
- alphabetic search
string.ascii_lowercase
- visualization
def printmap(d2):
xmin = min([x for x, y in d2])
ymin = min([y for x, y in d2])
xmax = max([x for x, y in d2])
ymax = max([y for x, y in d2])
# print(xmin, ymin, xmax, ymax)
for i in range(xmin - 1, xmax + 2):
sprint = ""
for j in range(ymin - 1, ymax + 2):
sprint += str(d2[i, j]) if (i, j) in d2 else "."
print(sprint.replace("1", "O").replace("0", " "))
- DFS (stack)
queue=[]
visited=[]
queue.append(a)
while queue:
node=queue.pop()
if node in visited:
#donothing
else:
# do
visited.append(node)
queue.append(resultFromDo(node))
- BFS (queue) - shortest path
queue=[]
visited=[]
queue.append(a)
while queue:
node=queue.pop(0)
if node in visited:
#donothing
else:
# do
visited.append(node)
queue.append(resultFromDo(node))