This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
NewerOlder