Skip to content

Instantly share code, notes, and snippets.

View advpetc's full-sized avatar
💱

Haoyang advpetc

💱
  • Linkedin
View GitHub Profile
@advpetc
advpetc / math.cpp
Created September 7, 2020 16:51
find the power n of x in cpp / find the sqrt of x
// x^n (n can be negative)
double myPow(double x, int n) {
typedef long long LL;
bool is_minus = n < 0;
double res = 1;
for (LL k = abs(LL(n)); k; k >>= 1) {
if (k & 1) res *= x;
x *= x;
}
if (is_minus) res = 1 / res;
@advpetc
advpetc / snippets.cpp
Last active June 5, 2019 23:09
Leetcode snippets
/* Binary search */
// find first element from the sorted list that is greater than certain number
// e.g. find 3 from [2, 4, 5, 6, 9], ans should be the index of element 4 (first greater than)
int find(vector<int>& nums, int target) {
int left = 0, right = nums.size();
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) left = mid + 1;
@advpetc
advpetc / 0_reuse_code.js
Created August 31, 2017 05:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@advpetc
advpetc / instructions.md
Last active August 9, 2017 05:54
PostgresSQL back_up

backup instructions

Here are some scripts which will backup all databases in a cluster individually, optionally only backing up the schema for a set list. The reason one might wish to use this over pg_dumpall is that you may only wish to restore individual databases from a backup, whereas pg_dumpall dumps a plain SQL copy into a single file. This also provides the option of specifying which databases you only want the schema of. The idea is to run these in a nightly cron job.

pg_backup.config

  • The main configuration file. This should be the only file which needs user modifications.

pg_backup.sh

  • The normal backup script which will go through each database and save a gzipped and/or a custom format copy of the backup into a date-based directory.

pg_backup_rotated.sh

  • The same as above except it will delete expired backups based on the configuration.
import redis
class Monitor():
def __init__(self, connection_pool):
self.connection_pool = connection_pool
self.connection = None
def __del__(self):
try:
self.reset()
@advpetc
advpetc / eli_cjk.py
Last active July 13, 2017 11:23
elimination cjk characters (including cjk punctuations) the efficient way
from nltk.tokenize.util import is_cjk
nltk_punc_range = (65280, 65519)
def eli_cjk(text):
"""
>>> eli_cjk("yes(中文),好玩".decode('utf8'))
u'yes'
>>> eli_cjk("how are you".decode('utf8'))
u'how are you'