Skip to content

Instantly share code, notes, and snippets.

@airekans
airekans / random_shuffle.cpp
Created March 27, 2014 00:39
A random shuffling algorithm
template<typename T>
void random_shuffle(std::vector<T>& elems)
{
for (int i = 1; i < elems.size(); ++i)
{
int index = rand() % (i + 1);
if (index != i)
{
swap(elems[i], elems[index]);
}
@airekans
airekans / heap_merge.cpp
Last active August 29, 2015 13:57
Use a heap to merge multiple files.
typedef pair<int, int> Elem;
bool compare(const Elem& lhs, const Elem& rhs)
{
return lhs.first < rhs.first;
}
int main()
{
vector<ifstream*> files;
@airekans
airekans / timer.html
Created March 20, 2014 04:33
A sample JS timer in HTML
<html>
<head>
<script>
window.onload = function() {
var test_elem = document.getElementById('test');
var number = 1;
test_elem.innerText = number;
setInterval(function() {
++number;
test_elem.innerText = number;
@airekans
airekans / echo_server.py
Created March 3, 2014 14:41
A simple echo server using gevent.
import gevent.server
def handle(socket, addr):
print addr
while True:
try:
content = socket.recv(1024)
if len(content) == 0:
break
@airekans
airekans / parse.py
Created January 22, 2014 08:05
A simple parser based on the lexer mentioned in https://github.com/alanszlosek/mercussion/blob/master/lexer.py
#! /usr/bin/env python
#
# The grammar for the parsed language is very simple.
# It's like a JSON variation.
# Here's a simple valid expression:
# {
# a 1,
# b 2,
# c {
# c_1 3,
@airekans
airekans / net.py
Created January 2, 2014 09:04
parse /proc/net/dev file to get network statistics.
import pprint
if __name__ == '__main__':
proc_net_file = open('/proc/net/dev')
field_values = {}
for l in proc_net_file:
if ':' in l:
head_content = l.split(':')
@airekans
airekans / get_ip.cpp
Last active January 1, 2016 19:59
Some useful /proc information I can use to check the state of the system.
// This program simply gets the ip for the specified interface.
#include <iostream>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
@airekans
airekans / go.sh
Created December 30, 2013 03:50
A script used to login remote machine without human interaction by using expect.
#! /usr/bin/expect -f
set host [lindex $argv 0]
set user [lindex $argv 1]
set pwsd [lindex $argv 2]
spawn ssh $user@$host
expect "yes/no" {
send "yes\r"
@airekans
airekans / libevent_echo_server.cpp
Last active January 1, 2016 10:29
All kinds of echo server implementations.
/*
This exmple program provides a trivial echo server program that listens for TCP.
Where possible, it exits cleanly in response to a SIGINT (ctrl-c).
*/
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
@airekans
airekans / diff.py
Created December 5, 2013 08:49
Get the diff of the two sorted sequence stored in files.
import sys
if __name__ == '__main__':
file1, file2 = sys.argv[1:]
file1_fd = open(file1)
file2_fd = open(file2)
line1 = file1_fd.readline()
line2 = file2_fd.readline()