Skip to content

Instantly share code, notes, and snippets.

View trhura's full-sized avatar

Thura Hlaing trhura

View GitHub Profile
@trhura
trhura / retry.py
Last active December 18, 2015 20:29
Python decorator to retry function calls on Exception
import functools
def retry (function, retry=3):
@functools.wraps(function)
def wrapper (*args, **kwargs):
exception = None
for i in range (retry):
try:
return function (*args, **kwargs)
except Exception, ex:
@trhura
trhura / shanno-fano.lisp
Last active December 18, 2015 17:49
Implementation of static shanno-fano algorithm in Common Lisp
;; Implementation of static shanno-fano algorithm
;; Algorithm - (Taken from Wikipedia)
;; 1. For a given list of symbols, develop a corresponding list of probabilities
;; or frequency counts so that each symbol’s relative frequency of occurrence is known.
;; 2. Sort the lists of symbols according to frequency, with the most frequently
;; occurring symbols at the left and the least common at the right.
;; 3. Divide the list into two parts, with the total frequency counts of the left half
;; being as close to the total of the right as possible.
;; 4. The left half of the list is assigned the binary digit 0, and
@trhura
trhura / rle.lisp
Last active December 18, 2015 17:49
Run-length encoding (RLE) in Common Lisp
;; Implementation of a simple variant of RLE
;; Algorithm
;; Whenever a character is repeated (appears 2 times), we put a number after it
;; which inidicate how many more times it is repeated.
;; For instance, "wwwwwwwwbb3333333123" will be decoded into"ww6bb0335123"
(defun rle-encode (string)
(loop as prev-char = #\Null then char
for char in (coerce string 'list)
@trhura
trhura / huffman.lisp
Last active December 18, 2015 17:49
Implementation of static minimum variance huffman algorithm in Common Lisp
;; Implementation of static minimum variance huffman algorithm
(defun hf-get-symbol-table (string &key (for 'encode))
;; Get the symbol table :for can be either encode or decode
;; For encode, the hashtable will use the symobls as keys
;; For decode, the hashtable will use the prefix codes as keys
(labels ((sort-func (x y) (< (cdr x) (cdr y)))
(build-tree (list)
(if (<= (length list) 2)
(cons (caar list) (caadr list))
@trhura
trhura / tic-tac-toe.lisp
Last active February 9, 2016 08:47
tic-tac-toe AI using two-ply minmax in racket (scheme)
#lang scheme/gui
(define new-game #t)
(define game-finished #f)
(define user-move #\X)
(define computer-move #\O)
(define move-count 0)
(define start-move user-move)
@trhura
trhura / if_nametoindex, if_indextoname.py
Last active April 5, 2019 12:51
if_nametoindex, if_indextoname functions for python 2.x. In python 3.x, they are available in socket module.
import ctypes
import ctypes.util
libc = ctypes.CDLL(ctypes.util.find_library('c'))
def if_nametoindex (name):
if not isinstance (name, str):
raise TypeError ('name must be a string.')
ret = libc.if_nametoindex (name)
if not ret: