Skip to content

Instantly share code, notes, and snippets.

// Determine support properties
(function( xhr ) {
jQuery.extend( jQuery.support, {
ajax: !!xhr,
cors: !!xhr && ( "withCredentials" in xhr )
});
})( jQuery.ajaxSettings.xhr() );
@dobrokot
dobrokot / may_be.hpp
Created April 10, 2012 08:45
nullable/optinal/MayBe - store some value or empty.
#ifndef MAY_BE_HEADER
#define MAY_BE_HEADER
#include <string.h>
#include <memory>
#include <assert.h>
///для того, что бы удобней было вернуть из функции пустое или дефолтное значение, например
/** \code
MayBe<Point> f() {
@dobrokot
dobrokot / may_be.hpp
Created April 10, 2012 08:52
nullable/optinal/MayBe - store some value or empty
#ifndef MAY_BE_HEADER
#define MAY_BE_HEADER
#include <string.h>
#include <memory>
#include <assert.h>
///для того, что бы удобней было вернуть из функции пустое или дефолтное значение, например
/** \code
MayBe<Point> f() {
@dobrokot
dobrokot / may_be_usage.cpp
Created April 10, 2012 09:00
Shows usage of may_be.hpp
#include "may_be.hpp"
#include <iostream>
struct Point { //тип, над которым мы будем эксперементировать.
float x, y;
Point(float x, float y): x(x), y(y) {}
Point(): x(0), y(0) {}
};
@dobrokot
dobrokot / may_be_usage.cpp
Created April 10, 2012 09:02
Shows usage of may_be.hpp
#include "may_be.hpp"
#include <iostream>
struct Point { //тип, над которым мы будем эксперементировать.
float x, y;
Point(float x, float y): x(x), y(y) {}
Point(): x(0), y(0) {}
};
#include <boost/shared_ptr.hpp>
#include <iostream>
struct T {
boost::shared_ptr<MayBe<T> > next;
int i;
T() { i = 10; }
};
@dobrokot
dobrokot / gist:2470067
Created April 23, 2012 10:07
compare set contains element on strings of ~tens symbols
import re
def f1(c):
return c in ''' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"\\'()*-.:<>[]^`{|}~'''
ALLOWED = re.compile('''[ a-zA-Z0-9!"'()*.:<>^`{|}~\\\[\]-]''')
def f2(c):
return bool(re.match(ALLOWED, c))
@dobrokot
dobrokot / knight_random_walk.py
Created May 9, 2012 12:17
knight random walk
# Ivan Dobrokotov, dobrokot 'at' gmail.com
def calc_moves(x, y):
result = []
for dx, dy in [(1, 2), (2, 1), (-1, 2), (-2, 1), (-1, -2), (-2, -1), (1, -2), (2, -1)]:
nx, ny = (x + dx, y+dy)
if 0 <= nx < 8 and 0 <= ny < 8:
result.append((nx, ny))
return result
@dobrokot
dobrokot / tree_cost.py
Created May 11, 2012 09:31
minimal tree cost built from list
# minimal tree cost built from list, problem from http://antilamer.livejournal.com/431230.html
# cost of tree is sum(depth(i)*value(i))
list = [1,2,3,4,5,7,6,5,4,3,2,10000]
n = len(list)
#table from (start, length) to (cost, x-sum, root) for best tree on segment (start, length)
table_start_l = [[(0,0,-1000)]*(n-start+1) for start in xrange(n+1)]
@dobrokot
dobrokot / segment_intersection.cpp
Created May 20, 2012 19:04
segments robust intersection code
#include <stdio.h>
struct Point2f {
float x, y;
Point2f(float _x, float _y): x(_x), y(_y) {}
Point2f(): x(0), y(0) {}
};
Point2f operator -(Point2f p1, Point2f p2) {