Skip to content

Instantly share code, notes, and snippets.

View akayj's full-sized avatar
:octocat:

Jian Yu akayj

:octocat:
  • Shanghai, China
  • 11:13 (UTC +08:00)
View GitHub Profile
@akayj
akayj / Lgz.js
Last active September 28, 2015 11:08
Logging for JavaScript
var Lgz = function(lgStr) {
var fn, msg, console = window.console || null;
var splitPos = lgStr.indexOf(':'), len = lgStr.length;
var fnMap = {
'i' : 'info',
'e' : 'error',
'w' : 'warn',
'l' : 'log'
@akayj
akayj / my_strcpy.c
Last active August 29, 2015 13:56
strcpy from scratch
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *my_strcpy(char *strDest, const char *strSrc)
{
if (strSrc == NULL || strDest == NULL)
return NULL;
char *dest = strDest;
@akayj
akayj / ReadNumber.c
Created March 10, 2014 05:33
Read number from command line, ignore char.
#include <stdio.h>
int main(int argc, char *argv[])
{
int input_var = 0;
int count;
do {
printf("Enter a number (-1 = quit): ");
count = scanf("%d", &input_var);
@akayj
akayj / 0_reuse_code.js
Last active August 29, 2015 14:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@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,
@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 / 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 / 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_tiny.py
Last active January 12, 2016 03:59
Tiny palindrome
palindrome = lambda s: s == s[::-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)