Skip to content

Instantly share code, notes, and snippets.

View akayj's full-sized avatar
:octocat:

Jian Yu akayj

:octocat:
  • Shanghai, China
  • 08:02 (UTC +08:00)
View GitHub Profile
@akayj
akayj / sync-git-repos.el
Created May 15, 2020 11:36
sync two git repos
;;; sync-git-repos.el -- sample emacs lisp script
;;; Commentary:
;;; Code:
;; colors
(defconst *color-nc* "\033[0m")
(defconst *color-red* "\033[31m")
(defconst *color-green* "\033[32m")
(defconst *color-yellow* "\033[33m")
def snake(n):
arr = [[0 for _ in range(n)] for _ in range(n)]
value = 1
border = 0
while value <= n*n:
x, y = border, border
for y in range(border, n-border):
@akayj
akayj / server.py
Created April 27, 2016 17:19 — forked from martijnvermaat/server.py
SimpleHTTPServer with history API fallback
#!/usr/bin/env python
"""
Modification of `python -m SimpleHTTPServer` with a fallback to /index.html
on requests for non-existing files.
This is useful when serving a static single page application using the HTML5
history API.
"""
@akayj
akayj / timeout_thread.py
Last active September 9, 2016 17:09
timeout implemented by thread
import threading
from functools import wraps
from Queue import Queue, Empty
class Timeout(object):
def __init__(self, seconds):
self.seconds = seconds
self.q = Queue(1)
@akayj
akayj / timeout.py
Last active December 3, 2015 15:52
Useful decorator - Timeout
import signal
import functools
class TimeoutError(Exception): pass
def timeout(seconds, error_message='Function called timed out!'):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
@akayj
akayj / palindrome_tiny.py
Last active January 12, 2016 03:59
Tiny palindrome
palindrome = lambda s: s == s[::-1]
@akayj
akayj / parlindrome_small.py
Last active November 18, 2015 13:25
parlindrome_small
def parlindrome(s):
splitter = len(s)/2
return s[:splitter] == s[-1:-1-splitter:-1]
@akayj
akayj / palindrome_simple.py
Last active November 17, 2015 05:55
Palindrome_2
# -*- coding: utf-8 -*-
def palindrome(s):
a = 0
b = len(s) - 1
while a < b:
if s[a] != s[b]:
return False
a, b = a+1, b-1
return True
@akayj
akayj / palindrome.py
Created November 12, 2015 06:58
Palindrome Detect
#!/usr/bin/env python2
def palindrome(s):
length = len(s)
barrier = length / 2
head2tail = (c for c in s)
tail2head = (s[i] for i in xrange(length-1, -1, -1))
for _ in xrange(barrier):
if head2tail.next() != tail2head.next():
@akayj
akayj / fib_generator.py
Last active November 6, 2015 03:12
Fibonacci generator
#!/usr/bin/env python2
def fib(max):
a, b = 0, 1
for _ in xrange(n):
yield b
a, b = b, a+b
for i in fib(10):
print i,