Skip to content

Instantly share code, notes, and snippets.

View dboyliao's full-sized avatar

dboyliao dboyliao

View GitHub Profile
@dboyliao
dboyliao / hash.c
Created November 23, 2017 02:02 — forked from tonious/hash.c
A quick hashtable implementation in c.
#define _XOPEN_SOURCE 500 /* Enable certain library functions (strdup) on linux. See feature_test_macros(7) */
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
struct entry_s {
char *key;
char *value;
@dboyliao
dboyliao / count7.py
Last active November 26, 2017 04:23
Interview Question 3 by Cardinal Blue
from math import log10, factorial
# O(nlog(n)) implementation
def count7(n, acc=0):
if n <= 0:
return acc
while n > 0:
v = n
while v > 0:
v, r = v // 10, v % 10
@dboyliao
dboyliao / data_model.ipynb
Last active November 15, 2017 09:06
data model demo
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python3
# -*- coding:utf8 -*-
from __future__ import print_function
from copy import deepcopy
class MyAwesomObject(object):
def __init__(self, private_value):
self.__private_value = private_value
@property
@dboyliao
dboyliao / iter_row.py
Created October 10, 2017 13:40
iterate over row in pandas example
#!/usr/bin/env python3
# -*- coding:utf8 -*-
import pandas as pd
data = {"split": ["台灣 世界競爭力", "台灣股票開盤", "台電躺著賺"],
"label": ["positive", "negative", "positive"]}
df = pd.DataFrame(data)
count = 0
for i, row in df.itertuples()
if "台灣" in row.split and row.label == "positive":
@dboyliao
dboyliao / tunnel.py
Last active August 18, 2017 15:12
helper script for ssh tunneling local host port to destination port
#!/usr/bin/env python3
# -*- coding: utf8 -*-
from __future__ import print_function
import re
import subprocess as sp
import argparse
import sys
import os
import yaml
@dboyliao
dboyliao / mp_global_var.py
Last active February 24, 2021 16:30
multiprocessing using global variable as read-only data (Bad idea)
#!/usr/bin/env python3
#-*- coding:utf8 -*-
from __future__ import print_function
import numpy as np
import multiprocessing as mp
import sys
from multiprocessing.queues import Empty
SHARE_DATA = None
@dboyliao
dboyliao / replace_demo.py
Last active August 13, 2017 04:16
Simple demo code for fast array replacement (with performance comparison)
#!/usr/bin/env python
from __future__ import print_function
import argparse
import numpy as np
import time
# a random sequence
arr = np.random.choice(['a', 'b', 'c', 'd'], 5000, replace=True)
positive_values = ['a', 'b']
#!/usr/bin/env python3
# -*- coding:utf8 -*-
def main():
a = 5
def foo():
nonlocal a;
a += 1
print(a)
print(foo())
@dboyliao
dboyliao / class_deco_example.py
Last active August 9, 2017 15:30
python class decorator example (Just for fun XDD)
# -*- coding:utf-8 -*-
from __future__ import print_function
from functools import wraps
def log_deco(func):
@wraps(func)
def wrapped(*args, **kwargs):
print("calling {}".format(func.__name__))
return func(*args, **kwargs)