Skip to content

Instantly share code, notes, and snippets.

View tkhoa2711's full-sized avatar

Khoa Le tkhoa2711

View GitHub Profile
@tkhoa2711
tkhoa2711 / bash-string-manipulation.txt
Created December 8, 2015 08:18
Info about string manipulation in Bash
http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
@tkhoa2711
tkhoa2711 / libcheck.sh
Last active December 8, 2015 05:51
Check if a library is installed in UNIX
# http://serverfault.com/questions/54736/how-to-check-if-a-library-is-installed
# work 99% of the time
alias libcheck='ldconfig -p | grep '
# other ways
# for deb-based distribution
# dpkg -s packagename
# for exact filename search
# locate filename
@tkhoa2711
tkhoa2711 / tmux-cheatsheet
Created November 17, 2015 03:55
tmux cheatsheet
https://gist.github.com/MohamedAlaa/2961058
@tkhoa2711
tkhoa2711 / system-process.el
Last active November 30, 2015 02:31
Emacs and system process
(defun system-process-running-p (regex-str)
"Check whether there is a system process running whom name matches REGEX-STR."
(dolist (pid (list-system-processes))
(let ((attrs (process-attributes pid)))
(if (string-match-p regex-str
(cdr (assoc 'comm attrs)))
(return t)))))
@tkhoa2711
tkhoa2711 / locate-ip.el
Created October 27, 2015 07:56
Get the location of an IP address
(defvar api-query "http://api.netimpact.com/qv1.php?key=your-key&qt=geoip&d=json&q=")
(defun locate-ip (&optional ip)
(let ((url-request-method "GET"))
(url-retrieve (concat api-query ip)
(lambda (status) (switch-to-buffer (current-buffer))))))
@tkhoa2711
tkhoa2711 / water_tower.py
Created October 15, 2015 10:25
How much water is collected between towers with different heights
import numpy as np
from itertools import izip
def _fold(func, iterable, iterize):
_iter = iterize(iterable)
res = _iter.next()
yield res
for i in _iter:
res = func(res, i)
yield res
@tkhoa2711
tkhoa2711 / fn.py
Created October 15, 2015 06:34
Functional Python
import time
def tail(f, from_start=False):
try:
if from_start:
f.seek(0, 2) # go to the end of file
while True:
line = f.readline()
if not line:
@tkhoa2711
tkhoa2711 / point_inside_triangle.py
Created October 15, 2015 03:21
Test if a point is inside a triangle
import numpy as np
def point_inside_triangle(p, a, b, c):
'''
Check whether point p is inside the triangle of (a,b,c).
'''
return same_side(p, a, b, c) \
and same_side(p, b, a, c) \
and same_side(p, c, a, b)
@tkhoa2711
tkhoa2711 / last-modified-file.el
Created October 13, 2015 01:42
Find the last modified file within a directory
(defun last-modified-file (parent-dir &optional pattern)
"Get the last modified file under `parent-dir'.
A regex `pattern' can be provided to only look for those that match."
(let ((default-directory parent-dir))
(first
(sort
(directory-files default-directory 'absolute pattern 'nosort)
(lambda (x y)
(time-less-p (nth 5 (file-attributes y))
(nth 5 (file-attributes x))))))))
@tkhoa2711
tkhoa2711 / Makefile
Last active October 4, 2015 15:46
A minimal memory pool allocator
CFLAGS=-g -O2 -W -Wall
TEST_USE_POOL=-DTEST_USE_POOL
OBJS=main.o pool.o
DEPS=pool.h
TARGETS=test_pool test_no_pool
TEMPS=main_pool.c
.PHONY : all clean test