Skip to content

Instantly share code, notes, and snippets.

View sokrato's full-sized avatar
💭
All (not just black) Lives Matter!

Sokrato sokrato

💭
All (not just black) Lives Matter!
  • Alibaba
  • HangZhou, China
View GitHub Profile
@sokrato
sokrato / exclude.sh
Created April 20, 2012 04:33
exclude some pub key files
#authorized keys file name
AK=authorized_keys
#enabled pub key dir
ENABLED=enabled_keys
#disabled pub key dir
DISABLED=disabled_keys
if [[ $# = 0 ]]
then
printf "Usage:\n %s to_be_removed.pub\n" "$0"
@sokrato
sokrato / oop.js
Created October 18, 2013 11:45
JavaScript Inheritance
var Base = {
create: function(attrs){
var o=this.clone();
o.init(attrs);
return o;
},
clone: function() {
var o={};
for(var i in this)
if (this.hasOwnProperty(i))
@sokrato
sokrato / wc.c
Created November 10, 2013 13:40
word counting program in C
#include<stdio.h>
#include<string.h>
#include<ctype.h>
struct node{
int num;
char *p;
struct node *next;
};
@sokrato
sokrato / postfix_calculator.py
Last active March 14, 2016 11:03
Postfix calculator implementation in Python, infix to postfix transformation, for theories, see http://www.smccd.net/accounts/hasson/C++2Notes/ArithmeticParsing.html
'''
Infix calculator
First tokenize infix expression,
then transform it into postfix,
then calculate it.
E.g.:
9 - 5 + 2 * 3
=> 9 5 - 2 3 * +
'''
import re
EGIN {
FIELD_LIST = "Field1,Field2,Field3"; # fields definition, in order
split(FIELD_LIST, fields, ",");
}
{
pos = find(fields, $1);
if (pos == 0) {
print("unexpected field " $1);
exit 1;
@sokrato
sokrato / log_ana.c
Last active January 3, 2016 00:39
解析一个912M(8e6行)的日志文本文件,同样的逻辑Python用了4'05'', C程序用了14''. 性能还是有差距的啊!
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define MAXLINE 1024
#define DATE_FORMAT "%Y-%m-%d %H:%M:%S"
#define GAP_IN_SEC 2
@sokrato
sokrato / advanced.py
Created January 14, 2014 03:31
Example advanced Python features
#-*- coding: utf8 -*-
'''
Examples of advanced Python features:
- metaclass
- descriptor
- generator/forloop
'''
from __future__ import print_function
import sys
@sokrato
sokrato / brainfuck.c
Created January 14, 2014 11:28
BrainFuck compiler
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/*
> becomes ++p;
< becomes --p;
+ becomes ++*p;
- becomes --*p;
@sokrato
sokrato / multiple.py
Created January 15, 2014 11:48
Multiples of 3 and 5, http://projecteuler.net/problem=1, performance difference
#-*- coding:utf8 -*-
from __future__ import print_function
import sys
import time
if sys.version_info < (3,0):
range = xrange
#-*- coding: utf8 -*-
def isPrime(num, ps):
for i in ps:
if num%i == 0:
return False
if i**2 > num:
break
return True