Skip to content

Instantly share code, notes, and snippets.

@airekans
airekans / earth_velocity.py
Created July 10, 2012 03:53
Earth Velocity
def calculate_earth_velocity_from_plain(v_plain, t_plain, t_diff, is_invert):
total_distance = v_plain * t_plain
return total_distance / ([t_diff, 24 - t_diff][int(is_invert)])
# Wenzhe's input
print calculate_earth_velocity_from_plain(950.0, 14, 15, true)
@airekans
airekans / ast2sym.py
Created July 20, 2012 05:35
AST to symbol representation tranformation
#! /usr/bin/env python
import token
import symbol
def ast2symbol(ast):
if len(ast) < 2:
if type(ast) is type(""):
return ast
else:
@airekans
airekans / video_sync.py
Created July 24, 2012 01:34
sync video server
def stringReceived(self, shortUrl):
self.transport.loseConnection()
self.downloadVideoFromShortUrl(shortUrl)
def downloadVideoFromShortUrl(self, shortUrl):
try:
url = transformShortUrl(shortUrl)
video = downloadVideoFromUrl(url)
storeVideo(video)
except BaseException, e:
@airekans
airekans / memlayout.cpp
Created July 31, 2012 09:26
Memory Layout for C++ multiple inheritance
// The output from the program compiled under gcc 3.4.4
// sizeof Base: 8
// sizeof Derived: 12
// sizeof AnotherBase: 8
// sizeof AnotherDerived: 20
// And you can run the program to get the log.txt to check its content
// =========================================================
//
// The inheritance relationship is as follow:
// class Base;
@airekans
airekans / max_subtree.py
Created September 18, 2012 02:34
max subtree
class Node:
def __init__(self):
self.left_child = None
self.right_child = None
self.num = 0
def max_node(tree):
return visit(tree)[1]
@airekans
airekans / weight_balance.py
Created October 9, 2012 22:01
Hacking Spot Vol.1 Issue 3: Weights and Balance
#! /usr/bin/env python
# num is a integer >= 0
# return a list containing the digits in base
# the digits are ordered from lower bits to higher bits
def convert_to_based_num(num, base):
digits = []
while num > 0:
quotient, remainder = num / base, num % base
digits.append(remainder)
@airekans
airekans / simple_popen.cpp
Created October 24, 2012 21:15
Popen in C++
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main(int argc, char *argv[])
@airekans
airekans / min_stack.py
Created November 8, 2012 14:55
min stack that is a stack and a min-heap
class MinStack(object):
""" Simulate the normal stack operations while
providing getting minimum with O(1) complexitiy
"""
def __init__(self):
self.__stack = []
self.__min_stack = []
def push(self, n):
@airekans
airekans / least_positive.py
Created December 12, 2012 15:45
Least positive number in an array of integer numbers.
def least_positive(nums):
length = len(nums)
is_valid = lambda e: 0 < e <= length
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]
i = 0
while i < length:
e = nums[i]
@airekans
airekans / clang_preprocessor.cpp
Created December 23, 2012 07:51
Clang Preprocessor for Clang 3.2
#include <string>
#include <iostream>
#include "llvm/Config/config.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/SourceManager.h"