Skip to content

Instantly share code, notes, and snippets.

@m00nlight
m00nlight / gist:245d917cb030c515c513
Last active June 25, 2022 05:57
Python heap optimize dijkstra algorithm
import sys
from heapq import heappush, heappop
class Dijkstra:
def __init__(self, adjacents):
self.adj = adjacents
self.n = len(adjacents)
def dijkstra(self, start):
dis, vis, hq = {}, {}, []
@m00nlight
m00nlight / gist:daa6786cc503fde12a77
Last active April 26, 2025 15:50
Python KMP algorithm
class KMP:
def partial(self, pattern):
""" Calculate partial match table: String -> [Int]"""
ret = [0]
for i in range(1, len(pattern)):
j = ret[i - 1]
while j > 0 and pattern[j] != pattern[i]:
j = ret[j - 1]
ret.append(j + 1 if pattern[j] == pattern[i] else j)
@m00nlight
m00nlight / gist:e4fc71e0d37b51842e5f
Last active August 29, 2015 14:14
Python N-Queen problem
class NQueen:
# @return a list of lists of string
def solveNQueens(self, n):
self.__ans = []
self.__stack = []
self.__n = n
self.solve(0)
return self.__ans

Every so often I have to restore my gpg keys and I'm never sure how best to do it. So, I've spent some time playing around with the various ways to export/import (backup/restore) keys.

Method 1

Backup the public and secret keyrings and trust database

cp ~/.gnupg/pubring.gpg /path/to/backups/
cp ~/.gnupg/secring.gpg /path/to/backups/
cp ~/.gnupg/trustdb.gpg /path/to/backups/

or, instead of backing up trustdb...